Creating Basic TCP Sockets
TCP (Transmission Control Protocol) sockets provide reliable, connection-oriented communication between applications. In this section, we'll learn how to create basic TCP client and server applications using the Socket API in C.
TCP Socket Characteristics
Before diving into the implementation, let's review the key characteristics of TCP sockets:
- Connection-oriented: A connection must be established before data can be exchanged
- Reliable: Guarantees delivery of all data in the correct order
- Stream-based: Data is treated as a continuous stream of bytes
- Bidirectional: Data can flow in both directions simultaneously
- Flow control: Prevents overwhelming the receiver with too much data
- Error detection and correction: Automatically handles packet loss and retransmission
TCP Server Implementation
A TCP server typically follows these steps:
- Create a socket
- Bind the socket to an address and port
- Listen for incoming connections
- Accept client connections
- Receive and send data
- Close the connection
Let's implement a basic TCP echo server that receives messages from clients and sends them back:
Key Components of the TCP Server
-
Socket Creation:
Creates a TCP socket using the IPv4 address family.
-
Socket Options:
Allows the socket to reuse the address, which is useful for server restarts.
-
Binding:
Associates the socket with a specific address and port.
-
Listening:
Marks the socket as passive and specifies a queue length for pending connections.
-
Accepting Connections:
Accepts an incoming connection and creates a new socket for communication.
-
Data Exchange:
Receives data from the client and sends it back.
-
Connection Closure:
Closes the client socket when communication is complete.
TCP Client Implementation
A TCP client typically follows these steps:
- Create a socket
- Connect to the server
- Send and receive data
- Close the connection
Let's implement a basic TCP client that sends messages to the server and receives responses:
Key Components of the TCP Client
-
Socket Creation:
Creates a TCP socket using the IPv4 address family.
-
Address Conversion:
Converts the server's IP address from string to binary form.
-
Connection Establishment:
Establishes a connection to the server.
-
Data Exchange:
Sends data to the server and receives the response.
-
Connection Closure:
Closes the socket when communication is complete.
Compiling and Running
To compile the server and client programs:
To run the programs:
-
Start the server in one terminal:
-
Start the client in another terminal:
-
Enter messages in the client terminal and observe the echo responses from the server.
Common TCP Socket Issues and Solutions
Connection Refused
Issue: The client receives a "Connection refused" error.
Possible causes:
- The server is not running
- The server is running on a different port
- A firewall is blocking the connection
Solutions:
- Ensure the server is running
- Verify the port number
- Check firewall settings
Connection Reset
Issue: The connection is unexpectedly reset.
Possible causes:
- The server crashed
- Network issues
- Timeout
Solutions:
- Implement error handling and reconnection logic
- Add timeout handling
- Use keepalive options
Slow Performance
Issue: Data transfer is slower than expected.
Possible causes:
- Small buffer sizes
- Nagle's algorithm (TCP_NODELAY)
- Network congestion
Solutions:
- Increase buffer sizes
- Disable Nagle's algorithm for latency-sensitive applications
- Implement proper flow control
Advanced TCP Socket Options
TCP_NODELAY
Disables Nagle's algorithm, which buffers small packets:
SO_KEEPALIVE
Enables sending of keepalive messages:
SO_RCVBUF and SO_SNDBUF
Sets the receive and send buffer sizes:
Conclusion
In this section, we've learned how to implement basic TCP client-server communication using sockets in C. TCP sockets provide reliable, connection-oriented communication that is suitable for applications where data integrity is critical.
In the next section, we'll explore UDP sockets, which provide connectionless, message-oriented communication that is faster but less reliable than TCP.
Test Your Knowledge
Take a quiz to reinforce what you've learned
Exam Preparation
Access short and long answer questions for written exams