Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Text Books:
1. Steven.W.R: UNIX Network Programming, PHI (VOL I& II)
Algorithm:-
Step 1 Start the program.
Step 2 Display the local host address by using getlocalhost() function.
Step 3 Display the inet address of node-10 by using getbyname(Node-10)
function.
Step 4 Stop the program.
Output :
D:\> javac net1.java
D:\> java netl
Node 1/127.0.0.1
Node 10/192.168.168.249
Algorithm:-
Step1: Start the program.
Step2: Display the protocol of an URL using getProtocol().
Step3: Display the port address of an URL using getport().
Step4: Display the external form of an URL using to ExternalForm().
Step5: Stop the program.
Output:
Protocol: http
Host: -1
External Form: http://www.agrawalsonu.com/download
Algorithm:
Step1 Start the program.
Step2 Get the URL using the path of the file.
Step3 Open that file using open connection function.
Step4 Get the length of the file using get content length function.
Step5 Read the content of that file using Input stream Reader.
Step6 Display the content of that file.
Step7 Display the of creation, data of expiry and date of modified of that file.
Step8 Stop the program.
}
Catch(IOException e)
{
System.out.println(e);
}
}
}
Output:
1. Write an echo program with client and iterative server using TCP.
Algorithm:-
Step1: Start The program.
Step2: Create Server Socket in server program.
Step3: Read the input of client message using Input stream reader.
Step4: Display that message.
Step5: Connect to the server in client program.
Step6: Send the message to the server.
Step7: Stop the program.
import java.net.*;
import java.io.*;
class ServerNew
{
public static void main(String[] str)throws IOException
{
ServerSocket ss= new ServerSocket(1234);
Socket s= null;
s = ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream()) ;
PrintStream dout=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str1;
str1=din.readLine();
System.out.println("From Client:"+str1);
dout.println("Recieved at server "+str1);
s.close();
ss.close();
}
}
import java.net.*;
import java.io.*;
class ClientNew
{
public static void main(String[] str)
{
Socket s= null;
DataInputStream dis ;
PrintStream ps;
BufferedReader br ;
String str1;
try
{ s = new Socket("127.0.0.1",1234); }
catch(IOException e)
{ System.out.println("Error is : "+e); }
try
{
dis = new DataInputStream(s.getInputStream());
ps= new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any text : ");
str1=br.readLine();
ps.println(str1);
str1= dis.readLine();
System.out.println("From Server : "+str1);
s.close();
}
catch(IOException e)
{
System.out.println("Error is : "+e);
}
}
}
2. Write an echo program with client and concurrent server using TCP.
BufferedReader in = null;
inClientThread(Socket soc)
{
try
{
in= new BufferedReader(
new InputStreamReader(soc.getInputStream()));
}
catch(Exception err){}
this.start();
}
try
{
do
{
System.out.print("Client : ");
userInput=br.readLine();
out.println(userInput);
}while(!userInput.equals("exit"));
}
catch(Exception err){}
}
}
3. Write an echo program with client and concurrent server using UDP.
import java.io.*;
import java.net.*;
public class udpserver
{
public static void main(String args[])throws IOException
{
DatagramSocket s=new DatagramSocket(8081);
byte[] sdata=new byte[1024];
byte[] rdata=new byte[1024];
String se;
try
{
do
{
DatagramPacket rpack=new
DatagramPacket(rdata,rdata.length);
s.receive(rpack);
String sen=new String(rpack.getData());
System.out.println("Received from client : "+sen);
InetAddress ipa=rpack.getAddress();
System.out.println("Address"+ipa);
int port=rpack.getPort();
System.out.println("port"+port);
System.out.println("Enter the value to send");
DataInputStream in=new
DataInputStream(System.in);
se=in.readLine();
sdata=se.getBytes();
DatagramPacket spack=new DatagramPacket
(sdata,sdata.length,ipa,port);
s.send(spack);
}
while(!se.equalsIgnoreCase("End"));
s.close();
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
}
//Client Coding
import java.io.*;
import java.net.*;
public class udpclient
{
public static void main(String args[])
{
String se;
try
{
DatagramSocket s=new DatagramSocket();
do
{
System.out.println("Enter the data");
DataInputStream in=new DataInputStream(System.in);
InetAddress ipa=InetAddress.getByName("127.0.0.1");
byte[] sdata=new byte[1024];
byte[] rdata=new byte[1024];
se=in.readLine();
sdata=se.getBytes();
DatagramPacket spack=new DatagramPacket
(sdata,sdata.length,ipa,8081);
s.send(spack);
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
setVisible(true);
try{
serSoc = new ServerSocket(1001);
while(true)
{
soc = serSoc.accept();
new frameReadServer(soc,jt);
}
}
catch(Exception err){}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
{
try{
out = new PrintStream(soc.getOutputStream());
}
catch(Exception er){}
String send = "";
send = jf.getText();
jt.append("Rahul : "+send);
jt.append("\n");
out.println(send);
jf.setText("");
}
else if(ae.getSource()==je)
{
try{
soc.close();
serSoc.close();
}
catch(Exception err){}
System.exit(0);
}
}
JTextArea t = null;
frameReadServer(Socket soc,JTextArea jt)
{
t = jt;
try {
in = new Scanner(soc.getInputStream());
}
catch(Exception err){}
this.start();
}
public void run()
{
String get = "";
try
{
do{
get = in.nextLine();
t.append("Client : "+get);
t.append("\n");
}while(!get.equals("!"));
in.close();
}
catch(Exception err){}
}
}
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
}catch(Exception err){}
System.exit(0);
}
}
catch(IOException e)
{
System.out.println("socket closed");
}
}
}
//RMI Server
import java.io.*;
import java.net.*;
public class remServer
{
public static void main(String args[])throws IOException
{
ServerSocket ss=new ServerSocket(8081);
Socket s=ss.accept();
String cmd;
BufferedReader in=new BufferedReader(new
inputStreamReader(s.getInputStream()));
cmd=in.readLine();
try
{
Runtime r=Runtime.getRuntime();
Process a=r.exec(cmd);
System.out.println("Executing command : "+cmd);
}
catch(Exception e)
{
System.out.println("Error"+e);
}
s.close();
}
}
//RMI Client
import java.io.*;
import java.net.*;
public class remClient
{
public static void main(String args[])throws IOException
{
try
{
Socket s=new Socket("127.0.0.1",8081);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
String cmd;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the command to execute on server : ");
cmd=in.readLine();
out.println(cmd);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
8. Write a client program that gets a number from the user and sends the
number to server for conversion into hexadecimal and gets the result from the
server.
import java.net.*;
import java.io.*;
class ServerNew
{
public static void main(String[] str)throws IOException
{
ServerSocket ss= new ServerSocket(1234);
Socket s= null;
s = ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream()) ;
PrintStream dout=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str1;
Int x,d,hex;
str1=din.readLine();
System.out.println("From Client:"+str1);
x = int.parseInt(str1);
if(x<10)
dout.println(x);
else
{
switch(x)
{
Case 10:
dout.println(A);
Case 11:
dout.println(B);
Case 12:
dout.println(C);
Case 13:
dout.println(D);
Case 14:
dout.println(E);
Case 15:
dout.println(F);
}
}
s.close();
ss.close();
}
}
Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 26
Network Programming Lab Manual
import java.net.*;
import java.io.*;
class ClientNew
{
public static void main(String[] str)
{
Socket s= null;
DataInputStream dis ;
PrintStream ps;
BufferedReader br ;
String str1;
try
{ s = new Socket("127.0.0.1",1234); }
catch(IOException e)
{ System.out.println("Error is : "+e); }
try
{
dis = new DataInputStream(s.getInputStream());
ps= new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any Number : ");
str1=br.readLine();
ps.println(str1);
str1= dis.readLine();
System.out.println("Equivalent Hexadecimal number is : "+str1);
s.close();
}
catch(IOException e)
{
System.out.println("Error is : "+e);
}
}
}
Algorithm:-
Step1: Start the program.
Step2: Create server socket in server program
Step3: Read the input of client message using input stream reader and send the
message to the client.
Step4: Display that message.
Step5: Connect the Server in the client program.
Step6: Send the message to the server and read message of the server.
Step7: Display that message.
Step8: Stop the program.
Import java.net.*;
Import java.io.*;
Public class chatt4
{
Static final int port=8080;
Public static void main(string s[])
{
Try
{
Socket cs=new Socket(localhost ,port);
System.out.println(cs= + cs);
OutputStream out2=cs.getoutputStream();
Inputstream inp2=cs.getInputStream();
PrintWriter p2= new PrintWriter(out2);
BufferedReader br2=new BufferedReader
(new InputStreamReader(System.in));
BufferedReader br1=new BufferedReader
(new InputStreamReader(inp2));
While(true)
{
P2.println(br2.readline());
P2.flish();
System.out.println(server: +br1.readline());
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
Aim:- To implement the one way communication and terminate the server if the
client message is end.
Algorithm:
Step1: Start the program.
Step2: Create server socket in server program.
Step3: Read the input message of the client using InputStreamReader.
Step4: Check the client message is end then terminate the server.
Step5: Connect to the server in the client program.
Step6: Send the message to the Server.
Step7: Stop the program.
Import java.net.*;
Import java.io.*;
Public class chatt4
{
Static final int port=8080;
Public static void main(string s[])
{
Try
{
ServerSocket s=new ServerSocket(port);
System.out.println(Server : +s);
Socket st=s.accept();
System.out.println(Connection : +st);
InputStream inp1=st.InputStream();
BufferedReader br1=new BufferedReader
(new InputStreamReader(inp1));
String d;
While(true)
{
D=br1.readline();
Break;
System.out.println(Client: +br1.readline());
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
}
Router Program
Aim:- To implement one way communication using router.
Algorithm:-
Step1: Start the program.
Step2: Connect the client and server using router.
Step3: Send the message to the server through router.
Step4: Display the client message in server mode.
Step5: Stop the program.
/*Router Program*/
Import java.net.*;
Import java.io.*;
Public class chatt4
{
Static final int port=8080;
Public static void main(string s[])
{
Try
{
ServerSocket s=new ServerSocket(port);
System.out.println(Server : +s);
Socket st=s.accept();
System.out.println(Connection : +st);
Socket cs=new Socket(localhost, port);
InputStream inp1=st.getInputStream();
BufferedReader br1=new BufferedReader
(new InputStreamReader(inp1));
System.out.println(cs= +cs);
OutputStream out1=cs.getoutputStream();
PrintWriter p1=newPrintWriter(out1);
BufferedReader br2=new BufferedReader
(new InputStreamReader(System.in));
While(true)
{
P2.println(br1.readline());
P2.flush();
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
}
Import java.net.*;
Import java.io.*;
Public class chatt2
{
Static final int port=8080;
Public static void main(string s[])
{
Try
{
Socket cs=new Socket(localhost, port);
System.out.println(cs= +cs);
OutputStream out1=cs.getoutputStream();
PrintWriter p2=newPrintWriter(out2);
BufferedReader br2=new BufferedReader
(new InputStreamReader(System.in));
While(true)
{
P2.println(br1.readline());
P2.flush();
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
}
D:\>javac chatt2.java
D:\>java chatt2
Cs = Socket[addr=node-1/190.168.168.201,port=8080,localport=1437]
Hii
How r u?
D:\>javac chatt6.java
Server StartedServerSocket[addr=0.0.0.0/0.0.0.0, port=0,localport=8080]
D:\>javac chatt2.java
Server StartedServerSocket[addr=0.0.0.0/0.0.0.0, port=0,localport=8080]
Connection Accepted Socket Socket[addr=NODE 8/192.168.168.244,
port=1437,localport=8080]
Client hii
Client how r u?