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

4

1 回答 1

0

引用重复的帖子

有几个可能的原因。

另一端故意重置连接,我不会在这里记录。应用软件很少这样做,而且通常是不正确的,但对于商业软件来说并不陌生。

更常见的情况是,写入连接的另一端已经正常关闭。换句话说,应用程序协议错误。

在 Windows 中,“软件导致连接中止”与“连接重置”不同,是由您端发送的网络问题引起的。有一篇关于此的 Microsoft 知识库文章。

于 2013-08-24T16:25:43.503 回答