By AMAN KAUSHAL, ABV IIITM, Gwalior(MP) & Deepan Gupta (IIIT Hyderabad 2k13 graduate)
Socket Programming Tutorial: Building a simple Client-Server setup in Java
In the following tutorial we will deal with the basics of socket programming over TCP/IP networks. Also, we will create our own client/server program to create a simple chatting system.
Sockets are just the endpoints of a two-way communication link in a network. Socket helps in the communication of two processes/programs on a network (eg. Internet).
Bidirectional inter process communication
The programs can communicate by reading/writing via their sockets.
A socket comprises of:
IPv4: 32-bit address
IPv6: 128-bit address
The combination of both is also termed as a socket address. Hence using socket address we can connect to any remote machine.
Every socket follows some protocol (set of rules) for transferring the data. Based on the type of protocol used sockets can be classified as:
This document below, contains a comprehensive tutorial which will introduce you to client-server and socket programming in Java.
You may simply read through this casually. You will understand these better once you read through the code for the sample applications discussed later.
Host:- is a Machine participating in a network.
Server Host:- ready to accept request & share the resources(data).
Client Host:- requesting for a service.
A socket is the one end-point of a two-way communication link between two programs running over the network. Running over the network means that the programs run on different computers, usually referred as the local and
the remote computers.
InetAddress:- is a singleton class which contains IP & machine name of a machine in a
static InetAddress getLocalHost() -> returns IP of Local host
static InetAddress getByName(“Machine name”) -> returns IP for a host name
static String getHostName() -> name of host
static String getHostAddress() → Address of Host
static byte getAddress() -----|↗
Socket Programming is the communication Mechanism used by two computers
A Client creates a socket at its end & tries to connect this socket with server.
A Socket requires two elements to establish communication.
(a) The Address of server
(b) The port at which server is listening the request
1. A server socket instantiates & registers a port for communication.
2. It waits till client generates a request.
3. Client Socket instantiates a request at IP of server & given port.
4. A two way communication establishes, Client InputStream connects to server
OutputStream & client OutputStream connects to server InputStream.
Server Socket :- Responsible to open socket at server end and wait for client request.
ServerSocket(int port) throws IOException (Creates server socket on the specified port with a queue
ServerSocket(int port,int maxQueue) throws IOException (Creates a server on the specified port with a maximum queue length of maxQueue)
ServerSocket(int port,int maxQueue,InetAddress ob) (Creates a server socket on the specified port with a maximum queue length of maxQueue.On a multihomed host, ob specifies the IP address to which this socket binds)
Socket accept() -> Server gets hang & wait for Client request
Socket:- class opens socket at client end
Creating a client/server application where the server sends “welcome to server” message to client & client sends “Hello from Client” to server.
Take a good look at the Java code in the tutorial document.
The next example is a chat application. A chat application consists of a chat server and a chat client. The server accepts connections from the clients and delivers all messages from each client to other clients. This is a tool to communicate with other people over Internet in real time. The client is implemented using two threads - one thread to interact with the server and the other with the standard input. Two threads are needed because a client must
communicate with the server and, simultaneously, it must be ready to read messages from the standard input to be sent to the server.
The server is implemented using threads also. It uses a separate thread for each connection. It spawns a new client thread every time a new connection from a client is accepted. This simplifies a lot the design of the server. Multi-threading, however, creates synchronization issues. We will present two implementations of the chat server. An implementation that focus on multi-threading without considering the synchronization issues will be presented
first. Then we will focus on the synchronization issues that a multi-threaded implementation creates. Finally, an updated version of the multithreaded chat server that fixes the synchronization issues is presented.
The code presented in the document is the multi-threaded chat client. It uses two threads: one to read the
data from the standard input and to sent it to the server, the other to read the data from the server and to print it on the standard output.
Computer Science >