title: Java client/server calculator not working
body: <p>I'm trying to create a simple calculator using sockets but when I send numbers to the server I have a problem: for example if I want to add 2 numbers x=13 and y=2 the server receives x=64 and y=42 and the result is 106. I think that the problem is with the method write, I have no problem with strings but with numbers I dont know how to work. I'm a beginner, thanks for the help.</p>
  
  <p>SERVER</p>
  
  <pre><code>    package calcolatrice;
  
  import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
      ServerSocket server;
      Socket msocket;
      BufferedReader in;
      DataOutputStream out;
      public Socket attendi() {
          try {
              //creo il server sulla porta ****
              System.out.println("Server in esecuzione..");
              server = new ServerSocket(1122);
  
              //accetto eventuale connessione da parte del client
              msocket = server.accept();
              System.out.println("Client connesso con successo! ");
  
              //chiudo la connessione per evitare altre connessioni
              server.close();
  
              //inizializzo gli stream per consentire la comunicazione
              out = new DataOutputStream(msocket.getOutputStream());
              in = new BufferedReader(new InputStreamReader(msocket.getInputStream()));
  
          } catch (IOException e) {
              System.out.println(e.getMessage());
              System.out.println("Errore durante l'istanza del server");
              System.exit(1);
          }
          return msocket;
      }
  
  
      public void calcola(){
          try{
              double x;
              double y;
              double risultato=0;
              //leggo la scelta e in base a quella eseguo le operazioni
              int scelta = in.read();
  
              //accetto prima i numeri
              x = in.read();
  
              System.out.println("x "+x);
              y = in.read();
              System.out.println("y "+y);
  
              switch (scelta){
                  case 1:
                      System.out.println("Scelta: "+scelta);
                      //somma
                      risultato = x+y;
  
  
              }
              System.out.println("Risultato: "+risultato);
  
          }catch (Exception e){
  
          }
      }
  
  
      public static void main(String args[]) {
          Server myServer = new Server();
          myServer.attendi();
          myServer.calcola();
      }
  }
  </code></pre>
  
  <p>CLIENT</p>
  
  <pre><code>    package calcolatrice;
  
  import java.io.*;
  import java.net.Socket;
  import java.util.Scanner;
  
  public class Client{
      Socket socket;
      protected int porta = 1122;
      String nomeServer = "localhost";
      //stream per comunicare
      DataOutputStream out;
      BufferedReader in;
  
  
      Scanner input = new Scanner(System.in);
  
      public Socket connetti(){
          try {
              System.out.println("Client avviato..");
              //creo il socket per connettermi al server
              socket = new Socket(nomeServer, porta);
              System.out.println("Client connesso con successo al server ' "+nomeServer+" ' sulla porta: "+porta);
              //inizializzo gli stream per consentire la comunicazione
              out = new DataOutputStream(socket.getOutputStream());
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
          } catch (Exception e){
              System.out.println("Errore, impossibile connettersi");
              System.exit(1);
          }
          return socket;
  
      }
  
      //menu per scelta operazioni
      private void menu(){
          try{
              System.out.println("---Calcolatrice---");
              System.out.println("[1.Somma \t 2.Differenza \t 3.Moltiplicazione \t 4.Divisione]");
              System.out.println("0.ESCI");
          } catch (Exception e){
              System.out.println("Errore");
          }
  
  
      }
  
      //funzione per comunicare
      public void comunica(){
          try {
              int scelta=0;
              //do{
                  menu();
                  System.out.println("Inserire operazione ");
                  scelta = input.nextInt();
                  //invio la scelta al server
                  //out.writeByte(scelta);
                  out.write(scelta);
  
                  //Inserisco i numeri e li spedisco al server
                  System.out.println("Inserisci primo numero.");
                  double x = input.nextDouble();
                  out.writeDouble(x);
  
                  System.out.println("Inserisci secondo numero.");
                  double y = input.nextDouble();
                  out.writeDouble(y);
  
              //}
              //while(scelta != 0);
          }catch (IOException e){
  
          }
  
  
      }
  
      public static void main(String args[]){
          Client myClient = new Client();
          myClient.connetti();
          myClient.comunica();
  
  
      }
  }
  </code></pre>