前两次的socket网络编程程序都是基于TCP 的,而这次使用UDP非面向连接的协议来写的,只管发送就可以了,在发送数据量小的情况下非常有效率,发送的这些数据不能是重要的数据,因为可能发生数据丢失、损毁等情况。Talk is cheap , show me the code.
server.c
#include
#include
#include
#define RECV_BUF_SIZE 1024
#pragma comment(lib,"ws2_32.lib")
int main(){
WSADATA wsd;
SOCKET sServer;
SOCKADDR_IN addrServer, addrClient;
int recvCount = 100;
int sizeAddrClient = 0;
char recvBuf[RECV_BUF_SIZE];
memset(recvBuf, 0, RECV_BUF_SIZE);
//初始化socket库
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){
ErrorHandling("WSAStartup error!");
}
puts("初始化socket库");
//创建服务端socket
sServer = socket(AF_INET, SOCK_DGRAM, 0);
if (sServer == INVALID_SOCKET){
ErrorHandling("socket() error!");
}
puts("创建服务端socket");
//初始化网络地址信息
memset(&addrServer, 0, sizeof(addrServer));
addrServer.sin_family = AF_INET;
addrServer.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
addrServer.sin_port = htons(6000);
puts("初始化网络地址信息");
//绑定地址信息到服务端socket
if (bind(sServer, (SOCKADDR*)&addrServer, sizeof(addrServer)) == SOCKET_ERROR){
ErrorHandling("bind() error!");
}
printf("%d",WSAGetLastError());
puts("绑定地址信息到服务端socket");
sizeAddrClient = sizeof(SOCKADDR);
//循环接收消息(我规定最多为200次)
while (recvCount>0){
int recvNum = recvfrom(sServer, recvBuf, RECV_BUF_SIZE,0, (SOCKADDR*)&addrClient, &sizeAddrClient);
printf("%d",recvNum);
sendto(sServer, recvBuf, RECV_BUF_SIZE, 0, (SOCKADDR*)&addrClient, sizeAddrClient);
printf("Echo:%s\n", recvBuf);
memset(recvBuf, 0, 1024);
recvCount--;
}
//关闭socket
closesocket(sServer);
WSACleanup();
puts("关闭socket");
system("pause");
return 0;
}
client.c
#include不明白的地方,请回复。#include #include #pragma comment(lib,"ws2_32.lib") void ErrorHandling(char * msg){ puts(msg); system("pause"); exit(1); } int main(){ WSADATA wsd; SOCKET sClient; SOCKADDR_IN addrServer; int sizeaddrServer = 0; int recvCount = 100; char charBuf[1024] = { 0 }; //初始化socket库 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){ ErrorHandling("WSAStartup error!"); } puts("初始化socket库"); //创建客户端socket sClient = socket(AF_INET, SOCK_DGRAM, 0); if (sClient == INVALID_SOCKET){ ErrorHandling("socket() error!"); } puts("创建客户端socket"); //初始化网络地址信息 memset(&addrServer, 0, sizeof(addrServer)); addrServer.sin_family = AF_INET; addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addrServer.sin_port = htons(6000); puts("初始化网络地址信息"); //连接服务端 if (connect(sClient, (SOCKADDR*)&addrServer, sizeof(addrServer)) == SOCKET_ERROR){ ErrorHandling("connect error!"); } puts("现在可以向服务端发送消息"); //循环发送消息(我规定最多为200次) int recvNum = 0; char charRecv[1024]; char recvBuf[1024]; memset(recvBuf, 0, 1024); memset(charRecv, 0, 1024); int addrSize=sizeof(SOCKADDR); while (recvCount>0){ memset(charBuf, 0, 1024); memset(recvBuf, 0, 1024); printf("To:"); gets(charBuf); int sendLen = sendto(sClient, charBuf, 1024,0,(SOCKADDR*)&addrServer,sizeof(SOCKADDR)); int recvLen = recvfrom(sClient, recvBuf, 1024, 0, (SOCKADDR*)&addrServer, &addrSize); if (recvLen==-1){ printf("服务器未响应\n"); } else{ printf("来自服务器的消息:%s\n",recvBuf); } recvCount--; } //关闭socket closesocket(sClient); WSACleanup(); puts("关闭socket"); system("pause"); return 0; }