📄 client.java
字号:
import java.net.*;
import java.util.regex.Pattern;
import java.io.*;
public class Client {
public static void main(String[] ar) {
int serverPort = 6666; // make sure you give the port number on which
// the server is listening.
String address = "127.0.0.1"; // this is the IP address of the server
// program's computer. // the address
// given here means "the same computer
// as the client".
int[][] share = new int[4][2];
share[0][0] = 4;
share[1][0] = 5;
share[2][0] = 6;
share[3][0] = 7;
share[0][1] = 0;
share[1][1] = 0;
share[2][1] = 0;
share[3][1] = 0;
try {
InetAddress ipAddress = InetAddress.getByName(address); // create an
// object
// that
// represents
// the above
// IP
// address.
Socket socket = new Socket(ipAddress, serverPort); // create a
// socket with
// the server's
// IP address
// and server's
// port.
// 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.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
String outLine[];
int index;
int value;
int totalShare = 0;
System.out.println("Type in something and press enter.");
System.out.println();
while (true) {
totalShare = 0;
line = keyboard.readLine(); // wait for the user to type in
// something and press enter.
System.out.println("Sending this line to the server...");
for (int i = 0; i < 4; i++) {
line += " " + share[i][0] + " " + share[i][1];
}
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data
// reaches the other end.
line = in.readUTF(); // wait for the server 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(" " + 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][0] + " " + share[i][1]);
totalShare += share[i][1];
}
System.out.println("totalShare : " + totalShare);
System.out.println();
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -