1 Star 0 Fork 0

上海市虹口区青少年活动中心/socket-management

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Server.cpp 992 Bytes
一键复制 编辑 原始数据 按行查看 历史
TianC-Wang 提交于 2020-11-28 22:07 . First Version
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "WS2_32.lib")
int main(void) {
int nSize = sizeof(sockaddr);
WSADATA wData = { 0 };
char* buffer = new char[256];
std::cout << "Server End Executive Program:" << std::endl << std::endl;
WSAStartup(MAKEWORD(2, 2), &wData);
SOCKET sockSer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN addrCli, addrSer = { 0 };
addrSer.sin_family = AF_INET;
addrSer.sin_port = htons(10101);
bind(sockSer, (sockaddr*)&addrSer, sizeof(addrSer));
while (true) {
time_t t = 0;
memset(buffer, 0, 256);
recvfrom(sockSer, buffer, 255, 0, (sockaddr*)&addrCli, &nSize);
time(&t);
std::cout << localtime(&t)->tm_hour << ":" << localtime(&t)->tm_min << " from: " << inet_ntoa(addrCli.sin_addr) <<" :" << std::endl;
std::cout << buffer << std::endl << std::endl;
}
delete[] buffer;
closesocket(sockSer);
WSACleanup();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/HKQQSNHDZX/socket-management.git
git@gitee.com:HKQQSNHDZX/socket-management.git
HKQQSNHDZX
socket-management
socket-management
master

搜索帮助