How do I make a socket connection to a server?
Author: Deron Eriksson
Description: This Java tutorial describes how to make a socket connection to a server.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4


Page: < 1 2

(Continued from page 1)

To test, first let's start up TestServerSocket in EclipseSW. It starts up and waits for connections on port 81.

Console output

Next, let's start the TestClientSocket class. It makes a socket connection to the server running on port 81. It display's the server's question ("What's your name?"). I enter 'Eunice', even though, believe it or not, that's not really my name. The server says "Hello, Eunice", which the client displays. The server then asks, again, "What's your name?"

Console output

I respond "Gertrude", and the server says, "Hello, Gertrude". Once again, it asks my name.

Console output

This time, I typed 'exit' in the client, which ended the client application. Back in the console output of the server, we can see that the server has received the input "Eunice", "Gertrude", and "exit".

Console output

As we have seen, it's very easy to make a socket connection to a server using the JavaSW Socket class. Reading from and writing to the socket is very easy, since we can use the socket's input stream and output stream for these purposes.

Page: < 1 2