SKR 5302: Advanced Distributed Computing
SKR 5302: Advanced Distributed Computing © 2025 by Masnida Hussin is licensed under CC BY-SA 4.0
4. Chapter 4: Inter-process Communication
4.8. Sockets
Sockets
How to use sockets
Setup socket- Where is the remote machine (IP address, hostname)
- What service gets the data (port)
Send and Receive
- Designed just like any other I/O in unix
- send -- write
- recv -- read
Close the socket
Sockets provide an interface for programming networks at the transport layer. Network communication using Sockets is very much similar to performing file I/O. In fact, socket handle is treated like file handle. The streams used in file I/O operation are also applicable to socket-based I/O. Socket-based communication is programming language independent.
That means, a socket program written in Java language can also communicate to a program written in Java or non-Java socket program.
The
Socket API
A socket API provides a programming construct termed a socket. A process wishing to communicate with another process must create an instance, or instantiate, such a construct. The two processes then issue operations provided by the API to send and receive data.
The conceptual model of the socket API

A socket is a programming abstraction which provides an endpoint for communication The receiver process’ socket must be bound to a local port and the Internet address of the computer on which the receiver runs. Messages sent to a particular Internet address and port number can be received only by a process whose socket is bound to that Internet address and port number. A socket is associated with a transport protocol – either TCP or UDP.

Endpoint for communication between processes. Both forms of communication (UDP and TCP ) use the socket abstraction.
Originate from BSD Unix :
- be present in most versions of UNIX
- be bound to a local port (216 possible port number) and one of the Internet address
- a process cannot share ports with other processes on the same computer

Socket
types
Datagram socket – using UDP
- Not sequenced
- Not reliable
- Not unduplicated
- Connectionless
- Border preserving
Stream socket – using TCP
- Sequenced
- Reliable
- Unduplicated
- Connection-oriented
- Not border preserving
Raw and others (extracurricular)

Socket
Communication
A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request.

If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client.

Sockets
and Java Socket Classes
A socket is an endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent. Java’s .net package provides two classes:
- Socket – for implementing a client
- ServerSocket – for implementing a server
Connection-oriented
& Connectionless Datagram Socket
A socket programming construct can make use of either the UDP (User Datagram Protocol) or TCP (Transmission Control Protocol). Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets.
TCP
Vs. UDP Communication

Datagram sockets can support both connectionless and connection-oriented communication at the application layer.
This is so because even though datagrams are sent or received without the notion of connections at the transport layer, the runtime support of the socket API can create and maintain logical connections for datagrams exchanged between two processes.
The runtime support of an API is a set of software that is bound to the program during execution in support of the API.