Socket Programming in C
Socket programming is a method used in network communication that allows data to be sent and received between devices. It is a critical concept in computer networking and is widely used to develop client-server applications.
In this blog post, we will explore the fundamentals of socket programming in C, focusing on TCP (Transmission Control Protocol) for reliable, connection-oriented communication.
What is Socket Programming?
Socket programming enables communication between two nodes on a network. A server listens for incoming client requests, and the client connects to the server to facilitate data exchange.
Sockets provide a communication channel between two processes, either on the same machine or different machines connected via a network.
Types of Sockets
Stream Sockets (SOCK_STREAM):
Uses TCP for communication.
Provides reliable, connection-oriented communication.
Data is transmitted in order and without loss.
Datagram Sockets (SOCK_DGRAM):
Uses UDP (User Datagram Protocol).
Connectionless and unreliable, but faster.
Data may arrive out of order or get lost.
Raw Sockets:
Allows direct access to lower-level protocols.
Used for packet-level manipulation.
How Socket Communication Works
Server Setup:
Create a socket.
Bind the socket to an IP address and port.
Listen for incoming connections.
Accept client connections.
Client Setup:
Create a socket.
Connect to the server.
Data Exchange:
Send and receive data between the server and client.
Server Code
How It Works
- Server:
- Creates a socket with
socket()
. - Binds to an IP/port with
bind()
. - Waits for connections using
listen()
. - Accepts incoming connections with
accept()
. - Receives and sends data using
recv()
andsend()
.
- Client:
- Creates a socket with
socket()
. - Connects to the server with
connect()
. - Sends a message using
send()
. - Receives a response from the server.
Testing the Program
- Compile the server and client:
- Run the server:
- In another terminal, run the client:
- The server will receive the message and respond back to the client.
This is a simple TCP server program in C that accepts a client connection, receives a message, and sends a response. Let me break down the key parts of the code for you:
Breakdown of the Code: Server
Headers:
- These headers include functions for input/output, string manipulation, memory allocation, system calls (like
read
andwrite
), and network communication.
- These headers include functions for input/output, string manipulation, memory allocation, system calls (like
Socket Creation:
- Creates a TCP socket.
AF_INET
specifies IPv4,SOCK_STREAM
indicates TCP (connection-oriented).
Server Address Structure:
sin_family
specifies the address family (IPv4).INADDR_ANY
allows the server to accept connections on any network interface.htons(8080)
converts the port number to network byte order (big-endian).
Binding the Socket:
- Associates the socket with the IP address and port.
Listening for Connections:
- Puts the socket in passive mode to listen for incoming connection requests.
5
specifies the backlog, the number of pending connections allowed.
Accepting a Connection:
- Accepts the first incoming client connection and creates a new socket (
newsock
) for communication.
- Accepts the first incoming client connection and creates a new socket (
Receiving Data:
- Reads data from the client and prints the received message.
Sending a Response:
- Sends a response back to the client.
Closing Sockets:
- Closes both the connection socket (
newsock
) and the listening socket (sockfd
).
- Closes both the connection socket (
Breakdown of the Client Code:
Socket Creation:
- Creates a TCP socket for the client.
Server Address Structure:
- The
inet_addr("127.0.0.1")
specifies the localhost (server running on the same machine). - Change the IP to the server's IP if running on different machines.
Connecting to Server:
- Connects to the server. The server must be running before this.
Sending Data:
- Sends a message to the server.
Receiving Response:
- Reads the server’s response and displays it.
Closing Socket:
Notes:
Socket Creation:
- Creates a TCP socket for the client.
Server Address Structure:
- The
inet_addr("127.0.0.1")
specifies the localhost (server running on the same machine). - Change the IP to the server's IP if running on different machines.
Connecting to Server:
- Connects to the server. The server must be running before this.
Sending Data:
- Sends a message to the server.
Receiving Response:
- Reads the server’s response and displays it.
Closing Socket:
- Error Handling: This code lacks error handling. Consider adding checks after socket creation, binding, listening, and accepting to ensure smooth operation.
- Port Selection: Ensure port 8080 is free or change to an available port if needed.
- Concurrency: This implementation handles one client at a time. For multiple clients, consider using
fork()
or threads.
Comments
Post a Comment