签到天数: 1 天 [LV.1]无主之魂
初入阿拉德
- 积分
- 14
- 怒气
- 1
- 声望
- 5
- 战力
- 0
|
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <WinSock2.h>
- #pragma comment(lib, "ws2_32.lib")
- int main()
- {
- char szIp[32];
- unsigned short uPort;
- struct
- {
- char tag;
- char str[80];
- char len;
- } buf;
- // 结构长度是固定的, 内容不能超过80个字
- buf.tag = 3;
- strcpy (buf.str, "Hello!");
- std::cout << "IP:";
- //IP是频道的IP, 也就是df_game_r监听的IP
- std::cin >> szIp;
- std::cout << "Port:";
- //端口是UDP的, 别搞错了
- std::cin >> uPort;
- std::cout << "Text:";
- std::cin >> buf.str;
- buf.len = strlen (buf.str);
- WSADATA wsaData;
- if (WSAStartup (MAKEWORD (2, 2), &wsaData))
- {
- getchar ();
- return 0;
- }
- SOCKET soSend = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (soSend == SOCKET_ERROR)
- {
- WSACleanup ();
- getchar ();
- return 0;
- }
- SOCKADDR_IN siLocal;
- memset (&siLocal, 0, sizeof (SOCKADDR_IN));
- siLocal.sin_family = AF_INET;
- siLocal.sin_port = htons (uPort);
- siLocal.sin_addr.s_addr = inet_addr (szIp);
- while (1)
- {
- sendto (soSend, (const char*)&buf, sizeof (buf), 0, (SOCKADDR*)&siLocal, sizeof (SOCKADDR));
- Sleep (10);
- }
- closesocket (soSend);
- WSACleanup ();
- return 0;
- }
复制代码
只要服务器不封频道的UDP端口, 好像任何人都可以广播(不确定)
复制到cpp里,用vs编译出来就行
|
|