This project demonstrates a basic TCP client-server implementation using Windows Sockets.
TCP (Transmission Control Protocol) is a connection-oriented communication protocol widely used for reliable data transmission over networks. Key features of TCP include:
- Reliability: Ensures data is delivered without errors and in the correct order.
- Connection-Oriented: Establishes a connection before data transfer begins.
- Full-Duplex Communication: Enables simultaneous data exchange between client and server.
- Flow Control and Congestion Control: Manages data transmission rates to avoid congestion.
In this project, TCP is used to establish a reliable connection between a server and a client for data exchange.
- g++ compiler with Windows Sockets 2 (WinSock2) library support
To compile the server and client programs, use the following commands:
g++ -o tcp_server tcp_server.cpp -lws2_32
g++ -o tcp_client tcp_client.cpp -lws2_32- Start the server first:
./tcp_server
- Then, start the client:
./tcp_client
- Check your IP address:
ipconfig
- Replace the IP address in both
tcp_server.cppandtcp_client.cppwith your machine's IPv4 address (e.g.,192.168.x.x).
- Initialize WinSock (
WSAStartup): Start the Winsock library. - Create a Socket (
socket): Create a socket to establish communication. - Bind (
bind): Assign the socket to a specific IP address and port. - Listen (
listen): Put the socket in listening mode to accept incoming connections. - Accept (
accept): Accept a connection from a client. - Send and Receive Messages (
sendandrecv):- Use
recvto receive data from the client. - Use
sendto send data to the client.
- Use
- Cleanup (
closesocketandWSACleanup): Close the sockets and clean up resources.
- Initialize WinSock (
WSAStartup): Start the Winsock library. - Create a Socket (
socket): Create a socket for communication with the server. - Connect (
connect): Establish a connection with the server using its IP and port. - Send and Receive Messages (
sendandrecv):- Use
sendto send data to the server. - Use
recvto receive data from the server.
- Use
- Cleanup (
closesocketandWSACleanup): Close the socket and clean up resources.
To connect to the server using a smartphone:
- Ensure both devices (server and client) are on the same network.
- Replace
inet_addr("192.168.102.33")intcp_server.cppwithinet_addr("0.0.0.0")to bind the server to all network interfaces. - Compile and run the server program again.
- Use a TCP Client app (such as "TCP Client" from the play store) on your smartphone to connect to the server. Enter the server's IP address and port (
8000).
- Full-duplex communication between server and client
- Data exchange over a reliable TCP connection
- Add multi-client support to the server.
- Implement a graphical user interface (GUI) for easier interaction.
- Secure the communication with encryption (e.g., TLS).
- Optimize the buffer size and handle edge cases like large messages.
- Add logging for server activity.
For detailed documentation and examples, refer to:
- Microsoft WinSock Documentation
- Beej's Guide to Network Programming (general networking concepts)
- Basics of TCP communication
- Key functions for TCP communication:
WSAStartup,bind,connect,send,recv - TCP client-server architecture
- Compiling and linking with
-lws2_32for networking - IP configuration and networking setup for local devices