article

Socket Programming Using Java

Email
Submitted on: 1/4/2015 7:49:00 AM
By: Karthik Veeramani (from psc cd)  
Level: Beginner
User Rating: By 80 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 806
 
     This tutorial will make you understand what sockets are, and will help you to start coding simple networking applications using Java.

 
				

SOCKET PROGRAMMING USING JAVA

If you find this tutorial good enough, please vote for me (use the Your Vote! section at the end of this page)

So you want to start doing some network programming with Java... You've come to the right place. I'll introduce you to the interesting world of Java sockets. By the end of this tutorial, you should be able to understand what sockets are and how to build simple Java applications using sockets.

Whats a socket?

Don't tell me you've never chatted on those instant messengers like yahoo, msn and aol. But its OK if you never understood what goes on behind the scene; after all, thats what we are here for. Lets say you've installed one of those instant messengers on your computer. After you run it and enter your user name and password, the messenger tries to connect to its server (say, the yahoo server). What exactly does this 'connect' mean?

Every computer on a network has an IP address. This address is like your house address, something that identifies your computer uniquely, allowing others to communicate with your computer. I wont go much into IP addresses, but let me just tell you that an IP address looks something like this - 64.104.137.158 - a set of numbers separated with dots. However, some computers with rich owners will also choose to have a domain name in addition to this sick looking number, so that people can easily identify them. A domain name looks far more sane - like www.yahoo.com. There are some special computers on the Internet, whose sole purpose in life is to translate the domain name to IP address and vice versa.

Now, you know that many programs can run on the same computer, don't you? Lets say that, there are some 10 programs running on a certain computer. To add to the confusion, lets say all of them are waiting for other computers to contact them. Imagine it like this - 10 of you share a big office space and a single telephone - and all of you expect calls from your own clients. How will you handle this? Perhaps appoint one person who'll hand over the call to the right person. Possible, but an undeniable menace. This will mean, when one of you take the call, other clients will not be able to reach the rest of you. Besides, its a pain to have a person route the calls to the right people. You must have guessed what I'm heading at - if all those programs running on a single computer proudly ask their clients to contact them on a certain IP address, their clients are not going to be pleased. The idea is... having a separate IP address per program, right? WRONG. Thats out of question. Its like asking for a separate office for each of you. Wont separate phone numbers suffice? Yes. In networking parlance, we call these 'separate phone numbers' as ports. A port is just a simple number - each program running on the same computer can choose to have a unique port number to identify itself to the outside world. REMEMBER - these ports are not slots on your computer hardware - dont think you can find them if you try hard enough. They are just logical numbers. Now the point should be clear. We have an IP address that lets the other computers look for a certain computer on the network. And we have a port number that'll identify a certain program running on that computer. Understand that, two programs running on different computers CAN use the same port number. Two houses on different streets can have the same house number, can't they? So, finally, we are almost there - just to scare you a bit, lets derive a formula -

An IP address = uniquely identifies a computer on the network. A port number = uniquely identifies a program running on a computer.

Adding the above equations,

An IP address + A port number = _______

In other words, A _____ = uniquely identifies a program on the network

If you guessed it right, thanks, my effort didn't go waste. If you didn't, no problem, go back and read from the beginning, or google for a better tutorial. The ____ is... SOCKET!

To summarize, a socket is a combination of an IP address and a port. A socket address lets other computers on the network locate a certain program running on a certain computer. You may represent a socket address like 64.104.137.58:80, where 64.104.137.58 is the IP address and 80 is the port number.

How to program

Enough of theory talk. Lets get into some action now. We are going to write some very simple Java code, that'll demonstrate the use of sockets. Here is what is going to happen -

1) One Java program will try to connect to another Java program (which is desperately waiting for someone to contact it). Lets call the first program as Client, and the second as Server.
2) Once connected, the client program is going to accept whatever you type, and send it dutifully to the server program.
3) The server is going to send back the same text to the client, just to show that it is least interested in doing such an uninteresting thing.
4) The client, after getting back the same text from the server, is going to throw it on your face, showing you what the server thinks about you.

Ready? Lets get started. Note that, I wont be teaching you Java programming from scratch, I'll explain only the socket-related portions of the code.

Create 2 fresh Java programs and call them Server.java and Client.java. I'll paste the code below, but don't be scared, I'll explain.

Server.java
 
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] ar) {
 int port = 6666; // just a random port. make sure you enter something between 1025 and 65535.
 try {
 ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
 System.out.println("Waiting for a client...");
 Socket socket = ss.accept(); // make the server listen for a connection, and let you know when it gets one.
 System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
 System.out.println();
 // Get the input and output streams of the socket, so that you can receive and send data to the client.
 InputStream sin = socket.getInputStream();
 OutputStream sout = socket.getOutputStream();
 // Just converting them to different streams, so that string handling becomes easier.
 DataInputStream in = new DataInputStream(sin);
 DataOutputStream out = new DataOutputStream(sout);
 String line = null;
 while(true) {
 line = in.readUTF(); // wait for the client to send a line of text.
 System.out.println("The dumb client just sent me this line : " + line);
 System.out.println("I'm sending it back...");
 out.writeUTF(line); // send the same line back to the client.
 out.flush(); // flush the stream to ensure that the data reaches the other end.
 System.out.println("Waiting for the next line...");
 System.out.println();
 }
 } catch(Exception x) {
 x.printStackTrace();
 }
 }
 }
 
Client.java
 
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] ar) {
 int serverPort = 6666; // make sure you give the port number on which the server is listening.
 String address = "127.0.0.1"; // this is the IP address of the server program's computer. // the address given here means "the same computer as the client".
 try {
 InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
 System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
 Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
 System.out.println("Yes! I just got hold of the program.");
 // Get the input and output streams of the socket, so that you can receive and send data to the client.
 InputStream sin = socket.getInputStream();
 OutputStream sout = socket.getOutputStream();
 // Just converting them to different streams, so that string handling becomes easier.
 DataInputStream in = new DataInputStream(sin);
 DataOutputStream out = new DataOutputStream(sout);
 // Create a stream to read from the keyboard.
 BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
 String line = null;
 System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
 System.out.println();
 while(true) {
 line = keyboard.readLine(); // wait for the user to type in something and press enter.
 System.out.println("Sending this line to the server...");
 out.writeUTF(line); // send the above line to the server.
 out.flush(); // flush the stream to ensure that the data reaches the other end.
 line = in.readUTF(); // wait for the server to send a line of text.
 System.out.println("The server was very polite. It sent me this : " + line);
 System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
 System.out.println();
 }
 } catch(Exception x) {
 x.printStackTrace();
 }
 }
 }
 
Compile it with
 javac Server.java Client.java 
Open two command windows (DOS prompts). In one of those, enter
 java Server
and in the other,
 java Client

(in that order!)

Type something on the client window and press enter. Observe both windows and see what happens. Finally, press ctrl-C to kill the programs.

Explanation

Lets delve into the code now. You must have got some idea looking at the comments, still, lets try to analyze a few critical lines.

The server code has the lines

ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();

A ServerSocket class is slightly different from the Socket class. The Socket class is exactly what you think.. well, it represents a socket. The ServerSocket class is mainly for allowing a program to listen for connections from clients. You create it by assigning it a port number on which it can work on. Once created, you need to call its accept() method. This method will make the program listen on the given port for connections. It hangs there until it gets a client. Once a client gets in touch, it creates a normal Socket object, and hands it over to you so that you can start doing all the socket operations you want. Note that, this Socket object returned by accept() method, represents the other end of the connection. After all, if you want to send some data to the client, you can't write it to your own socket!

Next is the Socket class. You create the Socket object by passing an IP address and a port number. Java gives you the InetAddress class to represent an IP address, so you better go their way and use it. To create an InetAddress object that represents an IP address, you can use this method -

InetAddress ipAddress = InetAddress.getByName(address);

Note that in our program, we have 127.0.0.1 for the address. This address is a special address called loopback address. Don't panic, it is just an address that represents the local computer. If you intend to run the client and server on different machines, use the correct IP address of the server.

Once the InetAddress is created, we create the Socket, Socket socket = new Socket(ipAddress, serverPort);

Once you have the Socket object, you can get the input and output streams of the socket. The input stream will let you read from the socket and the output stream lets you write to the socket.

InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream();

The below lines are just for converting the above to different types of streams. So that it becomes easy for us to deal with String objects. This has got nothing to do with networking.

DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout);

The rest is easy. Because it just deals with the stream objects you created, and not with sockets. You can use your favorite stream, call your favorite methods, somehow ensure the data reaches the other end. Read up on streams if you are not comfortable.

I hope I enlightened you all by introducing the fabulous world of network programming. Feel free to shoot your questions, I'll answer them if I can. You can also give me your valuable feedback, suggestions, or the mistakes you found in this tutorial.

And yes, if you found this useful, go to that Your Vote! section below and vote for me!


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.