Socket Programming
Socket Programming
Socket Programming
E X . N O: 1
AIM:
ALGORITHM:
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
67
import java.net.*;
import java.util.*;
String host;
host = input.next();
try
68
Out put
69
______________________________________________
E X . N O: 2
AIM:
ALGORITHM:
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
70
import java.net.*;
import java.util.*;
try
System.out.println(address);
71
Output
72
____________________________________________
E X . N O: 3
AIM:
ALGORITHM:
CLIENT SIDE
Step 2: Initialize the file stream and find the size of file using available()method.
Step 4: Read a character from the file and print it using Print stream object
SERVER SIDE
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 6: Write the data received in to the file until null character is reached
73
CLIENT PROGRAM:
import java.net.*;
import java.io.*;
class DatagramClient
ds=new DatagramSocket(clientport);
while(true)
ds.receive(dp);
System.out.println(pdata);
74
SERVER PROGRAM:
import java.net.*;
import java.io.*;
class DatagramServer
ds=new DatagramSocket(serverport);
while(true)
{ String str=breader.readLine();
if(str==null || str.equals("End"))
break;
buffer=str.getBytes();
75
OUTPUT:
SERVER SIDE:
CLIENT SIDE:
RESULT:
Thus the socket programming using UDP has been executed successfully.
76
_________________________________________
EX. NO: 4
____________________________________________
AIM:
To write a program to transfer string between client and server using TCP
ALGORITHM:
Import java.lang.*;Package
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
Step 7: The string is transferred by client to server. If the server is ready it sends the
77
SERVER:
import java.io.*;
import java.net.*;
class tcpserver1
Socket s=ss.accept();
System.out.println("Enter an string:");
String str=sysin.readLine();
out.writeBytes(str+"\n");
78
CLIENT
import java.net.*;
import java.io.*;
String str=in.readLine();
OUTPUT:
D:\java\MEPRACTICE>java server
D:\java\MEPRACTICE>java client
RESULT:
Thus the socket programming using TCP has been executed successfully.
79
________________________________________
EX. NO: 5
_____________________________________________
AIM:
ALGORITHM:
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 7: If it starts with request, the n print there is no replay and connection
Step 6: Stop
80
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
class pingdemo
{
public static void main(String args[])
{
BufferedReader in;
try
{
Runtime r=Runtime.getRuntime();
Process p=r.exec("Ping 127.0.0.1");
if(p==null)
System.out.println("could not connect");
in=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line=in.readLine())!=null)
{
if(line.startsWith("reply"))
System.out.println("this is reply");
else if(line.startsWith("request"))
System.out.println("there is no reply");
else if(line.startsWith("destinator"))
System.out.println("destinator host unreachabl");
else
System.out.println(line);
}
System.out.println(in.readLine());
in.close();
} catch(IOException e)
{System.out.println(e.toString());}
}
}
81
OUTPUT:
RESULT:
Thus the program for ping was implemented to check the connectivity of a host in a network.
82
__________________________________________
EX. NO: 6
_____________________________________________
AIM:
ALGORITHM:
83
CLIENT SIDE:
import java.net.*;
import java.io.*;
String iparr[]={"10.0.1.45","172.16.5.21","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=in.readLine();
int flag=0;
for(int i=0;i<5;i++)
if(str.equals(iparr[i])==true)
flag=1;
String str1=macarr[i];
out.writeBytes(str1+"\n");
break;
if(flag==0)
84
s.close();
SERVER SIDE:
import java.io.*;
import java.net.*;
class arpserver
Socket s=ss.accept();
System.out.println("Enter an IP Address:");
String str=sysin.readLine();
out.writeBytes(str+"\n");
85
OUTPUT:
SERVER
E:\ss\exno7>javac arpserver.java
E:\ss\exno7>java arpserver
Enter an IP Address:
10.0.1.45
CLIENT
E:\ss\exno7>javac arpclient.java
E:\ss\exno7>java arpclient
10.0.1.45
RESULT:
86
_______________________________________
EX. NO: 7
___________________________________________
AIM:
ALGORITHM:
87
CLIENT
import java.net.*;
import java.io.*;
String iparr[]={"10.0.1.45","172.16.5.21","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=in.readLine();
int flag=0;
for(int i=0;i<5;i++)
if(str.equals(macarr[i])==true)
flag=1;
String str1=iparr[i];
out.writeBytes(str1+"\n");
break;
88
if(flag==0)
s.close();
SERVER
import java.io.*;
import java.net.*;
class rarps
Socket s=ss.accept();
String str=sysin.readLine();
out.writeBytes(str+"\n");
89
OUTPUT:
SERVER
E:\ss\exno8>javac rarps.java
E:\ss\exno8>java rarps
00-0c-6e-5c-3c-63
CLIENT
E:\ss\exno8>javac rarpc.java
E:\ss\exno8>java rarpc
RESULT:
Thus the program for R-Address Resolution Protocol has been executed successfully.
90
_____________________________________
EX. NO: 8
_____________________________________________
AIM:
ALGORITHM:
Step 6: File received successfully message along with the copy of the file will be
displayed.
91
CLIENT PROGRAM:
import java.io.*;
import java.net.*;
String str=inp.readLine();
so.writeBytes(str);
so.writeBytes("\n");
int str1;
while((str1=s1.read())!=-1)
fos.write((char)str1);
s1.close();
so.close();
inp.close();
s.close();
92
SERVER PROGRAM:
import java.io.*;
import java.net.*;
Socket s=ss.accept();
String s1;
s1=din.readLine();
int str1;
while((str1=fin.read())!=-1)
dos.writeBytes(""+(char)str1);
dos.close();
din.close();
s.close();
93
SERVER SIDE:
CLIENT SIDE:
RESULT:
Thus the file transfer through FTP has been executed successfully.
94
___________________________________________
EX. NO: 9
_____________________________________________
AIM:
ALGORITHM:
CLIENT SIDE
SERVER SIDE
95
CLIENT SIDE
import java.net.*;
import java.io.*;
public class rmc
{ public static void main(String args[])
{ try
{
FileInputStream f=new FileInputStream("a.txt");
int si=f.available();
System.out.println("Size-----"+si);
System.out.println("contents in the file");
Socket s=new Socket("localhost",1500);
PrintStream p=new PrintStream(s.getOutputStream());
for(int i=0;i<si;i++)
{
char r=(char)f.read();
p.print(r);
System.out.print(r);
}
p.close();
s.close();
}
catch(Exception e)
{ System.out.print(e);
}
}
96
}
SERVER SIDE:
import java.net.*;
import java.io.*;
{ int port=1500;
ServerSocket server_socket;
BufferedReader input;
try{
server_socket=new ServerSocket(port);
while(true)
Socket socket=server_socket.accept();
while(true)
String message=input.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec(message);
97
}
catch(Exception e)
{ System.out.println(e);
OUTPUT:
CLIENT
E:\ss\exno6>javac rmc.java
E:\ss\exno6>java rmc
NotePad
SERVER
E:\ss\exno6>javac rms.java
E:\ss\exno6>java rms
Notepad
RESULT:
Thus the program for remote command execution has been executed successfully.
_____________________________
98
_________________________________________
EX. NO: 10
AIM:
To write a program to transfer string between client and server using UDP.
ALGORITHM:
Import java.lang.*;Package
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
Step 7: The string is transferred by client to server. If the server is ready it sends the
99
SERVER:
import java.io.*;
import java.net.* ;
import java.lang.*;
{ int pos=0;
while(true)
{ int c = System.in.read();
switch(c)
case 1:
System.out.println("server");
return;
case '\r':break;
case '\n':ds.send(new
DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientport));
100
pos=0;
break;
default:
buffer[pos++]=(byte)c;
break;
{ ds=new DatagramSocket();
serve();
CLIENT
import java.net.*;
class udpclient
101
while(true)
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
ds = new DatagramSocket(clientport);
serve();
OUTPUT:
D:\java\MEPRACTICE>java server
D:\java\MEPRACTICE>java client
102
103