Skip to content


April 23, 2008

Socket is a software interface between the application thread and the network layer(APSTNDP)

You can have a simple bash file as a server service- on a given port (4444,ip address) registered in xinetd file. And then – telnet (Client) to connect it.

There are three types of sockets:

   1. Unix Domain Sockets
   2. Internet Domain Sockets
   3. NS Domain Sockets

 The feature that distinguishes a network sockets from other sockets is the protocols that it supports. The supported protocols are:

   1. TCP :TCP data is sent as a stream of bytes
   2. UDP: Datagram
   3. Raw IP

 TCP  : guarantees reliable and in-order (correct order of packets) delivery of data from sender to receiver. connection oriented. That means TCP requires that a connection be made between the sender and receiver before data is sent. The socket associated with TCP is known as the Stream Socket.

UDP, like TCP, is one of the core protocols of the IP suite. However, unlike TCP, it neither guarantees in-order delivery of data nor does it requires a connection to be established for sending the data. To put it simply, UDP is an unreliable and connectionless protocol. Sockets associated with UDP are known as Datagram Sockets.

Raw IP is a non-formatted protocol  It works at network and transport layers. A socket associated with Raw IP is known as a Raw Socket. UDP and TCP sockets just receive the payload or the data, whereas Raw Sockets receive the header info of the packet along with the data. The downside of Raw Sockets is that they are tightly coupled with the implementation provided by the underlying host operating system.

The server’s main function is to wait for incoming requests, and to service them when they come in.

//to create a socket bound to port number 8888 with a backlog queue of size 5 and bound with     address of local host the statement would be:

ServerSocket server = new ServerSocket(8888, 5, InetAddress.getLocalHost() );

Next  to listen indefinitely and accept incoming requests. This is done by using the accept() method of ServerSocket class.

Socket incoming = server.accept();

Accept() === Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. Means on hold thread until connection is received.

A new Socket is created and, if there is a security manager, the security manager’s checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

  BufferedReader in = new BufferedReader=  (new InputStreamReader(incoming.getInputStream()));
  PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */);


Purpose of client is to connect to the server and communicate with it using the connection

Creating a Socket object.

Socket s=new Socket(“localhost”,8888);

 Communicating with the server using BufferedInputStream and PrintWriter.

 Here’s the basic process performed by a TCP based socket server.

   1. Make the socket data structure (socket())
   2. Declare and fill in the struct sock_addr_in structure
   3. Bind the socket structure to a port (bind())
   4. Convert the socket to a passive listener (listen())
   5. Accept a client and open an active socket for it (accept())
   6. Send and receive data to and from the client (send() and recv())

Two types of sockets   Two types of sockets are active and passive.
Active socket   A socket used to trade data between two programs, using a connection.
Passive socket   A socket used by the server program to listen for clients attempting to connect. When the attempt is detected, the accept() function is used to create a new active socket to service the incoming client. The server needs an active socket for every client with which it’s communicating, but it needs only one passive socket to listen for new clients attempting to connect.

Winsock ;  Berkeley socket library.


Comments are closed.

%d bloggers like this: