读取时重置连接()

时间:2016-09-25 17:29:00

标签: java sockets tcp

我正在尝试基于Web套接字技术实现服务器和客户端,但是当我运行TCPDataServer类时,我收到以下错误:

java.io.IOException: connection reset
at java.io.DataInputStream.read(Unknown Source)
at org.server.ClientWorker.run(TCPDataServer.java:67)
at java.lang.Thread.run(Unknown Source)

我除了获得以下输出Hello, I am Alex!

TCPDataClient

package org.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPDataClient {

    public static void main(String[] args) {
        try {
            TCPDataClient obj = new TCPDataClient();
            Socket obj_client = new Socket(InetAddress.getByName("127.0.1"), 1000);
            DataInputStream din = new DataInputStream(obj_client.getInputStream());
            DataOutputStream dout = new DataOutputStream(obj_client.getOutputStream());
            byte[] buffer = obj.createDataPacket("Hello, I am Alex!".getBytes("UTF8"));
            dout.write(buffer);

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private byte[] createDataPacket(byte[] data) {

        byte[] packet = null;
        try {
            byte[] initialize = new byte[1];
            initialize[0] = 2;
            byte[] separator = new byte[1];
            separator[0] = 4;
            byte[] data_length = String.valueOf(data.length).getBytes("UIF8");
            packet = new byte[initialize.length + separator.length + data_length.length + data.length];

            System.arraycopy(initialize, 0, packet, 0, initialize.length);
            System.arraycopy(data_length, 0, packet, initialize.length, data_length.length);
            System.arraycopy(separator, 0, packet, initialize.length + data_length.length, separator.length);
            System.arraycopy(data, 0, packet, initialize.length + data_length.length + separator.length, data.length);


        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return packet;

    }

}

TCPDataServer

package org.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.logging.Level;

import com.sun.istack.internal.logging.Logger;

public class TCPDataServer {

    public static void main(String[] args) {

        try {
            ServerSocket server_socket = new ServerSocket(1000);
            while (true) {
                new Thread(new ClientWorker(server_socket.accept())).start();
            }
        } catch (IOException e) {
              System.err.println("Error");
              System.err.println(e);
              System.err.println("\n2");
              System.err.println(e.getMessage());
              System.err.println("\n3");
              System.err.println(e.getLocalizedMessage());
              System.err.println("\n4");
              System.err.println(e.getCause());
              System.err.println("\n5");
              System.err.println(Arrays.toString(e.getStackTrace()));
              System.err.println("\n6");
              e.printStackTrace();
        }

    }

}

class ClientWorker implements Runnable {
    private Socket target_socket;
    private DataInputStream din;
    private DataOutputStream dout;

    public ClientWorker(Socket recv_scket) {

        try {
            target_socket = recv_scket;
            din = new DataInputStream(target_socket.getInputStream());
            dout = new DataOutputStream(target_socket.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void run() {
        while (true) {
            byte[] initilize = new byte[1];
            try {
                din.read(initilize, 0, initilize.length);
                if (initilize[0] == 2) {
                    //This is line 67.
                    System.out.println(new String(readStream()));

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    private byte[] readStream() {
        byte[] data_buff = null;

        try {
            int b = 0;
            String buff_length = "";
            while ((b = din.read()) != 4) {
                buff_length += (char) b;
            }
            int data_length = Integer.parseInt(buff_length);
            data_buff = new byte[Integer.parseInt(buff_length)];
            int byte_read = 0;
            int byte_offset = 0;
            while (byte_offset < data_length) {
                byte_read = din.read(data_buff, byte_read, data_length - byte_offset);
                byte_offset += byte_read;

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return data_buff;

    }

}

1 个答案:

答案 0 :(得分:-1)

您没有关闭套接字,因此您的操作系统会在JVM退出时重置它们。