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
(Continued from page 1)
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?"
I respond "Gertrude", and the server says, "Hello, Gertrude". Once again, it asks my name.
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".
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.