运行环境:
windows,VS2019,
用VS新建两个工程,一个用来做服务器,一个做客户端
直接上代码
client.c
#include <winsock.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib");
#define MAXLINE 4096 //接收缓冲区长度
#define SERVER_PORT 13 //时间同步服务器端口号
typedef struct sockaddr sockaddr;
int main(int argc, char* argv[])
{
char a[MAXLINE];
printf("请输入a:");
scanf_s("%s", &a);
char buff[MAXLINE];
SOCKET ConnectSocket = INVALID_SOCKET;
int iResult;
int senda;
int sendb;
struct sockaddr_in servaddr;
char recvline[MAXLINE + 1]; //初始化Windows Sockets DLL;协商版本号
WORD wVersionRequested;
WSADATA wsaData; //使用MAKEWORD(lowbyte, highbyte) 宏,在windef.h中声明
wVersionRequested = MAKEWORD(2, 2);
iResult = WSAStartup(wVersionRequested, &wsaData);
if (iResult != 0)
{
printf("WSAStartup函数调用错误,错误号:%d\n", WSAGetLastError());
return 1;
}
else
{
printf("初始化成功!\n");
}
//创建流式套接字
if ((ConnectSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket函数调用失败,错误号:%d\n", WSAGetLastError());
WSACleanup();
return 1;
}
//服务器地址赋值
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");;
servaddr.sin_port = htons(SERVER_PORT);
//向服务器请求连接
iResult = connect(ConnectSocket, (sockaddr*)&servaddr, sizeof(servaddr));
if (iResult == SOCKET_ERROR)
{
printf("connect 函数调用错误,错误号:%d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
memset(buff, 0, sizeof(buff));
sprintf(buff, "%s", a);
senda= send(ConnectSocket, buff, strlen(buff), 0);
//持续接收数据,直到服务器关闭连接
memset(&recvline, 0, sizeof(recvline));
printf("接收到来自服务器的数据:");
do {
iResult = recv(ConnectSocket, recvline, MAXLINE, 0);
if (iResult > 0)
printf("%s\n", recvline);
else
{
if (iResult == 0)
printf("对方连接关闭,退出\n");
else
printf("recv 函数调用错误,错误号:%d\n", WSAGetLastError());
}
} while (iResult > 0);
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
server.c
#include <time.h>
#include <winsock.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib");
#define MAXLINE 4096 //接收缓冲区长度
#define LISTENQ 1024 //监听队列长度
#define SERVER_PORT 13 //时间同步服务器端口号
int main(int argc, char* argv[])
{
SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;
int iResult;
int iResult1;
int iResult2;
char recva[MAXLINE + 1];
struct sockaddr_in servaddr;
char buff[MAXLINE]; time_t ticks;
int iSendResult; //初始化Windows Sockets DLL;协商版本号
WORD wVersionRequested;
WSADATA wsaData; //使用MAKEWORD(lowbyte, highbyte) 宏,在windef.h中声明
wVersionRequested = MAKEWORD(2, 2);
iResult = WSAStartup(wVersionRequested, &wsaData);
if (iResult != 0)
{
printf("WSAStartup函数调用错误,错误号:%d\n", WSAGetLastError());
return 1;
}
else
{
printf("初始化成功!\n");
}
//创建流式套接字
if ((ListenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket函数调用失败,错误号:%d\n", WSAGetLastError());
WSACleanup();
return 1;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1 ");
servaddr.sin_port = htons(SERVER_PORT);
//绑定服务器地址
iResult = bind(ListenSocket, (struct sockaddr*)&servaddr, sizeof(servaddr));
if (iResult == SOCKET_ERROR)
{
printf("bind 函数调用错误,错误号:%d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
//设置服务器位监听状态,监听队列长度为LISTENQ
iResult = listen(ListenSocket, LISTENQ);
if (iResult == SOCKET_ERROR)
{
printf("listen 函数调用错误,错误号:%d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
while (1)
{
//接收客户端连接请求,返回连接套接字ClientSocket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
printf("accept 函数调用错误,错误号:%d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
memset(&recva, 0, sizeof(recva));
iResult1 = recv(ClientSocket, recva, MAXLINE, 0);
printf("接收到来自客户端的数据%s,", recva);
//发送
iSendResult = send(ClientSocket, recva, strlen(recva), 0);
if (iSendResult == SOCKET_ERROR)
{
printf("send 函数调用错误,错误号:%d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("计算成功,已发送至客户端\n");
}
//关闭套接字
closesocket(ClientSocket);
printf("主动关闭连接\n");
closesocket(ListenSocket);
WSACleanup();
return 0;
}