Trapti Gupta

Socket Programming in Computer Network

Sockets in computer networks are used for allowing the transmission of information between two processes of the same machines or different machines in the network. The socket is the combination of IP address and software port number used for communication between multiple processes. Socket helps to recognize the address of the application to which data is to be sent using the IP address and port number.

What is Socket Programming in Computer Networks?

Sockets allow communication of two processes that are running on the same or different machines. Sockets are the end of two-way communication between two programs that are running on the networks.

  • Sockets are mostly used in client-server architecture for communication between multiple applications.
  • Socket programming tells us how we can use socket API for creating communication between local and remote processes.
  • The socket is created by the combination of the IP address and port number of the software. With this combination, the process knows the system address and address of the application where data is to be sent.
  • : is used to separate IP address and port number. For eg: 192.168.1.67:80, 155.2.12.23:77, etc.

Below image to show the socket address example

socket programming in computer networks

Which Classes are Used for Connection-Less Socket Programming?

Connection-oriented service involves connection establishment before transmitting the data and connection termination after data transmission. Connection-less service does not require any connection establishment and connection termination for transmitting the data over the network.

  • For connection-less socket programming, DatagramSocket and DatagramPacket classes are used.
  • For connection-oriented socket programming, Socket and ServerSocket classes are used.

DatagramSocket class represents a connectionless socket for transmitting datagram packets.

DatagramPacket is a message transmitted between two communicating parties. DatagramPacket is just like a data container that carries data between two communicating parties. When multiple datagram packets are sent over the network they may arrive in any order irrespective of their sending order.

Socket Programming in TCP

TCP stands for Transmission Control Protocol. TCP is a reliable connection-oriented protocol of the transport layer. TCP establishes the connection before data transmission. Steps for TCP socket programming for establishing TCP socket at the client-side:

  • The first step is to create a socket and use the socket() function to create a socket.
  • Use the connect() function for connecting the socket to the server address.
  • Transmit data between two communicating parties using read() and write() functions.
  • After data transmission completion close the connection using close() function.

Following are steps to be followed for establishing a TCP socket on the server-side:

  • Use socket() for establishing a socket.
  • Use the bind() function for binding the socket to an address.
  • Then for listening client connections use listen() function.
  • The accept() function is used for accepting the connection of the client.
  • Transmit data with the help of the read() and write() function.

Below image to show TCP Socket connection

Socket Programming in TCP

Socket Programming in UDP

UDP stands for User Datagram Protocol. UDP is a connection-less and unreliable protocol of transport layer. UDP does not establish a connection between two communicating parties before transmitting the data. Following are the steps given that are to be followed for establishing UDP socket connection on the client-side

  • Use socket() function for creating socket;
  • recvfrom() and sendto() functions are used for transmitting data between two communicating parties.

Steps to be followed for establishing UDP socket connection at the server-side.

  • Create a socket using the socket() function.
  • Use the bind() function for the binding socket to an address.
  • Transmit data with the help of the recvfrom() function and sendto().

Below image to show UDP socket connection

Socket Programming in UDP

Socket Programming Interface Types

There are three types of socket programming interface

Stream sockets: Stream socket is the most common type of socket programming interface. The communicating parties first establish a socket connection between them, so that any data passed through the connection will arrive in the order in which it was sent by the sender because of the connection-oriented service.

Datagram sockets: Provides connection-less services. No connection is established before data transmission. Communicating party transmits the datagrams as required or waits for the response. Data can be lost during the transmission or may arrive out of order. Implementing a datagram provides more flexibility in comparison to using stream sockets.

Raw sockets: This socket interface Bypasses the library’s built-in support for standard protocols such as UDP( User Datagram Protocol) and TCP( Transmission Control Protocol). Raw sockets are socket programming interfaces that are used for custom low-level protocol development.

Become a networking guru with our Free Computer Networks course. Join now and learn to design, implement, and manage networks that power the digital world.

Conclusion

  • The socket is a combination of the IP address and software port address used for communication between two processes.
  • Socket and ServerSocket classes are the classes that are used for connection-oriented socket programming.
  • DatagramSocket and DatagramPacket classes are the classes that are used for connectionless socket programming.
  • UDP is a connection-less unreliable protocol of the transport layer and TCP is a connection-oriented reliable protocol of the transport layer.
  • Stream sockets, Datagram sockets, and raw sockets are the three socket programming interface types.
  • socket(), connect(), read(), write(), close() functions are used on client-side in TCP socket programming.
  • socket(), bind(), listen(), read(), write(), accept() functions are used on server-side in TCP socket programming.
  • socket(), recvfrom(), sendto() functions are used at client-side in UDP Socket programming.
  • socket(), recvfrom(), sendto(), bind() functions are used on server-side in UDP socket programming.

Author