用标准C语言在window环境下写基于tcp协议的socket多次通信
时间: 2023-12-21 21:07:19 浏览: 82
下面是一个基于TCP协议的socket客户端和服务器端的示例代码,可以在Windows环境下使用标准C语言编写。
客户端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsa;
SOCKET client_socket;
struct sockaddr_in server_addr;
char message[1024] = {0};
char buffer[1024] = {0};
int bytes_received;
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// 创建socket
if ((client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
printf("socket creation failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// 连接服务器
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return 1;
}
// 发送消息
printf("Enter message: ");
fgets(message, sizeof(message), stdin);
send(client_socket, message, strlen(message), 0);
// 接收响应
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return 1;
}
// 输出响应
printf("Received: %s\n", buffer);
// 关闭socket和Winsock库
closesocket(client_socket);
WSACleanup();
return 0;
}
```
服务器端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
char buffer[1024] = {0};
int bytes_received, client_addr_size = sizeof(client_addr);
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// 创建socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
printf("socket creation failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// 绑定socket到指定地址和端口
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
// 监听连接请求
if (listen(server_socket, SOMAXCONN) == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
// 接受客户端连接
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size)) == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
// 接收消息
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(client_socket);
closesocket(server_socket);
WSACleanup();
return 1;
}
// 输出消息
printf("Received: %s\n", buffer);
// 发送响应
send(client_socket, "Hello from server!", strlen("Hello from server!"), 0);
// 关闭socket和Winsock库
closesocket(client_socket);
closesocket(server_socket);
WSACleanup();
return 0;
}
```
阅读全文