用C语言编写服务器端和客户端的代码实现windows系统的Socket编程,
时间: 2024-03-17 17:40:31 浏览: 174
Windows环境下socket编程(C语言编写)
5星 · 资源好评率100%
以下是一个简单的Windows系统下的Socket编程示例代码,包括服务器端和客户端的代码,可以用于建立TCP连接并互相发送数据:
服务器端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") // 链接ws2_32.lib库文件
int main()
{
WSADATA wsaData;
SOCKET listenSock, clientSock;
SOCKADDR_IN serverAddr, clientAddr;
char buffer[1024];
int ret, addrLen;
// 初始化WinSock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// 创建Socket
listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSock == INVALID_SOCKET)
{
printf("socket failed!\n");
WSACleanup();
return 1;
}
// 设置Socket地址信息
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// 绑定Socket到地址
if (bind(listenSock, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("bind failed!\n");
closesocket(listenSock);
WSACleanup();
return 1;
}
// 监听Socket连接
if (listen(listenSock, SOMAXCONN) == SOCKET_ERROR)
{
printf("listen failed!\n");
closesocket(listenSock);
WSACleanup();
return 1;
}
while (1)
{
printf("Waiting for client...\n");
// 接受客户端连接请求
addrLen = sizeof(clientAddr);
clientSock = accept(listenSock, (SOCKADDR*)&clientAddr, &addrLen);
if (clientSock == INVALID_SOCKET)
{
printf("accept failed!\n");
closesocket(listenSock);
WSACleanup();
return 1;
}
printf("Client connected: %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 接收客户端发送的数据
ret = recv(clientSock, buffer, sizeof(buffer), 0);
if (ret > 0)
{
buffer[ret] = '\0';
printf("Received from client: %s\n", buffer);
}
else
{
printf("recv failed!\n");
closesocket(clientSock);
continue;
}
// 发送数据给客户端
sprintf(buffer, "Hello, client!");
ret = send(clientSock, buffer, strlen(buffer), 0);
if (ret == SOCKET_ERROR)
{
printf("send failed!\n");
closesocket(clientSock);
continue;
}
// 关闭客户端Socket
closesocket(clientSock);
printf("Client disconnected!\n");
}
// 关闭监听Socket
closesocket(listenSock);
// 释放WinSock库资源
WSACleanup();
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") // 链接ws2_32.lib库文件
int main()
{
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN addr;
char buffer[1024];
int ret;
// 初始化WinSock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// 创建Socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
printf("socket failed!\n");
WSACleanup();
return 1;
}
// 设置Socket地址信息
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// 连接到服务器
if (connect(sock, (SOCKADDR*)&addr, sizeof(addr)) == SOCKET_ERROR)
{
printf("connect failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 发送数据
sprintf(buffer, "Hello, server!");
ret = send(sock, buffer, strlen(buffer), 0);
if (ret == SOCKET_ERROR)
{
printf("send failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 接收服务器发送的数据
ret = recv(sock, buffer, sizeof(buffer), 0);
if (ret > 0)
{
buffer[ret] = '\0';
printf("Received from server: %s\n", buffer);
}
else
{
printf("recv failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 关闭Socket
closesocket(sock);
// 释放WinSock库资源
WSACleanup();
return 0;
}
```
需要注意的是,服务器端需要在一个循环中不断接受客户端连接请求,并处理客户端发送的数据。客户端需要先连接到服务器,然后发送数据并接收服务器返回的数据。
阅读全文