Computer Network Socket Code
Computer Network Socket Code
Assignment 6
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:
close(): Closes , the socket which will also close the input and output streams.
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.
Program: (Client-side)
import java.io.*;
import java.net.*;
try {
Socket socket = new Socket(SERVER_IP, SERVER_PORT);
out.println(input);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Program: (Server-side)
import java.io.*;
import java.net.*;
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);
}
}
Output: (Client-side)
(Server-side)