读取和写入套接字,Java

时间:2014-12-26 23:04:52

标签: java sockets

我在服务器/客户端Java应用程序中读取和写入套接字时遇到了一些问题。服务器已连接到数据库。我想发送一个对象"员工"包括服务器的用户数据(名称,姓氏,密码),然后服务器查找有关此用户的数据库并重新发送到客户端信息 - 正(1)或负(-1)。

首先,当我想发送一个对象Employee时,我得到了: " java.net.SocketException:软件导致连接中止:套接字写入错误" 我关闭了防火墙。

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

问题是什么?请帮忙。

代码服务器:

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

                        }
                    }
                }
            }
        });
    }       

1 个答案:

答案 0 :(得分:2)

  1. 添加ex.printStackTrace()以查看

    中发生的情况

    catch(IOException | ClassNotFoundException | SQLException ex)

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