📄 server.java
字号:
import java.net.*;
import java.util.regex.Pattern;
import java.io.*;
public class Server {
public static void main(String[] ar) {
int port = 6666; // just a random port. make sure you enter something between 1025 and 65535.
try {
ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
System.out.println("Waiting for a client...");
Socket socket = ss.accept(); // make the server listen for a connection, and let you know when it gets one.
System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
System.out.println();
int[][] share = new int[4][2];
share[0][0]= 0;
share[1][0]= 1;
share[2][0]= 2;
share[3][0]= 3;
share[0][1]= 0;
share[1][1]= 0;
share[2][1]= 0;
share[3][1]= 0;
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
String outLine[];
int index;
int value;
int totalShare=0;
while(true) {
line = in.readUTF(); // wait for the client to send a line of text.
Pattern p = Pattern.compile(" ");
outLine = p.split(line);
index = Integer.parseInt(outLine[0].trim());
value = Integer.parseInt(outLine[1].trim());
for(int i=3,j=4;i<10;i=i+2,j++)
{
System.out.println("outLine"+j+" "+outLine[i]);
totalShare+=Integer.parseInt(outLine[i].trim());
}
System.out.println("totalShare : "+totalShare);
for(int i=0;i<4;i++)
{
if(share[i][0]==index)
{
share[i][1]=value;
// System.out.println("share[i][2]"+share[i][1]);
}
System.out.println(share[i][0]+" "+share[i][1]);
totalShare += share[i][1];
}
System.out.println("outLine "+outLine[1]);
System.out.println("totalShare : "+totalShare);
System.out.println("Write your string .. :");
line = keyboard.readLine();
out.writeUTF(line); // send the same line back to the client.
out.flush(); // flush the stream to ensure that the data reaches the other end.
System.out.println("Waiting for the next line...");
System.out.println();
}
}catch(Exception x) {
x.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -