注:代码在VS2015测试正常
服务器代码如下:
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(void)
{
WSADATA wsaData = { 0 }; //套接字信息结构
SOCKET ServerSocket = INVALID_SOCKET; //服务端套接字
SOCKADDR_IN ServerAddr = { 0 };
SOCKADDR_IN ClientAddr = { 0 };
int iClientAddr = sizeof(ClientAddr);
USHORT usPort = 18000; //服务器端口
//初始化
if (WSAStartup(MAKEWORD(2,2),&wsaData))
{
printf("WSAStartup failed with error code: %d\n",WSAGetLastError());
return -1;
}
//判断版本
if (LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
printf("wVersion not 2.2\n");
return -2;
}
//创建服务器套接字
ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SOCKET_ERROR == ServerSocket)
{
printf("socket failed with error code: %d\n", WSAGetLastError());
return -3;
}
//设置服务器地址
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(usPort);
ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//绑定服务器地址
if (SOCKET_ERROR == bind(ServerSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)))
{
printf("bind failed with error code: %d\n", WSAGetLastError());
closesocket(ServerSocket);
WSACleanup();
return -4;
}
printf("bind successfully......\n");
printf("ready recv message......\n");
char buffer[1024] = { 0 };
int iRecv = 0;
int iSend = 0;
while (1)
{
memset(buffer, 0, sizeof(buffer));
//接收数据
iRecv = recvfrom(ServerSocket, buffer, sizeof(buffer), 0, (SOCKADDR*)&ClientAddr, &iClientAddr);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom failed with error code: %d\n", WSAGetLastError());
closesocket(ServerSocket);
WSACleanup();
return -7;
}
printf("recvfrom client message:%s\n", buffer);
memset(buffer, 0, sizeof(buffer));
printf("Input message:");
gets_s(buffer);
//发送数据
iSend = sendto(ServerSocket, buffer, strlen(buffer), 0, (SOCKADDR*)&ClientAddr, iClientAddr);
if (iSend == SOCKET_ERROR)
{
printf("sendto failed with error code: %d\n", WSAGetLastError());
closesocket(ServerSocket);
WSACleanup();
return -7;
}
}
//关闭,清理
closesocket(ServerSocket);
WSACleanup();
return 0;
}
客户端代码如下:
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(void)
{
WSADATA wsaData = { 0 }; //套接字信息结构
SOCKET ClientSocket = INVALID_SOCKET; //客户端套接字
SOCKADDR_IN ServerAddr = { 0 }; //服务端地址
SOCKADDR_IN ClientAddr = { 0 }; //客户端地址
int iClientAddr = sizeof(ClientAddr);
int iServerAddrSize = sizeof(ServerAddr);
USHORT usPort = 18000; //服务器端口
//初始化
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
printf("WSAStartup failed with error code: %d\n", WSAGetLastError());
return -1;
}
//判断版本
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("wVersion not 2.2\n");
return -2;
}
//创建客户端UDP套接字
ClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SOCKET_ERROR == ClientSocket)
{
printf("socket failed with error code: %d\n", WSAGetLastError());
return -3;
}
//设置服务器地址
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(usPort);
ServerAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
printf("ready send message......\n");
char buffer[1024] = { 0 }; //数据缓冲区
int iSend = 0;
int iRecv = 0;
while (1)
{
memset(buffer, 0, sizeof(buffer));
printf("Input message:");
gets_s(buffer);
//发送数据
iSend = sendto(ClientSocket, buffer, strlen(buffer), 0,
(SOCKADDR*)&ServerAddr, iServerAddrSize);
if (iSend == SOCKET_ERROR)
{
printf("send failed with error code: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return -7;
}
memset(buffer, 0, sizeof(buffer));
//接收数据
iRecv = recvfrom(ClientSocket, buffer, sizeof(buffer), 0, (SOCKADDR*)&ClientAddr, &iClientAddr);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom failed with error code: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return -7;
}
printf("recvfrom server message: %s\n", buffer);
}
//关闭,清理
closesocket(ClientSocket);
WSACleanup();
return 0;
}