CN, LR 03
CN, LR 03
CN, LR 03
LAB REPORT NO 03
Course Title: Computer Networking Lab
Course Code: CSE -312 Section: 212 D6
Student Details
Name ID
1 Objective(s)
● To gather knowledge of simple one-way socket programming.
● To gather knowledge of simple two-way socket programming
● To implement client/server applications that communicate using sockets socket
programming.
2 Problem analysis
2.1One way Socket Programming
One-way socket programming refers to a communication pattern where data flows in one
direction, typically from a client to a server or vice versa. In the context of Java, this
involves using sockets for networking to enable communication between two processes,
one acting as a server and the other as a client.
Here, we are going to make one-way client and server communication. In this application,
the client sends a message to the server, the server reads the message and prints it. Here,
two classes are being used: Socket and ServerSocket. The Socket class is used to
communicate between client and server. Through this class, we can read and write a
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 the client, it returns the instance of Socket at server-side.
Here, we are going to make two-way client and server communication. In this application,
the client sends a message to the server, the server reads the message and replies to the
message and prints it. Here, two classes are being used: Socket and ServerSocket. The
Socket class is used to communicate between client and server. Through this class, we can
read and write a 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 the client, it returns the instance of Socket at server-side.
1
2.3 Basic Figure of the socket programing
The simple flow-chart of socket programming can be summarized as follows:
3 Procedure
3.1 Client Side Programming
For implementing client-side programming, we need to follow the below steps:
1. Create a Socket
2. Connect it to ServerSocket by specifying the IP address and the port number
3. Get the reference of the OutputStream
4. Attach this reference to OutputStreamWriter
5. Write and close
6. Get the reference of InputStream
7. For one-way socket programming , they don’t need any permission for writing.
But for two-way socket programming ,Attach this reference to InputStreamWriter
8. Read and Buffer
9. Parse, interpret and process it
10.Close Connection
2
4 Implementation in Java
Client Side Coding For One way :
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
DataInputStream dis;
try (DataOutputStream dos = new DataOutputStream(s.getOutputStream())) {
dis = new DataInputStream(s.getInputStream());
while (true) {
if (toSend.equalsIgnoreCase("Exit")) {
System.out.println("Closing the connection " + s);
s.close();
System.out.println("Connection closed");
break;
}
}
}
dis.close();
}
}
Socket s = ss.accept();
System.out.println("Client request is accepted at port no: " + s.getPort());
System.out.println("Server's communication port: " + s.getLocalPort());
3
String str = "";
while (!str.equals("Stop")) {
str = input.readUTF();
System.out.println("Client Says: " + str);
}
s.close();
ss.close();
}
}
import java.io.*;
import java.net.*;
try {
Thread serverHandlerThread = new ServerHandler(reader);
serverHandlerThread.start();
while (true) {
System.out.print("Client: ");
String toSend = System.console().readLine();
writer.println(toSend);
if (toSend.equalsIgnoreCase("exit")) {
System.out.println("Closing the connection...");
break;
}
}
} finally {
reader.close();
writer.close();
socket.close();
}
}
}
4
try {
while (true) {
String received = reader.readLine();
System.out.println("Server: " + received);
if (received.equalsIgnoreCase("exit")) {
System.out.println("Server closed the connection...");
break;
}
}
} catch (IOException e) {
}
}
}
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("A new client is connected: " + clientSocket);
while (true) {
5
System.out.print("Server: ");
String toSend = consoleScanner.nextLine();
writer.println(toSend);
if (toSend.equalsIgnoreCase("exit")) {
System.out.println("Closing the connection...");
break;
}
}
consoleScanner.close();
} catch (IOException e) {
System.out.println("Client disconnected: " + clientSocket);
} finally {
try {
reader.close();
writer.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5 Input/Output Client:
For One Way
Server : Client :
6
6 Discussion & Conclusion
Socket Programming in Java is used to set the communication between the two nodes on the
network. There are two important classes for Socket Programming in Java which are Socket and
ServerSocket classes. We covered various important methods of both the classes.At last, we
implemented the socket programming by connecting the client code with the server code. This
article will surely help you to build and sharpen your concepts in Java Socket Programming.