Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Java Socket programming for Client-Servers communication

Abstract

Over the years with the emergence of internet, ease of communications and sharing of files has
been improved tremendously. Network communication allows various devices in the network to
share data. Therefore, this paper describes network programming and elements of socket in java.
Socket programming is just similar to network programming which describes various concepts
involved in creating network communication between clients and servers using sockets. Therefore,
socket programming is important to understand as it gives insights on how inter-process
communication in a network works. This paper uses desk review to find relevant information from
existing sources to support the study. Informative journal articles on socket programming we
purposively selected to used in this paper.

Keywords: Socket: Network programming; Socket programming; Client-server; Client-server


communication.

Introduction

Client-server communication is very essential in network programming, this will allow the client to
make a request and the server to respond to clients’ request. We can write a code to connect the
client and the server, this is the main reason why we need socket programming. For a client-server
communication to work, we require a socket; a socket in the server side and a socket in the client
side. For a client to connect to the server, we require a socket, this is the same case for the server,
for a server to connect to the client, it also requires a socket. Therefore, the client machine and the
server machine are connected with the help of a socket.

A server establishing a connection with one client, needs one socket in the client side. Therefore, the
number of clients in the client side dictates the number of sockets that will be created in the server
side. A Server Socket is responsible for creating sockets in the server to connect with the client.
Every time a client sends a request to the server socket, the ServerSocket class will create a socket in
the server.

A socket class establishing a connection, should have an IP address and the port number. The IP
address is needed to identify the specific server that the client need to connect while the port
number is needed to connect to a specific application inside the server. There are various
applications running in a single server, for instance Mysql, FTP, Telent, HTTP, etc. The applications
that run at the server runs on specific ports, certain ports are fixed for specific applications for
example; port 21 for FTP, port 23 for Telnet, Port 80 for HTTP. Therefore, to make a connection
established and to share information, the client socket needs to know the IP address and port
number. Moreover, on the server side, the Server Socket needs to know the ip address of the client
machine, and the connection will be accepted. The client serves as the active device in client-server
communication, the client makes the first move in establishing connection by sending a request
which contains all the information about its address which will be used by the server for response
[1]. The client machine acts as the active device while the server machine acts as the passive device
which in this case only waits for the client to make a request.

Literature review

Client-Server
Socket

Sockets are needed to establish connections in the client-server communication. In the Open
Systems Interconnect (OSI) model, sockets work on the network layer of the OSI and uses IP
protocols. The IP protocols used are the TCP which is connection oriented and the UDP which is
connectionless oriented, and are used for the transmission of data. Furthermore, in session layer of
the OSI model, sockets are established and maintained for communication between the client and
the server machine [1].

Client-Server communication through sockets.

Client-server communication is achieved with the help of sockets. Sockets are used as a mode of
sharing data or information from a client machine to a server machine, and vice versa. The only main
thing for communication to be achieved is to have both devices in the network and they will be able
to communicate. A simple program is run on either of the client-server devices in this mode of
communication via sockets. When a socket is created on the client side, its corresponding socket is
also created on the server side. The sockets are then interconnected and prepared to communicate
via socket functions.
Figure 1: Source [2] (a one-way client- device and server communication)

In the socket API application, the client sends a message to the server, which reads it and prints it.
Two classes are used in this case: Socket and ServerSocket. The Socket class is used to communicate
between the client and the server. We can read and write messages using this class (Socket). The
ServerSocket class is used on the server side while the Socket class is used in the client side. The
accept() method of the ServerSocket class prevents the console from being used until the client is
connected. After a successful client connection, it returns a Socket instance to the server.

Socket Programming

Java Socket programming allows applications running on different JREs to communicate with one
another. Socket programming can be either connection-oriented (UTP) or connection-less (STP)
(UDP). Connection-oriented socket programming is done with the Socket and ServerSocket classes,
while connection-less socket programming is done with the DatagramSocket and DatagramPacket
classes [2].

Methods of Socket and Server Socket

Creating Server application


Creating a server application, we must first create an instance of the ServerSocket class. The
ServerSocket class is used to create a server socket that will be used to communicate with clients
and to establish a connection between client and server. For instance:

Port Number

The port number 6666 is used for communication between the client and server in this case. You can
also select another port number which ranges from 0 to 65535. However, you should know the port
number that is not used in the server. Though, there might be a chance that the port number
selected is already booked by an application, therefore you should use “throw Exception” command.
Ports that can be assigned 0-65535, where 0-1023 are reserved port numbers. Reserved port
numbers are those port numbers that are reserved or already booked for some process or
applications. The accept() method waits for the client to connects to the given port number, then it
returns a Socket instance and connection is established.

Creating a Client Application

In order to create a client application, we must first create an instance of the Socket class. To create
a socket, use the Socket class. A socket is simply a communication endpoint between machines in
the client-server architecture.

Port Number

The IP address or hostname of the server, as well as a port number, must be passed here. Because
our server is running on the same system or on the same machine, we're using "localhost". IP
address is needed at client side to uniquely identify a specific server in the network. Port number is
unique identification in a computer that is linked to a program.

Illustration of client and server socket communication

Methodology

The desktop research methodology was used in this study. Desk research is the collection of
secondary data or data that has already been collected. Because data can be gathered from existing
resources, it is frequently regarded as a low-cost technique when compared to field research. The
main goal in conducting desk research is to review findings of previous studies in order to gain a
broad understanding of a particular field (David, 2016). This study focused on information gathered
from various research papers, which included journals, theses, magazines, documents from
websites. Google search engine is the main tool that was used to collect relevant literature for this
study from research gate, Google Scholar and search of key words. Informative journal articles on
socket programming we purposively selected to be used in this paper. The following keywords were
used to find relevant information: Socket, network programming, socket programming, client-server,
client-server communication.

Findings

Conclusion

Recommendation

You might also like