Reading and writing to sockets, Java

I have a little problem reading and writing to Sockets in my Server / Client Java application. The server has a connection to the database. I want to send an "Employee" object consisting of user data (first name, last name, password) to the server, and then the server looks up the database about this user and sends it to the client information - positive (1) or negative (-1).

First, when I want to send an Employee object, I have: "java.net.SocketException: Software caused connection aborted: socket write error" I have my firewall disabled.

Second, when I want to send and receive only int via the writeInt - readInt method for the test, I cannot read anything on the server.

What's the problem? Please, help.

Code Server:

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

      

Client code:

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

                        }
                    }
                }
            }
        });
    }       

      

+3


source to share


1 answer


  • add ex.printStackTrace () to see what's going on in your

    catch(IOException | ClassNotFoundException | SQLException ex)

  • Server side, in your class ClientCommunication

    : it seems like you are closing the socket before entering the loop while

    . So the socket is already closed and cannot send / receive messages. You don't have to call incoming.close()

    there, but at the end of your method run()

    .



+2


source







All Articles