windows c语言写一个tcp的服务端和客户端
时间: 2024-02-06 10:04:04 浏览: 179
c语言实现IPV6服务端与客户端源码(嵌入式设备)
以下是一个简单的 Windows C 语言 TCP 服务端和客户端的示例代码。服务端可以监听客户端的连接,并将客户端发送的消息回显给客户端。
服务端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#define PORT 8080
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddress, clientAddress;
int addrLen = sizeof(clientAddress);
char buffer[1024];
// 初始化 Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建服务器套接字
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET) {
printf("Failed to create server socket.\n");
WSACleanup();
return 1;
}
// 设置服务器地址
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
// 绑定服务器套接字到服务器地址
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("Failed to bind server socket.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 监听客户端连接
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Failed to listen for incoming connections.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server is listening on port %d...\n", PORT);
// 接受客户端连接
clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, &addrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to accept incoming connection.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected from %s:%d\n", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
// 接收和发送数据
while (1) {
int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesReceived == SOCKET_ERROR) {
printf("Failed to receive data from client.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
if (bytesReceived == 0) {
printf("Client disconnected.\n");
closesocket(clientSocket);
WSACleanup();
return 0;
}
printf("Received %d bytes from client: %s", bytesReceived, buffer);
if (send(clientSocket, buffer, bytesReceived, 0) == SOCKET_ERROR) {
printf("Failed to send data to client.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#define PORT 8080
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddress;
char buffer[1024];
// 初始化 Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建客户端套接字
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to create client socket.\n");
WSACleanup();
return 1;
}
// 设置服务器地址
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_port = htons(PORT);
// 连接到服务器
if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("Failed to connect to server.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Connected to server on port %d.\n", PORT);
// 发送和接收数据
while (1) {
printf("Enter message to send: ");
fgets(buffer, sizeof(buffer), stdin);
if (send(clientSocket, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
printf("Failed to send data to server.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesReceived == SOCKET_ERROR) {
printf("Failed to receive data from server.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
if (bytesReceived == 0) {
printf("Server disconnected.\n");
closesocket(clientSocket);
WSACleanup();
return 0;
}
buffer[bytesReceived] = '\0';
printf("Received %d bytes from server: %s", bytesReceived, buffer);
}
return 0;
}
```
注意:上述代码仅为示例代码,没有进行错误处理和安全性检查。在实际应用中,应该添加适当的错误处理和安全性检查。
阅读全文