Windows网络通信UDP基础例程

  • I8_161309
    了解作者
  • 36.8MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-15 05:26
    上传日期
Windows网络通信UDP基础例程,代码里有详尽的注释,很适合初学者阅读
UDP_CS.zip
  • UDPClient
  • Release
  • mt.write.1.tlog
    212B
  • UDPClient.exe.intermediate.manifest
    381B
  • link.command.1.tlog
    1.2KB
  • cl.command.1.tlog
    586B
  • link.read.1.tlog
    2.8KB
  • UDPClient.lastbuildstate
    53B
  • UDPClient.write.1.tlog
    0B
  • CL.read.1.tlog
    21.1KB
  • vc100.pdb
    260KB
  • mt.command.1.tlog
    396B
  • Client.obj
    685.4KB
  • UDPClient.log
    1.9KB
  • link.write.1.tlog
    370B
  • CL.write.1.tlog
    214B
  • mt.read.1.tlog
    464B
  • Client.cpp
    899B
  • UDPClient.vcxproj
    3.8KB
  • UDPClient.vcxproj.user
    143B
  • UDPClient.vcxproj.filters
    944B
  • UDPServer
  • ipch
  • udpclient-264a79c6
  • udpclient-c612032e.ipch
    42.9MB
  • udpserver-6e0da472
  • udpserver-2d1fe446.ipch
    42.9MB
  • udpserver-f4e2e1cc.ipch
    43.8MB
  • Release
  • UDPClient.exe
    9KB
  • UDPServer.pdb
    435KB
  • UDPServer.exe
    9KB
  • UDPClient.pdb
    435KB
  • UDPServer
  • Release
  • UDPServer.exe.intermediate.manifest
    381B
  • mt.write.1.tlog
    232B
  • link.command.1.tlog
    1.2KB
  • cl.command.1.tlog
    626B
  • link.read.1.tlog
    2.8KB
  • CL.read.1.tlog
    21.2KB
  • vc100.pdb
    260KB
  • UDPServer.log
    2KB
  • mt.command.1.tlog
    416B
  • Server.obj
    686.1KB
  • UDPServer.lastbuildstate
    53B
  • UDPServer.write.1.tlog
    0B
  • link.write.1.tlog
    410B
  • CL.write.1.tlog
    274B
  • mt.read.1.tlog
    484B
  • Debug
  • vc100.idb
    515KB
  • cl.command.1.tlog
    606B
  • CL.read.1.tlog
    21.3KB
  • vc100.pdb
    276KB
  • UDPServer.log
    749B
  • UDPServer.vcxprojResolveAssemblyReference.cache
    713B
  • Server.obj
    64.7KB
  • UDPServer.write.1.tlog
    0B
  • CL.write.1.tlog
    266B
  • Server.cpp
    1.1KB
  • UDPServer.vcxproj.user
    143B
  • UDPServer.vcxproj
    3.8KB
  • UDPServer.vcxproj.filters
    944B
  • Debug
  • UDPServer.suo
    13KB
  • UDPServer.sdf
    29.3MB
  • UDPServer.sln
    1.3KB
内容介绍
#include <WinSock2.h> #include <stdio.h> #include <iostream> #pragma comment(lib, "WS2_32.lib") using namespace std; int main() { //初始化WS2_32库 WORD wVersionRequested = MAKEWORD(2, 0); WSADATA data; WSAStartup(wVersionRequested, &data); //创建socket SOCKET s; s = socket(AF_INET, SOCK_DGRAM, 0); //绑定socket sockaddr_in addr1, addr2; int n = sizeof(addr2); addr1.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addr1.sin_port = htons(88); addr1.sin_family = AF_INET; bind(s, (sockaddr*)&addr1, sizeof(addr1)); cout << "服务器已启动!" << endl; char strsend[] = "你好,我是服务器!"; char strrecv[50]; //数据收发 while (1) { if (::recvfrom(s, strrecv, sizeof(strrecv), 0, (sockaddr*)&addr2, &n) != 0) { cout << "客户端IP : " << inet_ntoa(addr2.sin_addr) << " 已连接!" << endl; cout << strrecv << endl; sendto(s, strsend, sizeof(strsend), 0, (sockaddr*)&addr2, n); break; } } ::closesocket(s); ::WSACleanup(); if (getchar()) { return 0; } else { ::Sleep(500); } }
评论
    相关推荐