Sunday 18 September 2011

TCP/IP


DATE/TIME  INFORMATION SERVER AND CLIENT USING TCP/IP
AIM

       To get Time and Date information from the server over a connection oriented Transmission Control Protocol(TCP) using socket programming.

PROBLEM DESCRIPTION

       The client program creating the socket object for initiating a TCP connection to the server. When the client creates the socket object, it specifies the address of the server process that is the IP address of the server and the port number of the process. Upon creation of the socket object, TCP in the client initiates a 3 way handshake and establishes a TCP connection with the server.

COMMAND PROGRAM

Socket(InetAddress host,int port)

               Creats the TCP socket to specify the port on the specified host and tries to connect.

getInputStream()

                Method returns an inputstream that can read that the data from the socket into a program.

getOutputStream()

                Method returns a raw outputstream from writing data from the application to the other end of the socket.

Close()

                Method to disconnect when one opf its two stream closes, when the program ends, or when it’s garbage collected.

ServerSocket(int port)

                It creates the server socket on the port specified by the argument. If to pass 0 for the port number, the system selects an available port.      
         

ALGORITHM

CLIENT PROGRAM

Step 1: Create a socket clientsocket for the client through the port 6789 on the specified host of the server.
Step 2: Get the server IP address of the server using inetaddress.getbyname(host name)  
            method.
Step 3: Get the input from the user and sent the packets to the server using  
getOutputStream method.
Step 4: Wait for the server response.
Step 5: Get the date/time from the server  using getInputStream method.
Step 6: After receiving date/time close the socket connection and print the content.

SERVER PROGRAM

Step 1: Open the server socket sersoc with the port number 6789.
Step 2: Get the address, port number and data packets from the client getInputStream
            method
Step 3: Get the packets from the client and process the packet.
Step 4: Sent the date/time information to the client.
Step 5: Print the date/time information packet.

CODE

DateServer

import java.io.*;
import java.net.*;
import java.util.*;

 public class tserver
{
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(3000);
System.out.println("waiting");
Socket so=s.accept();
PrintStream p=new PrintStream(so.getOutputStream());
p.println(new Date());
so.close();
s.close();
}
}



DateClient

import java.io.*;
import java.net.*;
import java.util.*;
public class tclient
{
public static void main(String args[]) throws Exception
{
DataInputStream din=new DataInputStream(System.in);
Socket s=new Socket("localhost",3000);
DataInputStream din1=new DataInputStream(s.getInputStream());
String st=din1.readLine();
System.out.println("date"+st);
s.close();
}
}


OUTPUT

SERVER
$ cd webtec
$ javac tserver.java
$ java tserver
Waiting…

CLIENT
$ javac tclient.java
@pgoracle webtec]$ java tclient
dateTue May 19 11:03:15 GMT+05:30 2009

0 comments:

Post a Comment