1 写socket的时候UDP和TCP的代码区别就是 是否有连接过程;有connect连接的代码的就是TCP,没有连接的就是UDP
以下代码是发送信息给组播地址(没有写接收代码。接收的代码就是要写个加入多播组,从多播组接收的逻辑)
参考:https://blog.csdn.net/zhizhengguan/article/details/109312144
// UDP_socket.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <stdio.h> #include <stdlib.h> #if defined(_WIN32) #include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #pragma comment (lib, "Ws2_32.lib") #pragma comment(lib,"Iphlpapi.lib") #else #include <netdb.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> #include <netinet/in.h> #include <netinet/tcp.h> #endif // defined(_WIN32) int main() { char group_addr[16] = "224.1.3.250"; //初始化WinSock.否则socket(AF_INET, SOCK_DGRAM, 0)会一直返回-1 WSADATA WSAData; if (WSAStartup(MAKEWORD(2, 0), &WSAData) != 0) { return FALSE; } int socked = ::socket(AF_INET, SOCK_DGRAM, 0); if (socked < 0) { perror("socket failed!"); return 2; } struct sockaddr_in remote_addr; memset(&remote_addr, 0, sizeof(remote_addr)); remote_addr.sin_family = AF_INET; inet_pton(AF_INET, group_addr, &remote_addr.sin_addr.s_addr); remote_addr.sin_port = htons(25666); char buf[200] = "1234567890"; int length = 0; int i = 0; while (1) { i++; length = sendto(socked, buf, strlen(buf), 0, (struct sockaddr*)&remote_addr, sizeof(remote_addr)); printf("Send Message%s,%d\n", buf,i); } closesocket(socked); return 0; }
标签:UDP,组播,remote,addr,c++,socked,include,socket From: https://www.cnblogs.com/8335IT/p/17382780.html