title: Java Client/Server send or get int or string type
body: <p>i'm trying to create a simple java program where the client and the server will be both able to send and get strings and ints. I can do it work with only strings or only ints but i find it hard to combine them without making a mess. If someone can tell me the easiest way to do it i would be thankfull. Here is what i've done till now but it gives the wrong number back.</p>
  
  <p>MyServer.java</p>
  
  <pre><code>package myserver;
  
  import java.io.DataInputStream;
  import java.util.Random;
  import java.io.*;
  import java.net.*;
  
  
  public class MyServer 
  {
     public static void main(String argv[]) throws Exception
        {
              String clientSentence;
              String answer;
              ServerSocket welcomeSocket = new ServerSocket(6789);
              Socket connectionSocket = welcomeSocket.accept();
              BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
              DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
              clientSentence = inFromClient.readLine();
              Random rand = new Random();
              int  n = rand.nextInt(100) + 1;
              System.out.println("Received: " + clientSentence);
              answer = "Hey " + clientSentence + " guess a number from 1 to 100!" + '\n';
              outToClient.writeBytes(answer);
               DataInputStream integerFromClient = new DataInputStream(connectionSocket.getInputStream());
              int k = integerFromClient.readInt();
              System.out.println(k);
  
        }
  } 
  </code></pre>
  
  <p>MyClient.java</p>
  
  <pre><code>package myserver;
  
  import java.io.*;
  import java.net.*;
  
  class MyClient
  {
   public static void main(String argv[]) throws Exception
   {
    String sentence;
    String modifiedSentence;
    BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
    Socket clientSocket = new Socket("localhost", 6789);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    sentence = inFromUser.readLine();
    outToServer.writeBytes(sentence + '\n');
    modifiedSentence = inFromServer.readLine();
    System.out.println("SERVER: " + modifiedSentence);
    int k = inFromUser.read();
    outToServer.writeInt(k);
    clientSocket.close();
   }
  }
  </code></pre>