Mike Haye
Mike Haye

Reputation: 793

Connection to ServerSocket from local machine

I am fairly new to java and I am currently experimenting with sockets and buffers.

What I wanted to try was just to instantiate a connection from one local java app and another. I am using ServerSocket and Socket.

The server app has a thread that listens for connections:

    public void run() {

    try{

        ServerSocket serverSock = new ServerSocket(62666);

        while(doRun){

            Socket sock = serverSock.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));

            InfoReader.gui.writeToTextArea(reader.readLine() + "\n");



            reader.close();

        }
    }catch(IOException e){
        e.printStackTrace();
    }

}

The "client" simply sends a string to the server (well it should, but I can't make it work):

try{
        Socket sock = new Socket("127.0.0.1",62666); 
        PrintWriter writer = new PrintWriter(sock.getOutputStream());
        writer.print("Connection works!");
    }catch(IOException e){
        e.printStackTrace();
    }

I am sure that the port is open and forwarded to the local machine already. I've checked on http://canyouseeme.org/.

I've also tried using my external IP address as the IP of the socket in the client. It did not work either.

Any help appreciated :).

Mike.

Upvotes: 1

Views: 1604

Answers (1)

SJuan76
SJuan76

Reputation: 24895

Ok, then as an answer so you can close the question ;-)

Add a flush() and a close() to the Writer.

Hope that helps. :-)

Upvotes: 1

Related Questions