Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取和写入套接字,Java_Java_Sockets - Fatal编程技术网

读取和写入套接字,Java

读取和写入套接字,Java,java,sockets,Java,Sockets,我在服务器/客户机Java应用程序中读写套接字时遇到了一个小问题。服务器已连接到数据库。我想向服务器发送一个对象“Employee”包括用户数据(姓名、姓氏、密码),然后服务器查找有关该用户的数据库并重新发送到客户端信息-正(1)或负(-1) 首先,当我想发送对象Employee时,我有: “java.net.SocketException:软件导致的连接中止:套接字写入错误” 我把防火墙关掉了 第二,当我想通过writeInt-readInt方法发送和接收int进行测试时,我无法在服务器上读取

我在服务器/客户机Java应用程序中读写套接字时遇到了一个小问题。服务器已连接到数据库。我想向服务器发送一个对象“Employee”包括用户数据(姓名、姓氏、密码),然后服务器查找有关该用户的数据库并重新发送到客户端信息-正(1)或负(-1)

首先,当我想发送对象Employee时,我有: “java.net.SocketException:软件导致的连接中止:套接字写入错误” 我把防火墙关掉了

第二,当我想通过writeInt-readInt方法发送和接收int进行测试时,我无法在服务器上读取任何内容

有什么问题吗?请帮忙

代码服务器:

class ClientCommunication implements Runnable {
    private Socket incoming;

    public ClientCommunication(Socket clientSocket) {
        incoming = clientSocket;

    }

    public void run() {
        try {
            synchronized (this) {
                try {                   
                    serverObjectOutput = new ObjectOutputStream(
                            incoming.getOutputStream());
                    serverObjectInput = new ObjectInputStream(
                            incoming.getInputStream()); 
                } finally {
                    incoming.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        synchronized(this) {
            while (true) {
                try{                        
                    int operation = serverObjectInput.readInt();                        

                    switch(operation) {
                    case 1:
                            Employee employee = (Employee) serverObjectInput.readObject();
                            String SelectUserDataSQL = "SELECT COUNT(*) AS COUNT FROM pracownik where Imie = ? AND Nazwisko = ? AND Haslo = ?";
                            PreparedStatement CheckEmployeeLogin;
                            CheckEmployeeLogin = conn.prepareStatement(SelectUserDataSQL);

                            CheckEmployeeLogin.setString(1, employee.getFirstName());
                            CheckEmployeeLogin.setString(2, employee.getLastName());
                            CheckEmployeeLogin.setString(3, new String(employee.getPassword()));                    

                            ResultSet resultSQL = CheckEmployeeLogin.executeQuery();
                            if (resultSQL.next()) 
                                if (resultSQL.getInt("COUNT") == 0)
                                    serverObjectOutput.writeInt(1);
                                else serverObjectOutput.writeInt(-1);
                            break;
                }
            } catch(IOException | ClassNotFoundException | SQLException ex)                 
            {                   
            }
          }
        }
    }
}

class ServerStart implements Runnable {
    private int portNumber;

    public ServerStart(int portNumber) {
        this.portNumber = portNumber;
    }

    public void run() {


        try {
            conn = getConnection();
            stat = conn.createStatement();

        } catch (SQLException e1) {             
            e1.printStackTrace();
        } catch (IOException e1) {              
            e1.printStackTrace();
        } catch (InterruptedException e) {              
            e.printStackTrace();
        }


        try {
            serverSocket = new ServerSocket(portNumber);                

        } catch (IOException e) {
            e.printStackTrace();                
        }


        try {
            while (true) {
                Socket incoming = serverSocket.accept();

                clientSockets.add(incoming);

                Runnable r = new ClientCommunication(incoming);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();                
        }
    }       
}
代码客户端:

public void actionPerformed(ActionEvent e) {
                if (isConnected == false) {
                    String ServerIP = ip.getText().trim();

                    int ServerPort = Integer
                            .parseInt(port.getText().trim());


                    try {
                        ClientSocket = new Socket(ServerIP, ServerPort);                        

                        clientObjectInput = new ObjectInputStream(
                                ClientSocket.getInputStream());
                        clientObjectOutput = new ObjectOutputStream(
                                ClientSocket.getOutputStream());

                        isConnected = true;
                    } catch (IOException ex) {                          
                    }
                    synchronized (this) {
                        try {                               
                            ClientLoginFrame login = new ClientLoginFrame();

                            Employee employee = login.getEmployee();                                                                

                            clientObjectOutput.writeObject(employee);                               
                            int result = clientObjectInput.readInt();

                            if(result == 1)
                            {       
                                  // DO SOMETHING
                            }
                            else { 
                                ClientSocket.close();                                   
                            }                           
                        } catch (IOException ex) {
                            ex.printStackTrace();

                        }
                    }
                }
            }
        });
    }       
  • 添加一个ex.printStackTrace()以查看您的

    catch(IOException | ClassNotFoundException | SQLException ex)

  • 服务器端,在
    ClientCommunication
    类中:似乎在进入
    while
    循环之前关闭了套接字。因此,套接字已关闭,无法发送/接收消息。您不应该在那里调用
    incoming.close()
    ,而应该在
    run()
    方法的末尾调用


  • 您好,Andrew,您在这里提出了两个问题:使用对象进行客户机-服务器通信时获得SocketException,以及使用readInt时“无法读取任何内容”。您是否能够以某种方式将代码示例分解,直到只剩下有问题的gode,并准确地写出在使用“readInt”/“writeInt”时遇到的错误?我在使用writeInt()和readInt()时没有任何错误,但如果我想进行测试,请在客户机上编写(1),在服务器上生成readInt(),然后显示收到的值,如console.append(“)+结果),我在控制台上什么都没有。我稍微修改了一段代码,希望它更简短易读。