Nothing Special   »   [go: up one dir, main page]

CN, LR 03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Green University of Bangladesh

Department of Computer Science and Engineering(CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year:2023), B.Sc. in CSE (Day)

LAB REPORT NO 03
Course Title: Computer Networking Lab
Course Code: CSE -312 Section: 212 D6

Lab Experiment Name: Implementation of one-way and two-way socket


programming

Student Details

Name ID

1. Maksuda Sarker Mow 202002073

Lab Date : 21-12-2023


Submission Date : 30-12-2023
Course Teacher’s Name : Maisha Muntaha

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
Title: Implementation of one-way and two-way socket programming

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.

2.2 Two way Socket Programming


Two-way socket programming in Java involves establishing a communication channel
between a server and a client, allowing them to send and receive messages.

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:

Figure : Basic flow chart of Socket Programming

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

3.2 Server Side Programming


For implementing server-side programming, we need to follow the below steps:
1. Create ServerSocket
2. Bind it to a port number
3. Put it into the listening mode
4. Get the reference of InputStream
5. Attach the reference to InputStreamReader
6. Read and buffer
For one-way communication after the 6th step we will follow the 12th step.
7. Parse the request
8. Prepare response
9. Get the reference of OutputStream
10.Attach the reference to OutputStreamReader
11.Write the response
12.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;

public class ClientClass {


public static void main(String args[]) throws IOException {

Socket s = new Socket("localhost", 5000);


System.out.println("Connected");

Scanner scn = new Scanner(System.in);

DataInputStream dis;
try (DataOutputStream dos = new DataOutputStream(s.getOutputStream())) {
dis = new DataInputStream(s.getInputStream());
while (true) {

String received = dis.readUTF();


System.out.println(received);

String toSend = scn.nextLine();


dos.writeUTF(toSend);

if (toSend.equalsIgnoreCase("Exit")) {
System.out.println("Closing the connection " + s);
s.close();
System.out.println("Connection closed");
break;
}
}
}
dis.close();
}
}

Server Coding for one way:


import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerClass {


public static void main(String[] args) throws IOException {
int port = 5000;

ServerSocket ss = new ServerSocket(port);


System.out.println("Server is connected at port no: " + port);
System.out.println("Server is Connecting ");
System.out.println("Waiting for the client ");

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

DataInputStream input = new DataInputStream(s.getInputStream());

3
String str = "";

while (!str.equals("Stop")) {
str = input.readUTF();
System.out.println("Client Says: " + str);
}

s.close();
ss.close();
}
}

Client Side coding for two way:

import java.io.*;
import java.net.*;

public class TwoWayClient {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 4030);
System.out.println("Connected to the server.");

BufferedReader reader = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

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

class ServerHandler extends Thread {


private final BufferedReader reader;

public ServerHandler(BufferedReader reader) {


this.reader = reader;
}

public void run() {

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

Server Side coding for two way :


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

public class TwoWayServer {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(4040);
System.out.println("Server is starting...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("A new client is connected: " + clientSocket);

BufferedReader reader = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);

Thread clientHandlerThread = new ClientHandler(clientSocket, reader, writer);


clientHandlerThread.start();
}
}
}

class ClientHandler extends Thread {


private final Socket clientSocket;
private final BufferedReader reader;
private final PrintWriter writer;

public ClientHandler(Socket clientSocket, BufferedReader reader, PrintWriter writer) {


this.clientSocket = clientSocket;
this.reader = reader;
this.writer = writer;
}

public void run() {


try {
Scanner consoleScanner = new Scanner(System.in);

while (true) {

String received = reader.readLine();


System.out.println("Client: " + received);

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 :

For Two 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.

You might also like