Echo Program using TCP Protocol
Program for Server:
import java.io.*;
import java.net.*;
import java.util.*;
class tcpechos
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
ServerSocket ss=new ServerSocket(1232);
while(true)
{Socket ws=ss.accept();
BufferedReader infromclient=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoclient=new DataOutputStream(ws.getOutputStream());
csent=infromclient.readLine();
System.out.println("From client: "+csent);
ssent=csent;
outtoclient.writeBytes(ssent+'\n');}
}
}
Program for Client:
import java.io.*;
import java.net.*;
import java.util.*;
class tcpechoc
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
Socket ws=new Socket("LocalHost",1232);
BufferedReader infromserver=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoserver=new DataOutputStream(ws.getOutputStream());
BufferedReader infromclient=new BufferedReader(new InputStreamReader(System.in));
csent=infromclient.readLine();
outtoserver.writeBytes(csent+'\n');
ssent=infromserver.readLine();
System.out.println("From server: "+ssent);
}
}
Output for client and server:
Program for Server:
import java.io.*;
import java.net.*;
import java.util.*;
class tcpechos
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
ServerSocket ss=new ServerSocket(1232);
while(true)
{Socket ws=ss.accept();
BufferedReader infromclient=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoclient=new DataOutputStream(ws.getOutputStream());
csent=infromclient.readLine();
System.out.println("From client: "+csent);
ssent=csent;
outtoclient.writeBytes(ssent+'\n');}
}
}
Program for Client:
import java.io.*;
import java.net.*;
import java.util.*;
class tcpechoc
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
Socket ws=new Socket("LocalHost",1232);
BufferedReader infromserver=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoserver=new DataOutputStream(ws.getOutputStream());
BufferedReader infromclient=new BufferedReader(new InputStreamReader(System.in));
csent=infromclient.readLine();
outtoserver.writeBytes(csent+'\n');
ssent=infromserver.readLine();
System.out.println("From server: "+ssent);
}
}
Output for client and server:
0 comments:
Post a Comment