0

I've been trying to write a client process that will communicate with a serve.

I used the same code below but instead of scanner i used BufferedReader and the client input was a string. Furthermore, the server just changes the string to uppercase and sends it back to the client to be displayed on the screen. It worked.

However, when i changed the code so that the client can enter a double number [(ex: 1.6) the server should round it and send it back so that it'll be printed on the screen], i get no response.

And i got this error:

run:
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at socketserver.SocketServer.main(SocketServer.java:38)
Java Result: 1

notes:

  1. I am using NetBeans.
  2. I used Scanner instead of using BufferedReader so that i'll be able to read a double number.

Client class

package socketclient;

import java.io.*; 
import java.net.*; 
import java.util.Scanner;


public class SocketClient {

    /**
     * @param args the command line arguments
     */
   public static void main(String args[]) throws Exception 
    { 
        double inputDouble; 
        double modifiedNum; 

        Scanner inFromUser = new Scanner(System.in);


        Socket clientSocket = new Socket("localhost", 6789); 

        DataOutputStream outToServer = 
          new DataOutputStream(clientSocket.getOutputStream());


          Scanner inFromServer = new Scanner(clientSocket.getInputStream());


        inputDouble = inFromUser.nextDouble(); 

        outToServer.writeDouble(inputDouble);


        modifiedNum = inFromServer.nextDouble(); 

        System.out.println("FROM SERVER: " + modifiedNum); 

        outToServer.close(); 
        clientSocket.close();

    } 

}

Server class

package socketserver;

import java.io.*; 
import java.net.*; 
import java.util.Scanner;


public class SocketServer {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception 
    { 
      double clientNum; 
      double roundedNum;

      ServerSocket welcomeSocket = new ServerSocket(6789); 

      while(true) { 

            Socket connectionSocket = welcomeSocket.accept(); 

            Scanner inFromClient = new Scanner(connectionSocket.getInputStream());


            DataOutputStream  outToClient = 
             new DataOutputStream(connectionSocket.getOutputStream()); 

           clientNum = inFromClient.nextDouble(); 


           roundedNum= Math.round(clientNum);


           outToClient.writeDouble(roundedNum); 
        } 
    } 


}
2
  • 1
    Follow this tut tutorialspoint.com/java/java_networking.htm Commented Feb 22, 2015 at 17:13
  • @Apurva thank you it's helpful. However, i want to know what exactly did i do wrong. Commented Feb 22, 2015 at 17:32

1 Answer 1

1

Your error is to use Scanner to read data sent by client from the server and the converse.

You use DataOutputStream to sent a double so you need a DataInputStream to read the double; on both sides.

Change your Scanners to DataInputStream for inputFromServer and inputFromClient. Also don't forget to flush your outputs and close the streams at the end.

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.