简体   繁体   中英

Server Socket Isn't Sending Data Back in Java

So, I just learned how to make socket s and all that good stuff in Java, and so my first try got me a message from the client, and then the client crashing. What was supposed to happen was get a message from the client, if that message is equal to this, then send data back. However, the if function for if the message was correct wasn't firing, even though the message was correct.

Even when I remove the if function to check if the string was right or not, the program still freezes up. And by the way, my server is a console application, and my client is a SWT application.

Here's the server code with the removed if function:

try {
    System.out.println("Waiting for a connection...");
    // Start a server
    ServerSocket server = new ServerSocket(3211);
    // Listen for anyone at that port
    Socket socket = server.accept();
    System.out.println("The client has connected!");
    // Get the data being sent in
    DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream());
    // Turn that into UTF-8
    String data = inputStream.readUTF();
    System.out.println("Received " + data);
    ouputStream.writeUTF("Great!");
    System.out.println("Awesome!");
    socket.close();
    inputStream.close();
    ouputStream.close;
    server.close();
    System.out.println("Socket closed\n-----------------------------");
}
catch(IOException e) {
    System.out.println(e);
}

And the client (which is fired when a button gets pressed):

try {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnecting to the server...");
    Socket socket = new Socket("192.168.1.206", 3211);
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnected to the server!");
    DataInputStream input = new DataInputStream(System.in);
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    output.writeUTF("sweet");
    String data = input.readUTF();
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nSERVER: " + data);
    input.close();
    output.close();
    socket.close();

}
catch (IOException er) {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\n" + er);
}

As soon as I press the button to try and start the connection (with the server already running), the client instantly freezes. It doesn't even send any of the "connecting to server" kind of stuff.

Any idea what's going wrong, and how to fix this?

Your client is reading from System.in . It should be reading from the socket input stream.

NB You only need to close the outermost output stream of a socket. That flushes it if necessary and closes the input stream and the socket. You're presently not only closing more than necessary but also in the wrong order,

Your socket is unable to send data because you did not called .flush() method on your outputstream reference. Use this one and you don't have to write flush() and close() method explicitely on streams

Server Code

System.out.println("Waiting for a connection...");
// Start a server
try (ServerSocket server = new ServerSocket(3211)) {
    // Listen for anyone at that port
    try (Socket socket = server.accept()) {
        System.out.println("The client has connected!");
        // Get the data being sent in
        try (DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()))) {
            try (DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream())) {
                // Turn that into UTF-8
                String data = inputStream.readUTF();
                System.out.println("Received " + data);
                ouputStream.writeUTF("Great!");
                System.out.println("Awesome!");
           }
        }
    }
    System.out.println("Socket closed\n-----------------------------");
} catch (IOException e) {
    System.out.println(e);
}

Client Code

try {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnecting to  the server...");
    try (Socket socket = new Socket("127.0.0.1", 3211)) {
        allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnected to the server!");
        try (DataInputStream input = new DataInputStream(socket.getInputStream())) {
            try (DataOutputStream output = new DataOutputStream(socket.getOutputStream())) {
                output.writeUTF("sweet");
            }
            String data = input.readUTF();
            System.out.println(String.format("data received from server '%s':\n", data));
            allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nSERVER: " +  data);
        }
    }
} catch (IOException er) {
    allMessagesTextBox.setText(allMessagesTextBox.getText() + "\n" + er);
}

Output on Server

Waiting for a connection...
The client has connected!
Received sweet
Awesome!
Socket closed
-----------------------------

Output on Client

data received from server 'Great!':

Now moving to problem in your code.

See the client side code you have written DataInputStream input = new DataInputStream(System.in); instead of DataInputStream input = new DataInputStream(socket.getInputStream())

which causes the failure in receiving message from server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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