Compter Network
Compter Network
Of
(KCS-653)
(2022-23)
4.
Study of Socket
Programming and Client-
Server model
Figure 1.4: Color coding sequence for RJ45 connectors at both ends in straight through wired
connection
Result- Configuration of networking hardware like RJ-45 connector, CAT-6 cable, crimping
tool, etc. is learned.
Question 2 : Configuration of router, hub, switch etc. (using real device or simulators)
Procedure:
Configuration of Hub using Star Topology
Step 1:- Open Cisco packet Tracer Software and choose Generic Hub on workspace.
Step 4:- Now select Simple Message(PDU) from right side of window and click over sender
Step 1:- Open Cisco packet Tracer Software and choose Generic Switch on workspace.
Configuration of Router
Modes of Router
3. Global Configuration Mode to Fast Ethernet Interface Mode: Router (config)# interface
FastEthernet 0/0
Step 3: Now click on each end device and enter IP address such as 10.0.0.1 and label the device
with corresponding IP address using text tool available in cisco packet tracer. Be ensure that router
should connect two different networks.
Figure 2.11:IP configuration in end devices
Step 4: Double click router and type router commands to configure router at interface fa0/0 and
fa0/1
Router>enable
Router#configure terminal
Router(config)#hostname R1
Router(config)#interface fastethernet
0/0 Router(config-if )# 10.0.0.1 255.0.0.0
Router(config-if )# no shut down
AIM
To implement the stop and wait protocol using java programming language.
PROCEDURE
Sender
Step1: sequence 0
Step2: Accept new packet and assign sequence to it.
Step3: Send packet sequence with sequence number sequence.
Step4: Set timer for recently sent packets.
Step5: If error free acknowledgment from receiver and NextFrameExpected -> sequence then
sequence NextFrameExpected.
Step6: If time out then go to step3.
Step7: Stop.
Receiver
Step1: Start.
Step2: NextFrameExpected 0, repeat steps 3 forever.
Step3: If error-free frame received and sequence= NextFrameExpected, then pass packet to higher layer and
NextFrameExpected NextFrameExpected+1(modulo 2).
Step4: Stop.
CODE
Sender.java
import java.io.*;
import java.net.*;
public class Sender{
Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,str, msg;
int n,i=0,sequence=0;
Sender(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for Connection....");
sender = new Socket("localhost",2005);
sequence=0;
out=new ObjectOutputStream(sender.getOutputStream());
out.flush();
in=new ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
System.out.println("reciver > "+str);
System.out.println("Enter the data to send....");
packet=br.readLine();
n=packet.length();
do{
try{
if(i<n){
msg=String.valueOf(sequence);
msg=msg.concat(packet.substring(i,i+1));
}else if(i==n){
msg="end";out.writeObject(msg);break;
}out.writeObject(msg);
sequence=(sequence==0)?1:0;
out.flush();
System.out.println("data sent>"+msg);
ack=(String)in.readObject();
System.out.println("waiting for ack.....\n\n");
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println("receiver > "+" packet recieved\n\n");
}else{
System.out.println("Time out resending data....\n\n");
sequence=(sequence==0)?1:0;
}}catch(Exception e){}
}while(i<n+1);
System.out.println("All data sent. exiting.");
}catch(Exception e){}
finally{
try{
in.close();
out.close();
sender.close();
}
catch(Exception e){}
}}
public static void main(String args[]){
Sender s=new Sender();
s.run();
}}
Receiver.java
import java.io.*;
import java.net.*;
public class Receiver{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Receiver(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
reciever = new ServerSocket(2005,10);
System.out.println("waiting for connection...");
connection=reciever.accept();
sequence=0;
System.out.println("Connection established :");
out=new ObjectOutputStream(connection.getOutputStream());
out.flush();
in=new ObjectInputStream(connection.getInputStream());
out.writeObject("connected .");
do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(packet.substring(0,1))==sequence){
data+=packet.substring(1);
sequence=(sequence==0)?1:0;
System.out.println("\n\nreceiver >"+packet);
}
else
{
System.out.println("\n\nreceiver >"+packet +" duplicate data");
}if(i<3){
out.writeObject(String.valueOf(sequence));i++;
}else{
out.writeObject(String.valueOf((sequence+1)%2));
i=0;
}}
catch(Exception e){}
}while(!packet.equals("end"));
System.out.println("Data recived="+data);
out.writeObject("connection ended .");
}catch(Exception e){}
finally{
try{in.close();
out.close();
reciever.close();
}
catch(Exception e){}
}}
Output:
client class
server class
Sliding window protocol can be implemented in Java using two different classes in the same packages using a
unique id.
Sliding window protocols are used to deliver in-order packets in Transmission Control Protocol (TCP) and Data link
layer and it is also a feature of packet-based data transmission protocols.
Detail:
The socket is an endpoint of the two-way communication link between two programs running on the same network
and the programs communicate with each other using the port running port numbers on the network. Here, the client
and server are the two programs using the socket for transferring data.
Advantage:
• Sliding window protocol is efficient than the Stop and Wait protocol.
• Sliding window protocol sends multiple frames and retransmits damaged frames.
Program Coding:
SERVER:
import java.io.*;
import java.net.*;
public class server {
try
{
ServerSocket ss1;
ss1 = new ServerSocket(9000);
Socket s =ss1.accept();
DataInputStream dd= new DataInputStream(s.getInputStream());
String sss1 = dd.readUTF();
System.out.println(sss1);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
--------------------------------
CLIENT:
import java.io.*;
import java.net.*;
public class client {
public static void main(String args[])
{
String h="localhost";
int p=9000;
Try
{
Socket s1;
s1= new Socket(h,p);
DataOutputStream d = new DataOutputStream(s1.getOutputStream());
d.writeUTF("Start server :");
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Output:
Server class
Client class
Question 4. Study of Socket Programming and Client-Server model
Socket Programming
Java Socket programming is used for communication between the applications running on different JRE.
Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and
DatagramPacket classes are used for connection-less socket programming.
Here, we are going to make one-way client and server communication. In this application, client sends a message to
the server, server reads the message and prints it. Here, two classes are being used: Socket and ServerSocket. The
Socket class is used to communicate client and server. Through this class, we can read and write message. The
ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the console until the client
is connected. After the successful connection of client, it returns the instance of Socket at server-side.
Socket class
A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a
socket.
Important methods
Method Description
1) public InputStream getInputStream() returns the InputStream attached with this socket.
2) public OutputStream getOutputStream() returns the OutputStream attached with this socket.
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish communication with the
clients.
Important methods
Method Description
1) public Socket accept() returns the socket and establish a connection between server and
client.
Creating Server:
To create the server application, we need to create the instance of ServerSocket class. Here, we are using 6666 port
number for the communication between the client and server. You may also choose any other port number. The
accept() method waits for the client. If clients connects with the given port number, it returns an instance of Socket.
Creating Client:
To create the client application, we need to create the instance of Socket class. Here, we need to pass the IP address
or hostname of the Server and a port number. Here, we are using "localhost" because our server is running on same
system.
1. Socket s=new Socket("localhost",6666);
Let's see a simple of Java socket programming where client sends a text and server receives and prints it.
File: MyServer.java
1. import java.io.*;
2. import java.net.*;
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(6666);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=new DataInputStream(s.getInputStream());
9. String str=(String)dis.readUTF();
10. System.out.println("message= "+str);
11. ss.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
File: MyClient.java
1. import java.io.*;
2. import java.net.*;
3. public class MyClient {
4. public static void main(String[] args) {
5. try{
6. Socket s=new Socket("localhost",6666);
7. DataOutputStream dout=new DataOutputStream(s.getOutputStream());
8. dout.writeUTF("Hello Server");
9. dout.flush();
10. dout.close();
11. s.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
Question 5. Create a socket for HTTP for web page upload and download.
Aim:
To write a java program for socket for HTTP for web page upload and download .
Algorithm
5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK
signal to client.
Program :
Client
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
dos.write(bytes, 0, bytes.length);
System.out.println("Image sent to server. ");
dos.close();
out.close();
soc.close();
}
}
Server
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class Server {
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept(); System.out.println("Client connected.");
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
dis.readFully(data);
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
JFrame f = new JFrame("Server");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}
}
Output
When you run the client code, following output screen would appear on client side.