Java beginner (Client-Server) : Sending multiple Integers to a socket
<p>i have a very simple assignment in which i am supposed to send 2 integers into a socket, which sends their sum back to the &quot;client&quot;.</p>
<p>this is my client:</p>
<pre><code>int a,b,sum;
    try
    {
        Socket Server_info = new Socket (&quot;localhost&quot;, 15000);
        BufferedReader FromServer = new BufferedReader (new InputStreamReader(Server_info.getInputStream()));
        DataOutputStream ToServer = new DataOutputStream(Server_info.getOutputStream());
        while (true)
        {
            System.out.println(&quot;Type in '0' at any point to quit&quot;);
            System.out.println(&quot;Please input a number&quot;);
            a = User_in.nextInt();
            ToServer.writeInt(a);
            System.out.println(&quot;Please input a second number&quot;);
            b = User_in.nextInt();
            ToServer.writeInt(b);
            sum = FromServer.read();
            System.out.println(&quot;the sum of &quot;  +a+ &quot; and &quot; +b+ &quot; is: &quot; +sum );
            if (a==0 || b==0)
                break;
        }
</code></pre>
<p>this is my socket handler:</p>
<pre><code>int num1=0 ,num2=0, sum;
    try
    {
        BufferedReader InFromClient = new BufferedReader (new InputStreamReader(soc_1.getInputStream()));
        DataOutputStream OutToClient = new DataOutputStream(soc_1.getOutputStream());
        while (true)
        {
            num1 = InFromClient.read();
            num2 = InFromClient.read();
            sum = num1 + num2 ;
            OutToClient.writeInt(sum);
        }
    }
    catch (Exception E){}
</code></pre>
<p>After the first Integer input upon running the client i get this:</p>
<blockquote>
<p>Type in '0' at any point to quit</p>
<p>Please input a number</p>
<p>5</p>
<p>Connection reset by peer: socket write error</p>
</blockquote>
<p>i think the problem lays at the socket receiving side, i must be doing something wrong. any suggestions?</p>