Socket Network Programming
Nishita Tanna-245890166
AI-B
Socket programming is the fundamental method for creating
network applications. A socket is one endpoint of a two-way
communication link between two programs running on the
network. Sockets provide an interface for programs to send
and receive data, just as a file descriptor provides an interface
for reading and writing to a file.
This report demonstrates the implementation of a basic
Client-Server model, which is the foundation for almost all
network communication (including web browsing, email, and
online gaming).
● The Server: A program that "listens" on a specific
network port (a numbered "door") for incoming
connection requests. It provides a service to clients.
● The Client: A program that "initiates" a connection to the
server's known address and port to use its service.
For this assignment, a simple chat application will be built to
show how this two-way communication is established and
used.
The communication is established using a specific sequence
of commands, or "system calls," on both the server and client
sides.
On the Server Side:
The server's job is to be passive, set up a "reception desk,"
and wait for clients to arrive.
1.socket() - Create the Socket: The server first creates a
socket object, which is like building the "door" but not yet
putting it in a wall.
2.bind() - Assign an Address: The server "binds" the
socket to a specific address and port number. This is like
installing the door at a specific street address (the IP
address) and giving it a specific apartment number (the
port). Other programs will use this exact address to find
it.
3.listen() - Listen for Connections: The server puts the
socket into "listening mode." This tells the operating
system that it's ready to accept incoming connections
from clients.
4.accept() - Accept a Connection: This command blocks
the server and makes it wait. When a client tries to
connect, the accept() command wakes up and
establishes the connection. It returns a new socket object
that is dedicated only to communicating with that specific
client. The original "listening" socket goes back to waiting
for more clients.
On the Client Side:
The client's job is to be active and initiate the conversation.
1.socket() - Create the Socket: Just like the server, the
client creates a socket object.
2.connect() - Connect to the Server: The client actively
tries to connect to the server's specific IP address and
port number. If the server is listen()-ing at that address,
the connection is established.
3.Data Exchange:
Once the connection is established (after the server's accept()
and client's connect() commands are successful), both sides
can communicate.
● send(): Both client and server use this command to send
data.
● recv() (receive): Both use this command to receive data.
● Encoding/Decoding: Data cannot be sent over a
network as plain text; it must be encoded into a stream of
bytes. When the data is received, it must be decoded
back into text. A common format for this is UTF-8.
This project provided a practical understanding of how
sockets are used to establish a two-way, real-time
communication link between a client and a server application.