/*PROGRAM
USING TCP SOCKET*/
SOURCE
CODE:
SERVER
PROGRAM:
import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String
args[])throws Exception
{
String fromclient;
String toclient;
ServerSocket Server=new
ServerSocket(5000);
System.out.println("TCP
server waiting for client on port 5000");
while(true)
{
Socket connected=Server.accept();
System.out.println("THE
CLIENT"+" +connected.getInetAddress()+":"+connected.
getPort()+"IS CONNECTED");
BufferedReader inFromUser=new BufferedReader(new
InputStreamReader(System.in));
BufferedReader inFromClient=new
BufferedReader(new InputStream
Reader(connected.getInputStream()));
PrintWriter
outToClient=new PrintWriter (connected.getOutputStream(),true);
while(true)
{
System.out.println("SEND(Type Q or
q to Quit):");
toclient=inFromUser.readLine();
if(toclient.equals("q")||toclient.equals("Q"))
{
outToClient.println(toclient);
connected.close();
break;
}
else
outToClient.println(toclient);
fromclient=inFromClient.readLine(); if(fromclient.equals("q")||fromclient.equals("Q"))
{
connected.close();
break;
}
else
System.out.println("RECEIVED:"+fromclient);
}
}
}
}
CLIENT
PROGRAM:
import java.io.*;
import java.net.*;
class TCPClients
{
public static void main(String
argv[])throws Exception
{
String FromServer;
String ToServer;
Socket ClientSocket=new
Socket("localhost",5000);
BufferedReader inFromUser=new
BufferedReader(new InputStreamReader(System.in));
PrintWriter outToServer=new
PrintWriter(Client
Socket.getOutputStream(),true);
BufferedReader inFromServer=new
BufferedReader(new InputStream
Reader(ClientSocket.getInputStream()));
while(true)
{
FromServer=inFromServer.readLine();
if(FromServer.equals("q")||FromServer.equals("Q"))
{
ClientSocket.close();
break;
}
else
{
System.out.println("RECEIVED:"+FromServer);
System.out.println("SEND(Type Q or
q to Quit):");
ToServer=inFromUser.readLine();
if(ToServer.equals("Q")||ToServer.equals("q"))
{
outToServer.println(ToServer);
ClientSocket.close();
break;
}
else
{
outToServer.println(ToServer);
}
}
}
}
}
--------------------------------------------------------
/*PROGRAM
USING UDP SOCKET*/
Source code
SERVER
PROGRAM:
import java.net.*;
import java.io.*;
class UDPServer
{
public static void
main(String args[])throws Exception
{
byte[] receive_data=new
byte[1024];
byte[] send_data=new
byte[1024];
int recv_port;
DatagramSocket
server_socket=new DatagramSocket(5000);
System.out.println("UDPServer
waiting for client on port 5000");
while(true)
{
DatagramPacket
receive_packet=new DatagramPacket(receive_data,receive_data.length);
server_socket.receive(receive_packet);
String data=new String(receive_packet.getData(),0,0,receive_packet.getLength());
InetAddress
IPAddress=receive_packet.getAddress();
recv_port=receive_packet.getPort();
if(data.equals("q")||data.equals("Q"))
break;
else
System.out.println("("+IPAddress+","+recv_port+")said:"+data);
}
}
}
CLIENT
PROGRAM:
import java.net.*;
import java.io.*;
class UDPClient
{
public static void
main(String args[])throws Exception
{
byte[] send_data=new
byte[1024];
BufferedReader infromuser=new
BufferedReader(new InputStreamReader(System.in));
DatagramSocket
client_socket=new DatagramSocket();
InetAddress
IPAddress=InetAddress.getByName("127.0.0.1");
while(true)
{
System.out.println("Type
something(q or Q to quit):");
String
data=infromuser.readLine();
if(data.equals("q")||data.equals("Q"))
break;
else
{
send_data=data.getBytes();
DatagramPacket send_packet=new
DatagramPacket(send_
data,send_data.length,IPAddress,5000);
client_socket.send(send_packet);
}
}
client_socket.close();
}
}
/*SIMULATION OF
SLIDING WINDOW PROTOCOL*/
SOURCE CODE:
//SENDER:
import java.net.*;
import java.io.*;
import java.rmi.*;
public class Sender
{
public static void
main(String a[])throws Exception
{
ServerSocket ser=new
ServerSocket(10);
Socket s=ser.accept();
DataInputStream in=new
DataInputStream(System.in);
DataInputStream in1=new
DataInputStream(s.getInputStream());
String sbuff[]=new
String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new
PrintStream(s.getOutputStream());
System.out.println("Enter
the no.of frames:");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter"+nf+"messages
to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.println("Acknowledgement
received");
ano=Integer.parseInt(in1.readLine());
System.out.println("for"+ano+"frames");
sws+=nf;
}
else
{
System.out.println("The
no.of frames exceeds window size");
break;
}
System.out.println("Do
you wants to send some more frames:");
ch=in.readLine();
p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}
//CLIENT:
import java.net.*;
import java.io.*;
class Receiver
{
public static void
main(String a[])throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new
DataInputStream(s.getInputStream());
PrintStream p=new
PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuff[]=new
String[8];
String ch;
System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuff[rptr]=in.readLine();
System.out.println("The
received frame"+rptr+"is:"+rbuff[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgement
sent\n");
p.println(rptr+1);
rws+=nf;
}
else
break;
ch=in.readLine();
}while(ch.equals("yes"));
}
}