我的客户端类应该从用户获取10个整数输入并将其发送到服务器。服务器应该接受这10个数字并对它们进行排序。然后它应该将数组发送回客户端,客户端应该打印它们。我的客户代码是:
public class TCPClient {
public static void main(String[] args) throws UnknownHostException, IOException {
int arr[]=new int[10];
int arrFromServer[]=new int[10];
BufferedReader inFromUser= new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost",6786);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
for(int i=0;i<10;i++)
arr[i]=Integer.parseInt(inFromUser.readLine());
for(int i=0;i<10;i++)
outToServer.writeInt(arr[i]);
for(int i=0;i<10;i++)
arrFromServer[i]=Integer.parseInt(inFromServer.readLine());
for (int i = 0; i < arrFromServer.length; i++) {
System.out.println("From Server::"+arrFromServer[i]);
}
clientSocket.close();
}
}
我的服务器代码是:
public class TCPServer {
public static void main(String[] args) throws IOException {
int arrFromClient[]=new int[10];
ServerSocket welcomeSocket = new ServerSocket(6786);
while(true){
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
for(int i=0;i<10;i++)
{
arrFromClient[i]=Integer.parseInt(inFromClient.readLine());
}
Arrays.sort(arrFromClient);
for (int i = 0; i < arrFromClient.length; i++) {
outToClient.writeInt(arrFromClient[i]);
}
}
}
}
当我尝试运行代码时,客户端继续接受数字,直到我手动终止程序。理想情况下,在10个输入后,它应该转到服务器,服务器应该给出排序的数组。
上述代码有什么问题。任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
您根本无法将Integer.parseInt(inFromClient.readLine())
与writeInt()
混合,您需要使用DataOutputStream
来编写整数,并使用DataInputStream
来阅读它们以确保您的整数将是使用相同的协议/算法进行适当的序列化和反序列化。
服务器强>
int arrFromClient[]=new int[10];
ServerSocket welcomeSocket = new ServerSocket(6786);
while(true){
Socket connectionSocket = welcomeSocket.accept();
DataInputStream inFromClient = new DataInputStream(connectionSocket.getInputStream());
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
for(int i=0;i<10;i++) {
arrFromClient[i] = inFromClient.readInt();
}
Arrays.sort(arrFromClient);
for (int i = 0; i < arrFromClient.length; i++) {
outToClient.writeInt(arrFromClient[i]);
}
}
<强>客户端:强>
int arr[]=new int[10];
int arrFromServer[]=new int[10];
Scanner inFromUser= new Scanner(System.in);
try (Socket clientSocket = new Socket("localhost",6786)) {
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
for(int i=0;i<10;i++)
arr[i]=inFromUser.nextInt();
for(int i=0;i<10;i++)
outToServer.writeInt(arr[i]);
for(int i=0;i<10;i++)
arrFromServer[i]=inFromServer.readInt();
for (int i = 0; i < arrFromServer.length; i++) {
System.out.println("From Server::"+arrFromServer[i]);
}
}
或者,您可以使用ObjectOutputStream
和ObjectInputStream
使用方法writeObject
和readObject
作为对象来编写和读取数组:
服务器强>
ServerSocket welcomeSocket = new ServerSocket(6786);
while(true){
Socket connectionSocket = welcomeSocket.accept();
ObjectInputStream inFromClient = new ObjectInputStream(connectionSocket.getInputStream());
ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
int[] arrFromClient = (int[]) inFromClient.readObject();
Arrays.sort(arrFromClient);
outToClient.writeObject(arrFromClient);
}
<强>客户端:强>
int arr[]=new int[10];
Scanner inFromUser= new Scanner(System.in);
try (Socket clientSocket = new Socket("localhost",6786)) {
ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());
for(int i=0;i<10;i++)
arr[i]=inFromUser.nextInt();
outToServer.writeObject(arr);
int[] arrFromServer = (int[])inFromServer.readObject();
for (int i = 0; i < arrFromServer.length; i++) {
System.out.println("From Server::"+arrFromServer[i]);
}
}