______________________________________________
E X . N O: 1
DATE: Find the Web site address using Socket programming
AIM:
To find the website address using socket programming
ALGORITHM:
Step 1: Start the process.
Step 2: Initialize the file stream and find the size of file using available()method.
Step 3: Read a character from the file and print it using Print stream object
Step 4: using Inet address to find the host ip address
Step 5: Close print stream
Step 6: Stop the process
67
import [Link].*;
import [Link].*;
public class IPFinder
public static void main(String[] args)
String host;
Scanner input = new Scanner([Link]);
[Link]("\n\nEnter host name: ");
host = [Link]();
try
InetAddress address = [Link](host);
[Link]("IP address: " + [Link]());
catch (UnknownHostException uhEx)
[Link]("Could not find " + host);
68
Out put
69
______________________________________________
E X . N O: 2
DATE: Find the local host IP address using Socket programming
AIM:
To find the local host IP address using socket programming
ALGORITHM:
Step 1: Start the process.
Step 2: Initialize the file stream and find the size of file using available()method.
Step 3: Read a character from the file and print it using Print stream object
Step 4: using Inet address to find the host ip address
Step 5: Close print stream
Step 6: Stop the process
70
import [Link].*;
import [Link].*;
public class MyLocalIPAddress
public static void main(String[] args)
try
InetAddress address =[Link]();
[Link](address);
catch (UnknownHostException uhEx)
[Link]("Could not find local address!");
71
Output
72
____________________________________________
E X . N O: 3
DATE: SOCKET PROGRAMMING USING UDP
AIM:
To transfer a file from the client to server using TCP/IP Protocol.
ALGORITHM:
CLIENT SIDE
Step 1: Start the process.
Step 2: Initialize the file stream and find the size of file using available()method.
Step 3: Initialize the socket with the Port ID
Step 4: Read a character from the file and print it using Print stream object
Step 5: Close print stream
Step 6: Stop the process
SERVER SIDE
Step 1: Start the process.
Step 2: Import the net package. Initialize a file pointer and buffered writer object.
Step 3: Initialize the server socket with the Port ID as given in the client
Step 4: Initialize the server socket to accept the data send by the clients.
Step 5: Initialize the buffered reader object
Step 6: Write the data received in to the file until null character is reached
Step 7: Close the File and Socket Objects
Step 8: Stop the process
73
CLIENT PROGRAM:
import [Link].*;
import [Link].*;
class DatagramClient
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789, serverport=790;
public static void main(String args[]) throws Exception
ds=new DatagramSocket(clientport);
[Link]("Client is waiting for servder to send data");
[Link]("Press Ctrl+C to come out");
while(true)
DatagramPacket dp=new DatagramPacket(buffer, [Link]);
[Link](dp);
String pdata=new String([Link](),0, [Link]());
[Link](pdata);
74
SERVER PROGRAM:
import [Link].*;
import [Link].*;
class DatagramServer
public static DatagramSocket ds;
public static int clientport=789, serverport=790;
public static void main(String args[]) throws Exception
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader breader=new BufferedReader(new InputStreamReader([Link]));
[Link]("Server is waiting for input");
while(true)
{ String str=[Link]();
if(str==null || [Link]("End"))
break;
buffer=[Link]();
[Link](new DatagramPacket(buffer, [Link](),
[Link](),clientport));
75
OUTPUT:
SERVER SIDE:
CLIENT SIDE:
RESULT:
Thus the socket programming using UDP has been executed successfully.
76
_________________________________________
EX. NO: 4
DATE: SOCKET PROGRAMMING USING TCP
____________________________________________
AIM:
To write a program to transfer string between client and server using TCP
ALGORITHM:
Step 1: Start the program
Step 2: Include the import [Link].*;
Import [Link].*;Package
Step 3: Define a class for implementing main method.
Step 4: Port is the number that indicates what kind of protocol a server on the Internet is
using.
Step 5: A datagram socket is the sending or receiving point for a packet delivery service.
Each packet sent or received on a datagram socket is individually addressed and noted.
Step 6: Datagram packets are used to implement a connectionless packet delivery service.
Each message is routed from one machine to another based solely on information
contained within that packet.
Step 7: The string is transferred by client to server. If the server is ready it sends the
sequence of the requested string to the client.
Step 8: Print out with the necessary details.
77
SERVER:
import [Link].*;
import [Link].*;
class tcpserver1
public static void main(String args[])throws IOException
ServerSocket ss=new ServerSocket(55);
Socket s=[Link]();
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
DataInputStream sysin=new DataInputStream([Link]);
[Link]("Enter an string:");
String str=[Link]();
[Link](str+"\n");
[Link]("The string from TcP server"+[Link]());
78
CLIENT
import [Link].*;
import [Link].*;
public class tCPclient1
public static void main(String args[])throws IOException
Socket s=new Socket("localhost",55);
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
String str=[Link]();
[Link]("string from TCP server"+str);
OUTPUT:
D:\java\MEPRACTICE>java server
CRICKET WORLD CUP
D:\java\MEPRACTICE>java client
CRICKET WORLD CUP
RESULT:
Thus the socket programming using TCP has been executed successfully.
79
________________________________________
EX. NO: 5
DATE: IMPLEMENTATION OF PING
_____________________________________________
AIM:
To implement ping command to determine the connectivity of a host in a network.
ALGORITHM:
Step 1: Create a object for runtime, process classes.
Step 2: Create an object r for runtime.
Step 3: Get the runtime environment by calling getRuntime() method and assign it to r.
Step 4: Execute the ping command using the exec() method of Runtime class.
Step 5: Store the result in p, object of process class, if object value is null
Step 6: Create a string object to get IP address.
Step 7: If it starts with request, the n print there is no replay and connection
Step 6: Stop
80
CLIENT PROGRAM:
import [Link].*;
import [Link].*;
class pingdemo
{
public static void main(String args[])
{
BufferedReader in;
try
{
Runtime r=[Link]();
Process p=[Link]("Ping [Link]");
if(p==null)
[Link]("could not connect");
in=new BufferedReader(new InputStreamReader([Link]()));
String line;
while((line=[Link]())!=null)
{
if([Link]("reply"))
[Link]("this is reply");
else if([Link]("request"))
[Link]("there is no reply");
else if([Link]("destinator"))
[Link]("destinator host unreachabl");
else
[Link](line);
}
[Link]([Link]());
[Link]();
} catch(IOException e)
{[Link]([Link]());}
}
}
81
OUTPUT:
RESULT:
Thus the program for ping was implemented to check the connectivity of a host in a network.
82
__________________________________________
EX. NO: 6
DATE: IMPLEMENTATION OF ARP
_____________________________________________
AIM:
To implement the concept of Address Resolution Protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: Execute the server side program.
Step 3: Enter the MAC address of the machine.
Step 4: The IP address will be displayed.
Step 5: Execute the client side program.
Step 6: IP address will be received from server.
Step 7: Stop the process.
83
CLIENT SIDE:
import [Link].*;
import [Link].*;
public class arpclient
{ public static void main(String args[])throws IOException
{ Socket s=new Socket("localhost",55);
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
String iparr[]={"[Link]","[Link]","172.16..5.22"};
String macarr[]={"00-0c-6e-5c-3c-63","02-11-B6-F3-EF-21","03-12-B3-F3-EF-18"};
String str=[Link]();
[Link]("Ip Address received from server"+str);
int flag=0;
for(int i=0;i<5;i++)
if([Link](iparr[i])==true)
flag=1;
String str1=macarr[i];
[Link](str1+"\n");
break;
if(flag==0)
[Link]("Given IPAddress is not in the network");
84
[Link]();
SERVER SIDE:
import [Link].*;
import [Link].*;
class arpserver
public static void main(String args[])throws IOException
ServerSocket ss=new ServerSocket(55);
Socket s=[Link]();
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
DataInputStream sysin=new DataInputStream([Link]);
[Link]("Enter an IP Address:");
String str=[Link]();
[Link](str+"\n");
[Link]("The corresponding MAC address"+[Link]());
85
OUTPUT:
SERVER
E:\ss\exno7>javac [Link]
E:\ss\exno7>java arpserver
Enter an IP Address:
[Link]
The corresponding MAC address is:00-0c-6e-5c-3c-63
CLIENT
E:\ss\exno7>javac [Link]
E:\ss\exno7>java arpclient
IP Address received from server
[Link]
RESULT:
Thus the program for ARP has been executed successfully.
86
_______________________________________
EX. NO: 7
DATE: IMPLEMENATATION OF RARP
___________________________________________
AIM:
To implement the concept of R-Address Resolution Protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: Execute the server side program.
Step 3: Enter the MAC address of the machine.
Step 4: The IP address will be displayed.
Step 5: Execute the client side program.
Step 6: MAC address will be received from server
Step 7: Stop the process
87
CLIENT
import [Link].*;
import [Link].*;
public class rarpc
public static void main(String args[])throws IOException
Socket s=new Socket("localhost",55);
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
String iparr[]={"[Link]","[Link]","172.16..5.22"};
String macarr[]={"00-0c-6e-5c-3c-63","02-11-B6-F3-EF-21","03-12-B3-F3-EF-18"};
String str=[Link]();
[Link]("Ip Address received from server"+str);
int flag=0;
for(int i=0;i<5;i++)
if([Link](macarr[i])==true)
flag=1;
String str1=iparr[i];
[Link](str1+"\n");
break;
88
if(flag==0)
[Link]("Given IPAddress is not in the network");
[Link]();
SERVER
import [Link].*;
import [Link].*;
class rarps
public static void main(String args[])throws IOException
ServerSocket ss=new ServerSocket(55);
Socket s=[Link]();
DataInputStream in=new DataInputStream([Link]());
DataOutputStream out=new DataOutputStream([Link]());
DataInputStream sysin=new DataInputStream([Link]);
[Link]("Enter an mac Address:");
String str=[Link]();
[Link](str+"\n");
[Link]("The corresponding MAC address"+[Link]());
89
OUTPUT:
SERVER
E:\ss\exno8>javac [Link]
E:\ss\exno8>java rarps
Enter an MAC Address:
00-0c-6e-5c-3c-63
The corresponding IP address is:[Link]
CLIENT
E:\ss\exno8>javac [Link]
E:\ss\exno8>java rarpc
MAC address received from server00-0c-6e-5c-3c-63
RESULT:
Thus the program for R-Address Resolution Protocol has been executed successfully.
90
_____________________________________
EX. NO: 8
DATE: IMPLEMENTATION OF FTP
_____________________________________________
AIM:
To transfer the file from server to client using FTP protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: create a file named output.
Step 3: Compile and run the server program.
Step 4: File successfully sent message will be displayed.
Step 5: Compile and run the client program.
Step 6: File received successfully message along with the copy of the file will be
displayed.
Step 7: Stop the Process.
91
CLIENT PROGRAM:
import [Link].*;
import [Link].*;
public class Ftpclient
public static void main(String a[])throws IOException
Socket s=new Socket([Link](),5555);
DataInputStream s1=new DataInputStream([Link]());
DataInputStream inp=new DataInputStream([Link]);
DataOutputStream so=new DataOutputStream([Link]());
[Link]("\n enter the filename(path)");
String str=[Link]();
[Link](str);
[Link]("\n");
FileOutputStream fos=new FileOutputStream("[Link]");
int str1;
while((str1=[Link]())!=-1)
[Link]((char)str1);
[Link]("\n file received successfully");
[Link]();
[Link]();
[Link]();
[Link]();
92
SERVER PROGRAM:
import [Link].*;
import [Link].*;
public class Ftpserver
public static void main(String a[])throws IOException
ServerSocket ss=new ServerSocket(5555);
Socket s=[Link]();
DataOutputStream dos=new DataOutputStream([Link]());
DataInputStream din=new DataInputStream([Link]());
String s1;
s1=[Link]();
FileInputStream fin=new FileInputStream(s1);
int str1;
while((str1=[Link]())!=-1)
[Link](""+(char)str1);
[Link]("\n file successfully sent");
[Link]();
[Link]();
[Link]();
93
SERVER SIDE:
CLIENT SIDE:
RESULT:
Thus the file transfer through FTP has been executed successfully.
94
___________________________________________
EX. NO: 9
DATE: IMPLEMENTATION OF REMOTE COMMAND EXECUTION
_____________________________________________
AIM:
To create a java program to implement the remote command execution.
ALGORITHM:
CLIENT SIDE
Step 1: Start the process.
Step 2: Connect the server host.
Step 3: Get the input
Step 4: Write the input to the output stream.
Step 5: Send the input to the server.
Step 6: Stop the process
SERVER SIDE
Step 1: Start the process.
Step 2: Connect to the client.
Step 3: Check whether the connection is accepted.
Step 4: Receive the input sent by the client.
Step 5: Print the data.
Step 6: Close the connection.
Step 7: Stop the process.
95
CLIENT SIDE
import [Link].*;
import [Link].*;
public class rmc
{ public static void main(String args[])
{ try
{
FileInputStream f=new FileInputStream("[Link]");
int si=[Link]();
[Link]("Size-----"+si);
[Link]("contents in the file");
Socket s=new Socket("localhost",1500);
PrintStream p=new PrintStream([Link]());
for(int i=0;i<si;i++)
{
char r=(char)[Link]();
[Link](r);
[Link](r);
}
[Link]();
[Link]();
}
catch(Exception e)
{ [Link](e);
}
}
96
}
SERVER SIDE:
import [Link].*;
import [Link].*;
public class rms
{ public static void main(String args[])
{ int port=1500;
ServerSocket server_socket;
BufferedReader input;
try{
server_socket=new ServerSocket(port);
[Link]("Server waiting for client on
port"+server_socket.getLocalPort());
while(true)
Socket socket=server_socket.accept();
[Link]("New connection accepted"+[Link]()
+":"+[Link]());
input=new BufferedReader(new InputStreamReader([Link]()));
while(true)
String message=[Link]();
Runtime r=[Link]();
Process p=[Link](message);
97
}
catch(Exception e)
{ [Link](e);
OUTPUT:
CLIENT
E:\ss\exno6>javac [Link]
E:\ss\exno6>java rmc
NotePad
SERVER
E:\ss\exno6>javac [Link]
E:\ss\exno6>java rms
Notepad
RESULT:
Thus the program for remote command execution has been executed successfully.
_____________________________
98
_________________________________________
EX. NO: 10
DATE DISPLAY THE MESSAGE FROM CLIENT TO SERVER USING UDP
AIM:
To write a program to transfer string between client and server using UDP.
ALGORITHM:
Step 1: Start the program
Step 2: Include the import [Link].*;
Import [Link].*;Package
Step 3: Define a class for implementing main method.
Step 4: Port is the number that indicates what kind of protocol a server on the Internet is
using.
Step 5: A datagram socket is the sending or receiving point for a packet delivery service.
Each packet sent or received on a datagram socket is individually addressed and noted.
Step 6: Datagram packets are used to implement a connectionless packet delivery service.
Each message is routed from one machine to another based solely on information
contained within that packet.
Step 7: The string is transferred by client to server. If the server is ready it sends the
sequence of the requested string to the client.
Step 8: Print out with the necessary details.
99
SERVER:
import [Link].*;
import [Link].* ;
import [Link].*;
public class udpserver
public static int serverport=998;
public static int clientport=999;
public static int buffersize=1024;
public static byte buffer[]=new byte[buffersize];
public static DatagramSocket ds;
public static void serve() throws Exception
{ int pos=0;
[Link](" I AM WAITING FOE YOUR INPUT");
while(true)
{ int c = [Link]();
switch(c)
case 1:
[Link]("server");
return;
case '\r':break;
case '\n':[Link](new
DatagramPacket(buffer,pos,[Link](),clientport));
100
pos=0;
break;
default:
buffer[pos++]=(byte)c;
break;
public static void main(String arg[])throws Exception
{ ds=new DatagramSocket();
serve();
CLIENT
import [Link].*;
class udpclient
public static int serverport=998;
public static int clientport=999;
public static int buffersize=1024;
public static byte buffer[]=new byte[buffersize];
public static DatagramSocket ds;
public static void serve() throws Exception
101
while(true)
{ DatagramPacket p = new DatagramPacket(buffer, [Link]);
[Link](p);
[Link](new String([Link](),0,[Link]()));
public static void main(String arg[])throws Exception
ds = new DatagramSocket(clientport);
serve();
OUTPUT:
D:\java\MEPRACTICE>java server
I AM WAITING FOR YOUR INPUT
Ibri college of technology
D:\java\MEPRACTICE>java client
Ibri college of technology
102
103