This repository contains basic implementations of a TCP server and client using the Windows Sockets API (Winsock). These examples demonstrate how to set up a TCP server and client, including initialization, socket creation, binding, listening, and connection.
-
Server:
- Initializes Winsock.
- Creates a TCP socket.
- Binds the socket to a specified IP address and port.
- Listens for incoming connections.
- Accepts a single client connection.
- Basic error handling and cleanup.
-
Client:
- Initializes Winsock.
- Creates a TCP socket.
- Connects to a specified server IP address and port.
- Basic error handling and cleanup.
- Operating System: Windows
- Compiler: Microsoft Visual Studio or MinGW
- Library: Winsock2 (ws2_32.lib)
- Open the project in Visual Studio.
- Ensure
Ws2_32.libis linked. This is typically done by including the following pragma directive in your code:#pragma comment(lib, "Ws2_32.lib") - Build and run the server and client projects separately.
- Open a terminal or command prompt.
- Navigate to the directory containing your
server.cppandclient.cppfiles. - Compile the server:
g++ server.cpp -o server -lws2_32
- Compile the client:
g++ client.cpp -o client -lws2_32
- Run the server executable in one terminal:
./server
- Run the client executable in another terminal:
./client
- Initialize Winsock.
- Create a TCP socket.
- Bind the socket to a specific IP address and port.
- Listen for incoming connections.
- Accept a client connection.
- Cleanup resources.
- Initialize Winsock.
- Create a TCP socket.
- Connect to the server using the specified IP address and port.
- Cleanup resources.