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

Shaggy
  • 5,422
  • 28
  • 98
  • 163
  • Did you close the connection at the server side? Look at possible causes for the exception: http://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset – jpe Aug 24 '13 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 ? – Shaggy Aug 24 '13 at 16:24
  • My Server doesnt close the connection with client – Shaggy Aug 24 '13 at 16:24
  • Try to use a BufferedInputStream instance and use that in the loop condition. – jpe Aug 24 '13 at 16:30
  • just to be sure if a more basic socket program worked for you ? I mean is this your first socket program ? – Srinath Ganesh Aug 24 '13 at 17:10
  • Your *client* has probably closed the connection while you were still writing to it. See my answer to the duplicate post quoted in Ganesh's answer here. – user207421 Aug 25 '13 at 00:01

1 Answers1

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.

Community
  • 1
  • 1
Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60