National University of Computer & Emerging Science Computer Network Lab (CL-307) Lab Session 08
National University of Computer & Emerging Science Computer Network Lab (CL-307) Lab Session 08
SCIENCE
Computer Network Lab (CL-307)
Lab Session 08
Awais Ahmed || Faizan Yousuf || Munim Ali
awais.ahmed@nu.edu.pk || faizan.yousuf@nu.edu.pk || munim.ali@nu.edu.pk
OBJECTIVE
ALGORITHM:
1. Start
2. Declare a variable ‘ip’ as a static InetAddress.
3. Using the function getLocalHost() to find the address of the system.
4. Get the name of the system by using the getHostName() function.
5. By specifying the system name,find out the IP address of the system using the function
getByName().
6. Stop.
FINDING IP ADDRESS
SOURCE CODE:
import java.io.*;
import java.net.*;
class address
{
public InetAddressip;
public static void main(String args[])throws UnknownHostException
{
InetAddress ip=InetAddress.getLocalHost();
System.out.println("\n IP address is :"+ip);
String s1=ip.getHostName();
System.out.println("system number is:"+s1);
InetAddress ip1=InetAddress.getByName("system 10");
System.out.println("\n name of other system is :"+ip1);
}
}
OBJECTIVE: To write a java program to implement one way communication using TCP(Transmission
Control Protocol).
ALGORITHM:
SERVER:
CLIENT:
SOURCE CODE:
CLIENT:
import java.io.*;
import java.net.*;
class client
{
public static void main(String args[])throws IOException
{
Socket s=new Socket("localHost",8000);
DataInputStream in=new DataInputStream(s.getInputStream());
while(true)
{
String str=in.readLine();
System.out.println("Message Received:"+str);
if(str.equals("end"))
{
s.c lose();
break;
}
}
}
}
SOURCE CODE:
SERVER:
import java.io.*;
import java.net.*;
class server
{
public static void main(String a[])throws IOException
{
ServerSocket ss=new ServerSocket(8000);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(System.in);
PrintStream dos=new PrintStream(s.getOutputStream());
while(true)
{
System.out.println("enter message to send:");
String str=in.readLine();
dos.println(str);
if(str.equals("end"))
{
ss.close();
break;
}
}
}
}
TCP-TWO WAY COMMUNICATION
OBJECTIVE: To write a java program to implement two way communication using TCP(Transmission
Control Protocol).
ALGORITHM:
SERVER:
CLIENT:
OBJECTIVE: To write a program in java to perform one way message transfer using the User Datagram
Protocol (UDP).
ALGORITHM:
SERVER:
CLIENT:
SENDER:
import java.io.*;
import java.net.*;
class sender
{
DatagramSocket ds;
DatagramPacketdp;
byte buff[]=new byte[1024];
String str,str1;
booleani=true;
public void send() throws IOException
{
while(i)
{
ds=new DatagramSocket();
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the msg:");
str=in.readLine();
buff=str.getBytes();
dp=new DatagramPacket(buff,buff.length,InetAddress.getLocalHost(),8000);
ds.send(dp);
System.out.println("do u want to continue:yes or no");
str1=in.readLine();
if(str1.equals("yes"))
{
i=true;
}
else
{
i=false;
}
}
}
public static void main(String args[])throws IOException
{
sender se=new sender();
se.send();
}
}
RECEIVER:
import java.io.*;
import java.net.*;
class receiver
{
DatagramSocket ds;
DatagramPacketdp;
byte buff[]=new byte[1024];
String str;
public void receive() throws IOException
{
ds=new DatagramSocket(8000);
while(true)
{
dp=new DatagramPacket(buff,buff.length);
ds.receive(dp);
str=new String (dp.getData(),0,0,dp.getLength());
System.out.println(str);
System.out.println("InetAddress:"+dp.getAddress());
}
}
public static void main(String args[])throws Exception
{
receive re=new receive();
re.receive();
}
}
OBJECTIVE: To write a java program to perform two way message transfer using the user datagram
protocol(UDP).
ALGORITHM:
SERVER:
PING COMMAND
ALGORITHM:
SOURCE CODE:
PING SERVER:
import java.io.*;
import java.net.*;
public class pingserver
{
public static void main(String a[])
{
String line1,line2;
inti;
ServerSocket es;
DataInputStream di;
PrintStream ps;
Socket csoc;
es=null;
csoc=null;
try
{
es=new ServerSocket(9999);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("ping server");
try
{
csoc=es.accept();
di=new DataInputStream(csoc.getInputStream());
ps=new PrintStream(csoc.getOutputStream());
for(i=0;i<4;i++)
{
line1=di.readLine();
System.out.println("pinged by client");
ps.println(line1+"reply from host:bytes=3<time<1ms TT<=128");
}
di.close();
ps.close(); }
catch(Exception e)
{
System.out.println(e);
}
}
}
PING CLIENT:
import java.io.*;
import java.net.*;
public class pingclient
{
public static void main(String args[])
{
PrintWriter out=null;
inti,j,k;
BufferedReadernetworkIn=null;
try
{
System.out.println("enter the IP address:"); DataInputStream in
= new DataInputStream(System.in); String ip = in.readLine();
Socket thesocket = new Socket(ip, 9999);
networkIn = new BufferedReader(new InputStreamReader(System.in)); out =
new PrintWriter(thesocket.getOutputStream()); System.out.println("\npinging"
+ ip + "with 32 bytes of data\n"); for (i = 0; i< 4; i++)
{
out.println(ip);
out.flush();
String inp = networkIn.readLine();
if (inp != null)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
}
System.out.println("reply from" + inp);
}
else
{
for (i = 0; i< 4; i++)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
System.out.println("\nrequest time out");}}}}
catch (IOException e)
{
for (i = 0; i< 4; i++)
{
for (j = 0; j < 1000; j++)
{
for (k = 0; k < 5000; k++)
{}}
System.out.println("\nrequest timed out");}}
try
{
if(networkIn!=null)
networkIn.close();
if(out!=null)
out.close();
}
catch(Exception e){
System.out.println("\nrequested time out");
}
}
}
IMPLEMENTATION OF FTP
ALGORITHM:
SERVER SIDE:
CLIENT SIDE
ALGORITHM:
SENDER:
CLIENT:
TASK 01:
Write a java program to perform two way message transfer using the Transmission Control Protocol
(TCP).
TASK 02:
Write a java program to perform two way message transfer using the User Datagram Protocol (UDP).
TASK 03: