Saturday, December 15, 2012

Socket Programming in JAVA


Whats a socket?

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 computer  on the Internet, whose sole purpose  in life is to translate the domain name to IP address and vice  versa

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

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.

Setps For Program

1: One Java program will try to connect to another Java program. 
   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.
 
 Create 2 fresh Java programs and call them Server.java and Client.java. 

Server.java
import java.net.*;
import java.io.*; 
public class Server
public static void main(String[] ar) 
 int port = 6666; // just a random port.  
 try 
   ServerSocket ss = new ServerSocket(port); // create a server socket and bind 
   System.out.println("Waiting for a client..."); 
   Socket socket = ss.accept(); // make the server listen for a connection     
   System.out.println("Client Recived."); 
   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("Data from Client: " + 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 .  
    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. 
 try 
{
 InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
 System.out.println("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("----Connect-----");
 /* 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. ");
 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