0% found this document useful (0 votes)
11 views

Computer Network Socket Code

in java and CN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Computer Network Socket Code

in java and CN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DES PUNE UNIVERSITY

Computer Engineering and Technology


Program: B.Tech. Computer Science and Engineering

Academic Year: 2023-24 Year: Second Year Term: II


Roll No.:32 Name: Ayuti Raj Pardeshi
Subject: Computer Networks
Assignment No.: 6 Title: Socket Programming
Date: 24-05-2024

Assignment 6

Theory: Understanding Socket Programming

Socket programming allows two-way endpoint communication between two nodes on the network
to communicate with each other. It allows the data transmission over the network and also between
programs running on different devices. Here in java programming it involves the use of in-build
classes and concepts and methods to establish the connection for sending and receiving the data
between the client and server.

Socket: A socket a one endpoint of two-way communication link between two programs running on
the network. It is identified by an IP address and a port number.

Client-Server Model: It is where the client means one side of the program initiates the connection
request to another program i.e server to request some service.

Classes used:

 Socket: Represents the client-side endpoint for communication with a server. It provides
methods for establishing a connection for sending and receiving data.
 BufferedReader: Reads the text from character input-stream, in this context its used to read
input for reading text form the server and the user
 InputStreamReader: Reads the bytes and decodes them into characters. It’s used to wrap the
input streams for reading text from the server and from the user.
 PrintWriter: Prints formatted representations of objects to a text output stream. In this
context, its used to send data to the server.

Methods used:

getInputStream(): Returns an input stream data from this socket.

getOutputStream(): Returns an output stream for writing data to this socket.

close(): Closes , the socket which will also close the input and output streams.

Comprehensive Methods and Class Explanation:

Methods for Sockets:


getInputStream(): This function reads from the socket and returns an InputStream. Data transmitted
from the opposite end of the link is received using it.
An output stream that writes to the socket is returned by the getOutputStream() function.
Data is sent to the opposite end of the connection via it.
close(): This function releases any related resources and closes the socket. The input and output
streams are also closed by it.

Assignment By: Ayuti Raj Pardeshi Page 1 of 5


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Methods for ServerSockets:

accept(): This function watches for new connections and responds to them. It waits for a connection
to be created before blocking and returning a new Socket object for the connected connection.
close(): This function releases any related resources and shuts the server socket. The server will no
longer accept new connections as a result.
Methods for BufferedReaders:

readLine() reads a single line of text. A line feed (n), a carriage return (r), or a carriage return
immediately followed by a line feed are all regarded as line terminations.
Methods for InputStreamReaders:

The Reader class is where this class mostly inherits its methods, which include read(), close(), and
other similar methods. It serves as a link between character and byte streams.
PrintWriter Techniques:

println(String x): This function ends the line after printing a string. It's the same as using println()
after print(String).

flush(): Ensures that all data given to the output stream gets written out by flushing the stream.

Understanding TCP:

 One of the primary protocols in the Internet protocol stack is Transmission Control Protocol
(TCP).
 It facilitates the safe, efficient, and error-checked transfer of data between hosts connected
by an IP network and their running programmes.
 Since TCP is connection-oriented, data exchange occurs only once a connection has been
made between the client and server.
 With the use of acknowledgments and retransmissions, it guarantees that all data
transmitted is received and is delivered in the right sequence.
Important Features of TCP:
Dependability: TCP makes ensuring that information transmitted from one end is received in
the right sequence, error-free, and exactly as it was sent.
Ordered Delivery: TCP ensures that information is sent and received in the same sequential
sequence.
Connectivity-focused: TCP creates a link between the client and server.

Assignment By: Ayuti Raj Pardeshi Page 2 of 5


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Program: (Client-side)

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

public class Main {


public static void main(String[] args) {
final String SERVER_IP = "192.168.214.203"; // Replace with the
server's IP address
final int SERVER_PORT = 12345;

try {
Socket socket = new Socket(SERVER_IP, SERVER_PORT);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

System.out.println("Enter two numbers separated by space:");


String input = userInput.readLine();

out.println(input);

String result = in.readLine();


System.out.println("Sum received from server: " + result);

socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Assignment By: Ayuti Raj Pardeshi Page 3 of 5


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

Program: (Server-side)

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

public class TCPServer {


public static void main(String[] args) {
final int SERVER_PORT = 12345;

try {
// Create a server socket and bind it to the specified port
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Server listening on port " + SERVER_PORT);

// Wait for a client connection


Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " +
clientSocket.getInetAddress());

// Create input reader to read data from the client


BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
// Create output writer to send data to the client
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);

// Read the input from the client


String input = in.readLine();
System.out.println("Received from client: " + input);

// Parse the input string and calculate the sum


String[] numbers = input.split(" ");
int num1 = Integer.parseInt(numbers[0]);
int num2 = Integer.parseInt(numbers[1]);
int sum = num1 + num2;

// Send the sum back to the client


out.println(sum);

// Close the client socket


clientSocket.close();
// Close the server socket
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}

Assignment By: Ayuti Raj Pardeshi Page 4 of 5


DES PUNE UNIVERSITY
Computer Engineering and Technology
Program: B.Tech. Computer Science and Engineering

}
}

Output: (Client-side)

(Server-side)

Assignment By: Ayuti Raj Pardeshi Page 5 of 5

You might also like