TWO WAY COMMUNICATION USING TCP

Step 1 : Open 2 editors and write the coding separately for client and server.
Step2 : Include the  necessary   packages.
Step  3 : Create a new server socket in server side and storing the inputstream  in the buffered    
                   Reader.
Server Socket  ss = new  Server Socket(98);
Socket  s=  ss. Accept ();
BufferedReader  d = new BufferedReader(new  InputStreamReader (SyStepm.in));
Step  4 :  Print  the output stream  by using   print  writer.
PrintWriter  out=  new  PrintWriter(s.getOutputStream(),true);
Step 5 : Read the input and print the string  from the client until the string gets exit.
While(!(inp.equals(“exit”)))
{
String l= sin.readLine();
Out.println(l);
String inp=d.readLine();
}

Step 6 : Create a new socket in the client side.
Socket c= new Socket(InetAddress.getLocalhost(),98);
Step 7 : Print the outputstream and stored in buffered reader.
PrintWriter out=new Printwriter(c.getoutpustream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
Step 8 : Save the program separately with the   java extensions.
Step 9 : Compile and execute the program.


 PROGRAM
SERVER
import java.io.*;
import java.net.*;
import java.lang.*;
class tcpc1
{
public static void main(String args[]) throws IOException
{
try
{
Socket ctsoc=new Socket("localhost",8000);
while(true)
{
BufferedReader in=new BufferedReader(new InputStreamReader(ctsoc.getInputStream()));
String str1=in.readLine();
SyStepm.out.println("Server:"+str1);
if(str1.equals("exit"))
{
ctsoc.close();
}
else
SyStepm.out.println("Client");
BufferedReader stdin=new BufferedReader(new InputStreamReader(SyStepm.in));
PrintWriter out=new PrintWriter(ctsoc.getOutputStream(),true);
String userinput=stdin.readLine();
out.println(userinput);
if(userinput.equals("exit"))
{
ctsoc.close();
}}}
catch(IOException se)
{
SyStepm.out.println("server disconnected...");
}}}

CLIENT
import java.io.*;
import java.net.*;
import java.lang.*;
class tcps1
{
public static void main(String args[])throws IOException
{
ServerSocket srsoc=new ServerSocket(8000);
Socket clientsoc=srsoc.accept();
while(true)
{
try{
SyStepm.out.println("Start:");
String ipln;
BufferedReader stdin=new BufferedReader(new InputStreamReader(SyStepm.in));
ipln=stdin.readLine();
PrintWriter out=new PrintWriter(clientsoc.getOutputStream(),true);
out.println(ipln);
if(ipln.equals("exit"))
{
clientsoc.close();
srsoc.close();
break;
}
BufferedReader in=new BufferedReader(new InputStreamReader(clientsoc.getInputStream()));
String str1=in.readLine();
if(str1.equals("exit"))
{
SyStepm.out.println("Client disconnected...");
clientsoc=srsoc.accept();
}else
{
SyStepm.out.println("Client:"+str1);
}}catch(IOException se)
{
SyStepm.out.println("Client not found...");
clientsoc=srsoc.accept();        
}}}}
OUTPUT
Server
C:\Users\User>cd C:\j2sdk1.4.2_08\bin
C:\j2sdk1.4.2_08\bin>javac tcps1.java
C:\j2sdk1.4.2_08\bin>java tcps1
Start:welcome to india
Client:thank you
Start:hello how are you
Client:fine thanks
Start:
Client
C:\Users\User>cd C:\j2sdk1.4.2_08\bin
C:\j2sdk1.4.2_08\bin>javac tcpc1.java
C:\j2sdk1.4.2_08\bin>java tcpc1
Server: welcome to india
Client: thank you
Server: hello how is you
Client: fine thanks

0 comments: