0

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.

9
  • Hint: writeInt vs. readLine. Commented Sep 15, 2016 at 14:28
  • I dint get it :( Which one should I use? I am using readLine to read input from user and writeInt to write it to the server. Is this wrong? Commented Sep 15, 2016 at 14:30
  • writeInt doesn't write a line. Both methods are explained in the javadocs, I recommend reading them. Commented Sep 15, 2016 at 14:32
  • I want to write single integer values. I am using for and passing single values to the server. Commented Sep 15, 2016 at 14:34
  • 1
    You can't use readLine() to read what writeInt() writes. Commented Sep 15, 2016 at 14:36

1 Answer 1

2

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.