简体   繁体   English

当我同时将多个客户端连接到Java服务器时,套接字写入错误

[英]Socket write error when i connect multiple client to java server simultaneously

I am creating a desktop application in which multiple clients have to connect to the server using socket connection between them. 我正在创建一个桌面应用程序,其中多个客户端必须使用它们之间的套接字连接来连接到服务器。 I successfully connected them but the problem occurs when i connect multiple client simultaneously to the server, sever got an error " socket write error " my code is below plz suggest me an answer.. 我成功连接了它们,但是当我同时将多个客户端连接到服务器时,发生了问题,服务器出现错误“ 套接字写入错误 ”,我的代码低于plz,建议我给出答案。

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();
                    }
                }




    }

}

Why you are doing this in that low-level way? 为什么您以这种低级方式进行此操作? Choose an well documented API and communicate through it! 选择一个文档齐全的API并通过它进行通信!

Did you already read articles like this: Multithread client server chat on sockets. 您是否已经阅读过这样的文章: 多线程客户端服务器在套接字上聊天。 Load test. 负载测试。 recv failed 接收失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM