0

I am quite a newbie to Java. Please excuse me if you find this as a very basic question.There are many answers available already in stack overflow about this and I went through almost all the possible helps i can get in Stack overflow and also in some other forums. Unfortunately none of them helped me.

I have client/server program in which the client send a string to server and server just attaches another string to the string sent by client and sends it back to the client.

Server program looks like this.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class server {

    public static void main(String[] args) {

        try
        {
            ServerSocket server = new ServerSocket(7300);
            Socket s = server.accept();
            DataInputStream inp = new DataInputStream(s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            String str =inp.readUTF();
            str = str+"  buddy!";
            out.writeUTF(str);



        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

}

Client looks like This.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.*;

public class client {

    public static void main(String[] args) {
        try
        {
            Socket s = new Socket("192.168.1.3",7300);
            DataInputStream inp = new DataInputStream(s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());

            out.writeUTF("hi");
            System.out.println(inp.readUTF());
            Thread.sleep(2000);
            out.writeUTF("hello");
            System.out.println(inp.readUTF());


        }

        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

Everything works fine while client writes "hi" and when client starts sending "hello" i am getting Connection reset error. I am not getting what mistake am i doing please help me in resolving this.

The output with the error i am getting looks like this.

hi  buddy!
java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeUTF(Unknown Source)
    at java.io.DataOutputStream.writeUTF(Unknown Source)
    at sokry.client.main(client.java:18)
Chiyaan Suraj
  • 1,021
  • 2
  • 13
  • 27

2 Answers2

1

In your server example, readUTF is only called once on the DataInputStream, even though the client wrote to the DataOutputStream twice. Thus, simply adding

str = inp.readUTF();
str = str + "  buddy!";
out.writeUTF(str);

to your server example, after the last out.writeUTF(str), will solve your problem.

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
1

do comment on following line of your client.java file and try.it will work

Thread.sleep(2000); `//out.writeUTF("hello");; //System.out.println(inp.readUTF()); because when you are sending "hi" from client to server and server gives reply then it finished it work and it stop connection but in client.java you sending another request to server but server is at rest.

you should start server until client finish it work..

hope it will wait