windows c socket收发数据
时间: 2024-02-24 16:54:46 浏览: 119
在 Windows 平台上使用 C 语言进行 Socket 编程,可以使用 Winsock 库来实现。下面是一个简单的例子,演示如何在客户端和服务器端之间收发数据:
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET sock;
struct sockaddr_in serverAddr;
char sendBuf[1024];
char recvBuf[1024];
int iRet;
// 初始化 Winsock 库
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0) {
printf("WSAStartup failed with error: %d\n", iRet);
return 1;
}
// 创建套接字
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 设置服务器地址
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(8888);
// 连接服务器
iRet = connect(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (iRet != 0) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// 发送数据
sprintf(sendBuf, "hello server");
iRet = send(sock, sendBuf, strlen(sendBuf), 0);
if (iRet == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// 接收数据
iRet = recv(sock, recvBuf, 1024, 0);
if (iRet == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// 处理接收到的数据
recvBuf[iRet] = '\0';
printf("recv: %s\n", recvBuf);
// 关闭套接字和 Winsock 库
closesocket(sock);
WSACleanup();
return 0;
}
```
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET sock;
SOCKET clientSock;
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
char recvBuf[1024];
int iRet;
int addrLen = sizeof(clientAddr);
// 初始化 Winsock 库
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0) {
printf("WSAStartup failed with error: %d\n", iRet);
return 1;
}
// 创建套接字
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 设置服务器地址
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(8888);
// 绑定套接字
iRet = bind(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (iRet == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// 监听端口
iRet = listen(sock, SOMAXCONN);
if (iRet == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
printf("waiting for client...\n");
// 接受连接
clientSock = accept(sock, (struct sockaddr *)&clientAddr, &addrLen);
if (clientSock == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
printf("client connected: %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 接收数据
iRet = recv(clientSock, recvBuf, 1024, 0);
if (iRet == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
closesocket(sock);
WSACleanup();
return 1;
}
// 处理接收到的数据
recvBuf[iRet] = '\0';
printf("recv: %s\n", recvBuf);
// 发送数据
sprintf(recvBuf, "hello client");
iRet = send(clientSock, recvBuf, strlen(recvBuf), 0);
if (iRet == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
closesocket(sock);
WSACleanup();
return 1;
}
// 关闭套接字和 Winsock 库
closesocket(clientSock);
closesocket(sock);
WSACleanup();
return 0;
}
```
阅读全文