Sei sulla pagina 1di 36

Network Programming Lab Manual

CHHATTISGARH SWAMI VIVEKANAND TECHNICAL UNIVERSITY

Network Programming Lab

Department of Computer Science and Engineering


Shri Shankaracharya Group of Intuitions
Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 1
Network Programming Lab Manual

Prerequisites Experiments to be performed


1. Write a Program to Display Inet Address.
2. Write a Program To display the protocol, host and external form of
an URL.
3. Write a Program To display the content of a file
4.

List of Experiments to be performed


1. Write an echo program with client and iterative server using TCP.
2. Write an echo program with client and concurrent server using
TCP.
3. Write an echo program with client and concurrent server using
UDP.
4. Write a client and server program for chatting.
5. Write a program to retrieve date and time using TCP.
6. Write a program to retrieve date and time using UDP.
7. Write a client and server routines showing Blocking I/O.
8. Write a client and server routines showing I/O multiplexing.
9. Write an echo client and server program using Unix domain
stream socket.
10.Write an echo client and server program using Unix domain
Datagram socket.
11.Write a client and server program to implement file transfer.
12.Write a client and server program to implement the remote
command execution.
13.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.

Text Books:
1. Steven.W.R: UNIX Network Programming, PHI (VOL I& II)

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 2


Network Programming Lab Manual

Aim 1 :- To display the inet address of a node.

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.

/*Display Inet Address*/


Import java.net.*;
Import java.io.*;
Public class net1
{
Public static void main(string s[])
{
Try
{
System.out.println(Inetaddress.getLocalHost());
System.out.println(Inetaddress.getByName(Node-10));
}
Catch(UnknownHostException e)
{
System.out.println(e);
}
}
}

Output :
D:\> javac net1.java
D:\> java netl
Node 1/127.0.0.1
Node 10/192.168.168.249

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 3


Network Programming Lab Manual

Aim 2 :- To display the protocol, host and external form of an URL.

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.

/*Display Protocol, Host and External Form of an URL*/


Import java.net.*;
Class net3
{
Public static void main(String s[])throws MalformedURLException
{
URL hp=new URL ( http://www.agrawalsonu.com/download);
System.out.println (Protocol : + hp.getProtocol());
System.out.println (Inetaddress.getByName(Host: + hp.getport());
System.out.println (External Form: + hp.toExternalForm());
}
}

Output:

D:\> javac net3.java


D:\> java net3

Protocol: http
Host: -1
External Form: http://www.agrawalsonu.com/download

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 4


Network Programming Lab Manual

Aim 3 :- To display the content of a file

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.

/*Display the contents of the file*/


Import java .io.*;
Import java .net.*;
Import java .util.*;
Public class net2
{
Public static void main(string s[])
{
Try
{
URL url=new URl(files:///d:/djava/t1.html);
URL Connection uconn=url.openConnection();
System.out.println (content type + u conn-getContentType());
Int len=uconn.getContentLength();
InputStream inp=uconn.get?InputStream();
BufferedReader br=new BufferedReader
(new InputStreamReader(inp));
String s= ;
While(( s = br.readLine())!=null
{
System.out.println(s);
}
System.out.println( Date : +newDate(uconn.getDate()));
System.out.println(Expired : + new
Date(uconn.getExpiration()));
System.out.println(Modified : + new
Date(uconn.getLastModified()));

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 5


Network Programming Lab Manual

}
Catch(IOException e)
{
System.out.println(e);
}
}
}

Output:

D:\> javac net2.java


D:\> java net 2

Contet type text/html


This is a sample program.
It displays content of this file. This file name is net2.java.

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 6


Network Programming Lab Manual

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.

//Coding for Echo 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;
str1=din.readLine();
System.out.println("From Client:"+str1);
dout.println("Recieved at server "+str1);
s.close();
ss.close();
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 7


Network Programming Lab Manual

//Coding for Echo Client

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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 8


Network Programming Lab Manual

2. Write an echo program with client and concurrent server using TCP.

//Coding for concurrent server


import java.io.*;
import java.net.*;
class newServer
{
public static void main(String as[]) throws IOException
{
ServerSocket serSoc = new ServerSocket(1001);
Socket soc= serSoc.accept();
inServerThread in = new inServerThread(soc);
outServerThread out = new outServerThread(soc);
}
}

class inServerThread extends Thread


{
BufferedReader in = null;
inServerThread(Socket soc)
{
try
{
in= new BufferedReader(new
inputStreamReader(soc.getInputStream()));
}
catch(Exception err) { }
this.start();
}
public void run()
{
String userInput = "";
try
{
do {
userInput= in.readLine();
System.out.println("\nClient : "+userInput);
}while(!userInput.equals("exit"));
in.close();
}
catch(Exception err){}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 9


Network Programming Lab Manual

class outServerThread extends Thread


{
PrintStream out = null;
BufferedReader br = null;
outServerThread(Socket soc)
{
try
{
br = new BufferedReader(new
inputStreamReader(System.in));
out = new PrintStream(soc.getOutputStream());
}
catch(Exception err){}
this.start();
}
public void run()
{
String userInput = "";
try
{
do{
System.out.print("Server : ");
userInput=br.readLine();
out.println(userInput);
}while(!userInput.equals("exit"));
}
catch(Exception err){}
}
}

//Coding for client


import java.io.*;
import java.net.*;
class newClient
{
public static void main(String as[]) throws IOException
{
Socket soc= new Socket("127.0.0.1",1001);
inClientThread in = new inClientThread(soc);
outClientThread out = new outClientThread(soc);
}
}

class inClientThread extends Thread


{

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 10


Network Programming Lab Manual

BufferedReader in = null;
inClientThread(Socket soc)
{
try
{
in= new BufferedReader(
new InputStreamReader(soc.getInputStream()));
}
catch(Exception err){}
this.start();
}

public void run()


{
String userInput = "";
try
{
do
{
userInput= in.readLine();
System.out.println("\nServer : "+userInput);
}while(!userInput.equals("exit"));
in.close();
}catch(Exception err){}
}
}

class outClientThread extends Thread


{
PrintStream out = null;
BufferedReader br = null;
outClientThread(Socket soc)
{
try
{
br = new BufferedReader(
new InputStreamReader(System.in));
out = new PrintStream(soc.getOutputStream());
}
catch(Exception err){}
this.start();
}

public void run()


{
String userInput = "";

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 11


Network Programming Lab Manual

try
{
do
{
System.out.print("Client : ");
userInput=br.readLine();
out.println(userInput);
}while(!userInput.equals("exit"));
}
catch(Exception err){}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 12


Network Programming Lab Manual

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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 13


Network Programming Lab Manual

//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);

DatagramPacket rpack=new DatagramPacket


(rdata,rdata.length);
s.receive(rpack);
String modse=new String(rpack.getData());
System.out.println("From server : "+modse);
}
while(!se.equalsIgnoreCase("End"));
s.close();
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 14


Network Programming Lab Manual

4. Write a client and server program for chatting.

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;

public class frameServer extends JFrame implements ActionListener


{
JTextArea jt = new JTextArea();
JScrollPane jsp = new JScrollPane
(jt,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextField jf = new JTextField(30);
JButton jb = new JButton("Send");
JButton je = new JButton("Exit");
PrintStream out = null;
Socket soc = null;
ServerSocket serSoc = null;
PrintStream out1 = null;
Socket soc1 = null;
ServerSocket serSoc1 = null;
KeyStroke ks = null;
frameServer()
{
add(jsp);
add(jf);
add(jb);
add(je);
setLayout(null);
ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);
jb.registerKeyboardAction(this,"Send",ks,
JComponent.WHEN_IN_FOCUSED_WINDOW);
jsp.setBounds(20,20,300,100);
jf.setBounds(65,140,210,25);
jb.setBounds(65,180,80,20);
je.setBounds(195,180,80,20);
jb.addActionListener(this);
je.addActionListener(this);
jt.setEditable(false);
setSize(370,250);
setTitle("Java Chat Server");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setResizable(false);

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 15


Network Programming Lab Manual

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);
}
}

public static void main(String a[])


{
new frameServer();
}
}

class frameReadServer extends Thread


{
Scanner in = null;

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 16


Network Programming Lab Manual

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){}
}
}

////Coding for Chat Client

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;

public class frameClient extends JFrame implements ActionListener


{
JTextArea jt = new JTextArea();
JScrollPane jsp = new JScrollPane(jt,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextField jf = new JTextField(30);
JButton jb = new JButton("Send");
JButton je = new JButton("Exit");
PrintStream out = null;

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 17


Network Programming Lab Manual

Socket soc = null;


KeyStroke ks = null;
frameClient()
{
add(jsp);
add(jf);
add(jb);
add(je);
ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);
jb.registerKeyboardAction(this,"Send",ks,
JComponent.WHEN_IN_FOCUSED_WINDOW);
setLayout(null);
jsp.setBounds(20,20,300,100);
jf.setBounds(65,140,210,25);
jb.setBounds(65,180,80,20);
je.setBounds(195,180,80,20);
jb.addActionListener(this);
je.addActionListener(this);
jt.setEditable(false);
setSize(370,250);
setTitle("Java Chat Client");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setResizable(false);
setVisible(true);
try {
soc = new Socket("192.168.3.117",1001);
out = new PrintStream(soc.getOutputStream());
}
catch(Exception err){}
new frameReadClient(soc,jt);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
{
String send = "";
send = jf.getText();
jt.append("Client : "+send);
jt.append("\n");
out.println(send);
jf.setText("");
}
else if(ae.getSource()==je)
{
try{
soc.close();

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 18


Network Programming Lab Manual

}catch(Exception err){}
System.exit(0);
}
}

public static void main(String a[])


{
new frameClient();
}
}

class frameReadClient extends Thread


{
Scanner in = null;
JTextArea t = null;
frameReadClient(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("Rahul : "+get);
t.append("\n");
}while(!get.equals("!"));
in.close();
}catch(Exception err){}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 19


Network Programming Lab Manual

5. Write a program to retrieve date and time using TCP.

//CODE FOR SERVER


import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class tcpserverdate
{
public static void main(String args[])throws Exception
{
Socket c=null;
ServerSocket s=null;
String line;
DataInputStream is,is1;
PrintStream os=null;
Date date=new Date();
SimpleDateFormat d;
Try { s=new ServerSocket(9999); }
catch(IOException e)
{ System.out.println(e); }
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
is1=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
do {
line=is.readLine();
System.out.println("client"+line);
System.out.println("server");
if(line.equalsIgnoreCase("date"))
{
d=new SimpleDateFormat("dd/mm/yyyy hh:mm:ss");
String str=d.format(date);
os.println(str);
System.out.println(str);
}
else{
line=is1.readLine();
os.println(line);
}
}while(line.equalsIgnoreCase("quit")==false);
is.close();
os.close();
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 20


Network Programming Lab Manual

catch(IOException e)
{
System.out.println("socket closed");
}
}
}

//CODE FOR CLIENT


import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;

public class tcpclidate


{
public static void main(String args[])throws Exception
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{ c=new Socket("127.0.0.1",9999); }
catch(IOException e)
{ System.out.println(e); }
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
do
{
System.out.println("client");
line=is.readLine();
os.println(line);
System.out.println("server:"+is1.readLine());
}
while(line.equalsIgnoreCase("quit")==false);
is1.close();
os.close();
}
catch(IOException e)
{ System.out.println("socket closed,message passing is over"); }
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 21


Network Programming Lab Manual

6. Write a client and server program to implement file transfer.

//FTP Server Coding


import java.io.*;
import java.net.*;
import java.util.*;
public class ft1server
{
public static void main(String args[])throws IOException
{
ServerSocket ss;
Socket s;
try
{
System.out.println("Waiting for client");
ss=new ServerSocket(8081);
s=ss.accept();
System.out.println("Connection established");
BufferedReader get=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String fname;
fname =get.readLine();
System.out.println("File name is : "+fname);
File f=new File(fname);
FileWriter fw=new FileWriter(f);
String c;
while((c=get.readLine())!=null)
fw.write(Integer.parseInt(c));
System.out.println("Finished the content");
fw.close();
}
catch(IOException e)
{ }
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 22


Network Programming Lab Manual

//FTP Client Coding


import java.io.*;
import java.net.*;
import java.util.*;
public class ft1client
{
public static void main(String args[])
{
Try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the file name :");
String f=dis.readLine();
File f1= new File(f);
FileReader fr=new FileReader(f1);
Socket s=new Socket("127.0.0.1",8081);
PrintWriter put=new PrintWriter(s.getOutputStream(),true);
put.println(f);
int c=0;
while((c=fr.read())!=-1)
put.println(c);
System.out.println("File content are transferred");
fr.close();
s.close();
}
catch(IOException e)
{ }
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 23


Network Programming Lab Manual

7. Write a client and server program to implement the remote command


execution.

//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();
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 24


Network Programming Lab Manual

//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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 25


Network Programming Lab Manual

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

//Coding for Client

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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 27


Network Programming Lab Manual

Aim:- To implement two way communication.

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.

/*Two way Communication - Client*/

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());
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 28


Network Programming Lab Manual

Catch(Exception e)
{
System.out.println(e);
}
}

/*Two way Communication - Server*/


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));
OutputStream out1=st.getOutputStream();
Bufferedreader br2=new BufferedReader
(new InputStreamReader(System.in));
PrintWriter p1=new PrintWriter(out1);
While(true)
{
System.out.println(Client: +br1.readline());
P1.println(br2.readline());
P1.flush();
}
}
Catch(Exception e)
{
System.out.println(e);
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 29


Network Programming Lab Manual

/*Two Way Communication Server Output*/


D:\>javac chatt3.java
D:\>java chatt3
Server : ServerSocket[addr=0.0.0.0/0.0.0.0, port=0,localport=8080]
Connection :Socket[addr=127.0.0.1/127.0.0.1, port=1032,localport=8080]
Client: hi
How r u?
Client Fine and u?
Ya, I am also fine
Client: k, bye

/* Two Way Communication Client Output */


D:\>javac chatt4.java
D:\>java chatt4
Cs=Socket[addr=localhost/127.0.0.1, port=8080, localport=1032]
Hi
Server: how r u?
Fine and u?
Server: ya, I m also fine
K, bye

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 30


Network Programming Lab Manual

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.

/*One Way Communication with end - Client*/


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();
PrintWriter p2= new printwriter (out2);
BufferedReader br2=new BufferedReader
(new InputStreamReader(System.in));
While(true)
{
P2.println(br2.readline());
P2.flush();
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 31


Network Programming Lab Manual

/*One Way Communication with end - Server*/

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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 32


Network Programming Lab Manual

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)

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 33


Network Programming Lab Manual

{
System.out.println(e);
}
}
}

/*One Way Communication using Router - Client*/

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);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 34


Network Programming Lab Manual

/*One Way Communication using Router - Server*/


Import java.net.*;
Import java.io.*;
Public class chatt2
{
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));
While(true)
{
P2.println(br1.readline());
P2.flush();
}
}
Catch(Exception e)
{
System.out.println(e);
}
}
}

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 35


Network Programming Lab Manual

/* Output Router Program Client */

D:\>javac chatt2.java
D:\>java chatt2
Cs = Socket[addr=node-1/190.168.168.201,port=8080,localport=1437]
Hii
How r u?

/* Output Router Program Router */

D:\>javac chatt6.java
Server StartedServerSocket[addr=0.0.0.0/0.0.0.0, port=0,localport=8080]

/* Output Router Program Router */

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?

Prof. SonuAgrawal, Sr. Assistant Professor, CSE, SSCET, Bhilai Page 36

Potrebbero piacerti anche