0

I'm trying to learn Java Network Programming but I'm running into some roadblocks. I've written a server and a client already but every time I try to connect them, I instantly get a connection closed error. Then, I tried to edit it but now I get a connection refused error. Giving up on that, I decided to work on a very simple server to test out the basics of Sockets and ServerSockets. Doing so, I've come up with these two classes:

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        System.out.println("hey there");
        ServerSocket server = new ServerSocket(50010);
        Socket socket = server.accept();
        System.out.println("Connection at " + socket);

        InputStream in = socket.getInputStream();
        int c = 0;
        while ((c = in.read()) != -1) {
            System.out.print((char)c);
        }


        OutputStream out = socket.getOutputStream();
        for (byte b : (new String("Thanks for connecting!")).getBytes()) {
            out.write(b);
            out.flush();
        }

        in.close();
        out.close();
        socket.close();
        server.close();
    }
}

and

import java.net.Socket;
import java.io.*;

public class SimpleClient {
    public static void main(String[] args) throws Exception {
        System.out.println("Attempting connection");
        Socket s = new Socket("130.49.89.208", 50010);
        System.out.println("Cool");

        OutputStream out = s.getOutputStream();
        for (byte b : (new String("Hey server\r\nThis message is from the client\r\nEnd of message\r\n")).getBytes()) {
            out.write(b);
            out.flush();
        }

        InputStream in = s.getInputStream();
        int c = 0;
        System.out.println("this message will print");
        while ((c = in.read()) != -1) {
            System.out.print((char)c);
            System.out.println("this does not print");
        }

        out.close();
        in.close();
        s.close();
    }
}

The server receives the client's message perfectly fine, but then when it is the server's turn to write to the client, everything just blocks.

Server's output:

-java SimpleServer 
----hey there 
----Connection at Socket[addr=/130.49.89.208,port=59136,localport=50010]
----Hey server
----This message is from the client
----End of message

Client's Output:

-java SimpleClient
----Attempting connection
----Cool
----this message will print

The client and the server both run on my laptop on an ethernet connection to a university's internet connection, if that helps.

2 Answers 2

1

According to the Javadocs, the InputStream.read() is described as:

If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

In your case, the only possibility to break the while-loop is when the client closes the connection, thereby resulting in end-of-stream.

This is expected as per what you've coded!

Sign up to request clarification or add additional context in comments.

Comments

0

Your server code is getting stuck here and thus never writing back to the client

   while ((c = in.read()) != -1) {
        System.out.print((char)c);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.