同时将多个客户端连接到java服务器时发生套接字写入错误

同时将多个客户端连接到java服务器时发生套接字写入错误,java,sockets,serversocket,Java,Sockets,Serversocket,我正在创建一个桌面应用程序,其中多个客户端必须使用它们之间的套接字连接连接到服务器。我成功地连接了它们,但当我同时将多个客户端连接到服务器时,出现了问题,服务器出现错误“套接字写入错误” 我的代码在下面,请给我一个答案 public class SocketConnection implements Runnable { // password of oracle database ServerSocket serverSocket = null; Socket soc

我正在创建一个桌面应用程序,其中多个客户端必须使用它们之间的套接字连接连接到服务器。我成功地连接了它们,但当我同时将多个客户端连接到服务器时,出现了问题,服务器出现错误“套接字写入错误” 我的代码在下面,请给我一个答案

public class SocketConnection implements Runnable {
    // password of oracle database

    ServerSocket serverSocket = null;
    Socket socket = null;
    DataInputStream dataInputStream = null;
    DataOutputStream dataOutputStream = null;
    Socket clientSocket = null;
    DBConnection dbConnection;


    public SocketConnection() {
        // TODO Auto-generated constructor stub

        dbConnection = new DBConnection();

        if (con != null) {

            serverSocket = dbConnection.createSocket();

            if (serverSocket != null) {

                System.out.println("Server Started. Looking for the connections.");
                System.out.println("Listening Port:8888.......");
            }



            Thread t = new Thread(this);
            t.start();
        }

    }


    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            try {
                clientSocket = serverSocket.accept();
                System.out.println("Connection Accepted");
                Connect m_connect = new Connect(clientSocket);

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

        }

    }

    public class Connect implements Runnable {
        Socket clientSocket = null;
        Thread t = null;
        private ResultSet res1;
        private ResultSet res2;
        Statement stmt;
        private File mkFolder;

        public Connect(Socket clientSocke) {
            // TODO Auto-generated constructor stub
            this.clientSocket = clientSocke;

            try {

                stmt = con.createStatement();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            t = new Thread(this);
            t.start();
        }

        @Override
        public void run() {

            try {
                dataInputStream = new DataInputStream(
                        clientSocket.getInputStream());
                dataOutputStream = new DataOutputStream(
                        clientSocket.getOutputStream());
                System.out.println("Connection established::"
                        + clientSocket.getInetAddress());
                String pass = dataInputStream.readUTF();
                System.out.println(pass);
                if (pass.equals("1")) {
                    //here is read n write operation

                } else if (pass.equals("3")) {

                    //here is read n write operation

                } else if (pass.equals("2")) {

                    //here is read n write operation

                } else if (pass.equals("4")) {
                    //here is read n write operation

                } else if (pass.equals("5")) {
                    //here is read n write operation

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataInputStream != null) {
                    try {
                        dataInputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }




    }

}

你为什么用这种低级的方式做这件事?选择一个记录良好的API并通过它进行通信


您是否已经阅读过这样的文章:

您知道哪一行抛出了该错误吗?我在读写操作中遇到了错误。代码中的哪个读写操作抛出了该错误?很明显,这是一个读/写操作,因为它在错误消息的标题中。。java.net.SocketException:软件导致的连接中止:java.net.SocketOutputStream.socketWrite0(本机方法)处的套接字写入错误,java.net.SocketOutputStream.socketWrite(未知源)处的java.net.SocketOutputStream.write(未知源)处的java.io.DataOutputStream.write(未知源)处的java.io.DataOutputStream.writef处的套接字写入(未知源)连接Oracle$SocketConnection$Connect.run(ConnectOracle.java:529)的java.io.DataOutputStream.writeUTF(未知源)n第529行是..DataOutputStream.writeUTF(res2.getString(t));in pass=1;我不知道..如何实现这一点我很抱歉,但我不能教你开发协议。为什么你不使用RMI或其他?然后你可以发送愚蠢的简单POJO来表示你的命令并返回一些数据。但是我永远不会实现这一点,不管你尝试什么,由我自己来考虑。!!但我的问题现在已经解决了我也试着去学。。