0

I am developing Client-Server App using Java Socket. My Application Has Server which will listen on Port

  • Client will connect to that port
  • Receive Data From Client
  • Send Data To Client

Part of My Code

public void run() {
    System.out.println("Got a client !");
    try {
        // Get Data From Client
        int red = -1;
        byte[] buffer = new byte[5 * 1024]; // a read buffer of 5KiB
        byte[] redData;
        StringBuilder clientData = new StringBuilder();
        String redDataText;
        while ((red = clientSocket.getInputStream().read(buffer)) > -1) {

/* Get Data From Client Here Code Hidden */

            System.out.println("Data From Client :"
                    + clientData.toString());

            OutputStream out = clientSocket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(out);

            String sDataToClient = "TEST DATA TO SEND IN BYTE ARRAY";

            byte[] b = sDataToClient.getBytes("UTF-8");

            byte[] bClientSend = new byte[b.length + 2];

            bClientSend[0] = (byte) 1;
            bClientSend[1] = (byte) 79;

            System.arraycopy(b, 0, bClientSend, 2, b.length);

            dos.write(bClientSend);
            System.out.println(Arrays.toString(bClientSend));

        }
        clientSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I Get java.net.SocketException: Connection reset after Data SENT to Client at following line

while ((red = clientSocket.getInputStream().read(buffer)) > -1) {

I am able to see Array Contents of System.out.println(Arrays.toString(bClientSend)); Then error occurs

6
  • Did you close the connection at the server side? Look at possible causes for the exception: stackoverflow.com/questions/62929/… Commented Aug 24, 2013 at 16:22
  • visited link before... is it because after Sending Data to client My Client Closes connection with server and hence i get error in while loop ? Commented Aug 24, 2013 at 16:24
  • My Server doesnt close the connection with client Commented Aug 24, 2013 at 16:24
  • Try to use a BufferedInputStream instance and use that in the loop condition. Commented Aug 24, 2013 at 16:30
  • just to be sure if a more basic socket program worked for you ? I mean is this your first socket program ? Commented Aug 24, 2013 at 17:10

1 Answer 1

0

Quoting a duplicate post

There are several possible causes.

The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.

More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.

In Windows, 'software caused connection abort', which is not the same as 'connection reset', is caused by network problems sending from your end. There's a Microsoft knowledge base article about this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.