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! "; if (_argc <= 1) { printf("Command line no parameter ."); return 0; } printf("%s ", _argv[0]); if (_argv[1][0] == 's') main1(); else if (_argv[1][0] == 'c') main2(); else printf("Unknown command line parameter : %s ", _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! "); if (Ret != 0) { printf("无法初始化winsock. "); WSACleanup(); } else { // printf("初始化winsock成功 "); } 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] = "