Skip to main content
Dat 2nd Sem Fall 2025
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

HTTP Sockets

What is a socket in Java?

A TCP (Transmission Control Protocol) socket is an endpoint in a network communication where an application can send and receive data in a reliable and ordered manner. It’s a core component of the TCP/IP networking protocol suite and facilitates a bidirectional, persistent connection between two devices on a network. Here’s a brief breakdown:

  1. Endpoint for Communication: A TCP socket represents one endpoint of a two-way communication link between two programs running on the network.

  2. Connection-Oriented: TCP is connection-oriented, meaning a connection is established and maintained until the application programs at each end finish exchanging messages.

  3. Reliability: TCP ensures the reliable delivery of data packets. It guarantees that data sent from one end of the connection is received at the other end correctly and in the same order it was sent.

  4. Data Stream with No Boundaries: Data sent through a TCP socket is treated as a continuous stream of bytes. TCP does not introduce any framing or boundaries, so it’s up to the application to give structure to the data.

  5. Flow Control and Congestion Handling: TCP provides mechanisms for flow control and congestion avoidance, ensuring that the sending rate is adjusted based on the network capacity and the receiver’s ability to handle incoming data.

  6. Port Numbers: Each TCP socket is identified by its IP address combined with a port number, allowing multiple sockets (and hence, multiple connections) to coexist on the same host.

In practice, when an application wants to communicate over the network, it requests the operating system to create a TCP socket, then either connects it to another socket (for client applications) or listens for connections on it (for server applications).

The brief explanation of the communication process

In the context of network communications, a socket exists on both the client and the server side. A socket is an endpoint for sending or receiving data across a computer network. Here’s a bit more detail:

  1. Server Side: On the server side, a socket is created and listens on a specific port for incoming connections. The server socket waits for requests to come in over the network. Upon receiving a request, the server socket may accept the connection, establishing a new socket for the server to communicate with the specific client.

  2. Client Side: On the client side, a socket is used to initiate a connection to the server socket. The client specifies the address of the server (IP address) and the port number on which the server is listening. Once the connection is established, the client socket can send data to and receive data from the server socket.

Once a connection is established, both the client and the server have a socket that they can use to send and receive data. The communication can be bi-directional, meaning data can flow in both directions - from client to server and from server to client. This is how data is exchanged over a network, using protocols like TCP (Transmission Control Protocol) or UDP (User Datagram Protocol), depending on the type of socket and the requirements of the application.

How does the ServerSocket and Socket classes work in Java?

In Java, ServerSocket and Socket are classes used for network communication, but they serve different purposes and operate in distinct roles within a client-server paradigm.

  1. ServerSocket (java.net.ServerSocket):

    • Role: A ServerSocket is used on the server side of a client-server network communication. Its primary role is to listen for incoming client connections.
    • Operation:
      • It waits for incoming requests (listens on a specific port).
      • When a client requests a connection, ServerSocket accepts the request and creates a Socket to handle the client-server communication.
      • It’s like a receptionist in an office waiting for phone calls. When a call comes in, the receptionist directs it to the appropriate person (the Socket).
    • Methods: Common methods include .accept(), which waits for a client to connect and returns a Socket for communicating with the client.
    • Usage: Typically used in server applications to create a point of communication for clients to connect to.
  2. Socket (java.net.Socket):

    • Role: A Socket is used on both the server and client sides, representing an endpoint for communication between two machines.
    • Operation:
      • On the client side, it’s used to initiate a connection to the server (ServerSocket).
      • On the server side, it’s created by the ServerSocket upon a client’s connection request. It represents the server’s side of the client-server connection.
      • It’s like the telephone on each end of a call. One Socket on the client side and another Socket on the server side, facilitating the conversation between the client and the server.
    • Methods: Provides input and output streams for sending and receiving data over the network. Common methods include .getInputStream(), .getOutputStream(), .connect(), .close(), etc.
    • Usage: Used for both server-side and client-side to send and receive data once a connection is established.

In summary, ServerSocket is all about waiting for and accepting incoming connections, acting as a fixed point for clients to connect to. In contrast, Socket is about establishing a connection and facilitating communication (sending and receiving data) between the client and the server once the connection is established.