在Windows环境下C语言通过socket客户端向服务端发送整型一维数组并计算和返回结果给客户端,请给出具体代码
时间: 2024-02-19 15:59:22 浏览: 76
C语言实现socket服务端和客户端
4星 · 用户满意度95%
客户端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") //链接ws2_32.lib库
#define BUF_SIZE 1024 //缓冲区大小
int main()
{
//初始化Winsock库
WSADATA wsaData;
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;
}
//设置服务端地址信息
SOCKADDR_IN serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //服务端IP地址
serverAddr.sin_port = htons(12345); //服务端端口号
//连接服务端
if (connect(sock, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("connect failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
//发送整型一维数组到服务端
int arr[] = {1, 2, 3, 4, 5};
int arrSize = sizeof(arr) / sizeof(int);
char sendBuf[BUF_SIZE];
int sendLen = sprintf_s(sendBuf, BUF_SIZE, "%d ", arrSize);
for (int i = 0; i < arrSize; ++i)
{
sendLen += sprintf_s(sendBuf + sendLen, BUF_SIZE - sendLen, "%d ", arr[i]);
}
send(sock, sendBuf, sendLen, 0);
//接收服务端返回的结果
char recvBuf[BUF_SIZE];
int recvLen = recv(sock, recvBuf, BUF_SIZE, 0);
if (recvLen > 0)
{
recvBuf[recvLen] = '\0';
int sum = atoi(recvBuf);
printf("The sum is %d\n", sum);
}
//关闭套接字
closesocket(sock);
//释放Winsock库
WSACleanup();
return 0;
}
```
服务端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") //链接ws2_32.lib库
#define BUF_SIZE 1024 //缓冲区大小
int main()
{
//初始化Winsock库
WSADATA wsaData;
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;
}
//绑定本地地址
SOCKADDR_IN localAddr;
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = INADDR_ANY;
localAddr.sin_port = htons(12345); //本地端口号
if (bind(sock, (SOCKADDR*)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
{
printf("bind failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
//监听客户端连接请求
if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
{
printf("listen failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
//等待客户端连接
SOCKADDR_IN clientAddr;
int addrLen = sizeof(clientAddr);
SOCKET clientSock = accept(sock, (SOCKADDR*)&clientAddr, &addrLen);
if (clientSock == INVALID_SOCKET)
{
printf("accept failed!\n");
closesocket(sock);
WSACleanup();
return 1;
}
//接收客户端发送的整型一维数组
char recvBuf[BUF_SIZE];
int recvLen = recv(clientSock, recvBuf, BUF_SIZE, 0);
if (recvLen > 0)
{
recvBuf[recvLen] = '\0';
int arrSize = 0;
int arr[BUF_SIZE];
char* p = strtok(recvBuf, " ");
if (p)
{
arrSize = atoi(p);
int i = 0;
while ((p = strtok(NULL, " ")) && i < arrSize)
{
arr[i++] = atoi(p);
}
}
int sum = 0;
for (int i = 0; i < arrSize; ++i)
{
sum += arr[i];
}
char sendBuf[BUF_SIZE];
sprintf_s(sendBuf, BUF_SIZE, "%d", sum);
send(clientSock, sendBuf, strlen(sendBuf), 0);
}
//关闭套接字
closesocket(clientSock);
closesocket(sock);
//释放Winsock库
WSACleanup();
return 0;
}
```
阅读全文