简体   繁体   中英

Reading and Writing array of int socket programming, java

My client class should take 10 integer input from user and send it to the server. The server should accept these 10 numbers and sort them. It should then send the array back to the client and the client should print them. My client code is:

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();
}
}

My server code is:

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]);
        }
    }
}
}

When I try to run the code the client keeps on accepting numbers until I terminate the program manually. Ideally after 10 inputs it should go to the server and server should give the sorted array.

What is wrong with the above code. Any help will be appreciated. Thanks in advance.

You simply cannot mix Integer.parseInt(inFromClient.readLine()) with writeInt() , you need to use a DataOutputStream to write your integers and DataInputStream to read them to make sure that your integers will be properly serialized and deserialized using the same protocol/algorithm.

Server:

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]);
    }
}

Client:

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]);
    }
}

Alternatively you can use ObjectOutputStream and ObjectInputStream to write and read your array as an object using the methods writeObject and readObject as next:

Server:

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);
}

Client:

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]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM