Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Writing to Sockets, Java

Tags:

java

sockets

I have a little problem with reading and writing to Sockets in my Server/Client Java application. Server have connection to database. I want to send an object "Employee" consist User Data (Name, Surname, Password) to Server, then Server look up to database about this user and resend to Client information - positive (1) or negative (-1).

First, when I want to send an object Employee, I've got : "java.net.SocketException: Software caused connection abort: socket write error" I have my Firewall turned off.

Second, when I want to send and receive just int through writeInt - readInt method for test, I can't to read anything on 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();                
        }
    }       
}

Code Client:

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

                        }
                    }
                }
            }
        });
    }       
like image 927
Andrew Avatar asked Dec 06 '25 08:12

Andrew


1 Answers

  1. add an ex.printStackTrace() to see what is happening in your

    catch(IOException | ClassNotFoundException | SQLException ex)

  2. Server side, on your ClientCommunication class: it seems you are closing the socket before entering the while loop. So the socket is already closed and cannot send/receive messages. You should NOT call incoming.close() there, but at the end of your run() method.

like image 66
Kostas Chalkias Avatar answered Dec 07 '25 20:12

Kostas Chalkias