Socket Communication in Client/Server Relationship
When writing java code for a Client or Server, using Sockets are the most efficient way to communicate between the client and server. Sockets are flexible, sufficient, easy to implement, and they do not use a lot of network traffic.
There are many reasons why your client/server may not be connecting to each other:
How to code this in Java:
Published at DZone with permission of its author, Joseph Randolph.There are many reasons why your client/server may not be connecting to each other:
- Make sure you import “java.net”
- Make sure the socket is bound to a port number
- Make sure you have the correct IP address
- Make sure the socket for the server accepts the right port number
In Java, the client initiates the connection by instantiating a Socket. The Socket constructor usually takes in two parameters: TCP/IP address of the server and port number of the application. After the client instantiates the Socket, the client will instantiate DataInputStream and DataOutputStream. These high-level classes share the same Socket. The client will wait for the server to accept the connection.
How to code this in Java:
Socket s = new Socket(serverAddress, 1234); //connect to server appThe Server application uses a “ServerSocket” class and the constructor takes the port number as a parameter. If you are an application programmer on the server, you would need to get a port number from the administrator. When you call accept method, the server application waits for the client to call the port number. After the ServerSocket accepts the client, it negotiates with the client to switch to an available port so that the main port is still open for other clients to connect to the server. The server will wait for the client to say something and each client will spend its time waiting in a readUTF method.
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(question);
String answer = dis.readUTF(); //wait for reply
How to code this in Java:
Socket s = ss.accept(); // wait for a client connection
DataInputStream dis = new DataInputStream (s.getInputStream());
DataOutputStream dos = new DataOutputStream (s.getOutputStream());
String question = dis.readUTF();
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Thomas Eichberger replied on Sat, 2010/05/22 - 6:21pm
I don't see a reason why this is published here. It's really only for absolute beginners who are on the 4th day of their Java class or so...
Or maybe this is an article from Dec. 1995.
Alex(JAlexoid) ... replied on Sun, 2010/05/23 - 6:19pm
in response to:
Thomas Eichberger
I have to agree with Thomas.
The main problem with the article though is that DataInputStream and DataOutputStream classes are not common in use(I believe it's not in encouraged for use since Java 1.4). The readUTF method does not follow the standard UTF-8 format.
Sevgi Tas replied on Tue, 2010/05/25 - 2:32pm