Winsock.简单UDP
PS:vs2017 编译C++代码 支持 XP:项目属性-->链接器-->系统-->需要的最小版本--> 输入 "5.1"
1、ZC:测试:c向s 发送长度为0的数据,是可行的。(1)用s和c都在本机 测试,OK (2)用 冬的HP笔记本 和 我的联想小新笔记本 测试,OK
2、测试代码:
// SampleUDP_01.cpp : This file contains the 'main' function. Program execution begins and ends there. // #define _CRT_SECURE_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <iostream> int main1(); int main2(); int main(int _argc, char** _argv) { std::cout << "Hello World!\n"; if (_argc <= 1) { printf("Command line no parameter ."); return 0; } printf("%s\n", _argv[0]); if (_argv[1][0] == 's') main1(); else if (_argv[1][0] == 'c') main2(); else printf("Unknown command line parameter : %s\n", _argv[1]); return 0; } #include <stdio.h> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #define PORT 8888 int main1() { WSADATA wsaData; int Ret = WSAStartup(MAKEWORD(2, 2), &wsaData); printf("Server!\n"); if (Ret != 0) { printf("无法初始化winsock.\n"); WSACleanup(); } else { // printf("初始化winsock成功\n"); } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { WSACleanup(); return 1; } SOCKET socket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); sockaddr_in addr; addr.sin_addr.S_un.S_addr = INADDR_ANY; addr.sin_family = AF_INET; addr.sin_port = ntohs(PORT); if (socket == INVALID_SOCKET) { printf("socket Error!"); } int rs = bind(socket, (sockaddr*)&addr, sizeof(addr)); if (rs != 0) { printf("bind failed!"); return 1; } char buf[200] = "\0"; char temp[200] = "\0"; sockaddr_in addrcl; int len = sizeof(sockaddr); while (1) { int iRecv = recvfrom(socket, buf, sizeof(buf), 0, (sockaddr*)&addrcl, &len);//用新建的地址结构保存客户端的信息 if (iRecv == 0) { printf("recvfrom len : %d\n", iRecv); break; } sprintf(temp, "%s:%s", inet_ntoa(addrcl.sin_addr), buf); sendto(socket, temp, strlen(temp) + 1, 0, (sockaddr*)&addrcl, sizeof(addr)); if (strcmp(buf, "srvexit") == 0) { break; } printf("%s", buf); } closesocket(socket); WSACleanup(); printf("exit\n"); return 0; } #include <WinSock2.h> #include <stdio.h> #define PORT 8888 int main2() { WSADATA wsaData; int Ret = WSAStartup(MAKEWORD(2, 2), &wsaData); printf("Client..!\n"); if (Ret != 0) { printf("无法初始化winsock.\n"); WSACleanup(); } else { // printf("初始化winsock成功\n"); } SOCKET socketc = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); sockaddr_in addr; //addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addr.sin_addr.S_un.S_addr = inet_addr("192.168.2.102"); addr.sin_family = AF_INET; addr.sin_port = ntohs(PORT); if (socketc == INVALID_SOCKET) { printf("socket Error!"); } int len = sizeof(sockaddr); while (1)//client { char buf[200] = "\0"; char rbuf[300] = "\0"; printf("\n:"); scanf("%s", &buf); //sendto(socketc, buf, strlen(buf) + 1, 0, (SOCKADDR*)&addr, len);//用回原来的地址结构 sendto(socketc, buf, 0, 0, (SOCKADDR*)&addr, len);//用回原来的地址结构 break; recvfrom(socketc, rbuf, sizeof(rbuf), 0, (SOCKADDR*)&addr, &len); if (strcmp(rbuf, "exit") == 0) { break; } printf("%s", rbuf); } closesocket(socketc); WSACleanup(); printf("end\n"); return 0; }
3、vs2017 编译C++代码 支持 XP
4、
5、