udp組播示例 傳送 接收

2021-10-24 04:23:05 字數 3350 閱讀 6338

原因不多說,目錄結構扁平,

data.h如下:

#pragma once

#include #include #include #define multicast_ip "234.0.0.1"

#define multicast_port 28928

#pragma pack(1)

typedef struct price pricetype;

struct sse_marketdata_stock

uint64_t seq_num;

u_char mdstreamid[8];

u_char timestamp[8];

u_char secutrityid[8];

u_char symbol[8];

uint64_t tradevolume;

pricetype totalvaluetraded;

pricetype precloseprice;

pricetype prevsetprice;

uint64_t totallongposition;

pricetype indexvalue;

pricetype openprice;

pricetype closeprice;

pricetype highprice;

pricetype lowprice;

u_char tradingphasecode[8];

u_char reserved[24];

};#pragma pack()

multi_cast_send.cpp如下:

#include #include #include #include #include #include #include #include #include #include "data.h"

using namespace std;

int main(int argc, char *ar**)

memset(&mcast_addr, 0, sizeof(mcast_addr));

mcast_addr.sin_family = af_inet;

mcast_addr.sin_addr.s_addr = inet_addr(multicast_ip);

mcast_addr.sin_port = htons(multicast_port);

docout<<"send to["#include #include #include #include #include #include #include "data.h"

using namespace std;

int main(int argc,char* ar**)

int on = 1;

/* 設定位址復用許可, 根據具體情況判斷是否增加此功能 */

if (setsockopt(fd, sol_socket, so_reuseaddr, &on, sizeof(on)) < 0)

/* 初始化位址 */

memset(&addr, 0, sizeof(addr));

addr.sin_family = af_inet;

addr.sin_addr.s_addr = htonl(inaddr_any);

addr.sin_port = htons(multicast_port);

if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)

/*加入多播組*/

mreq.imr_multiaddr.s_addr = inet_addr(multicast_ip);

mreq.imr_inte***ce.s_addr = htonl(inaddr_any);

if (setsockopt(fd, ipproto_ip, ip_add_membership, &mreq, sizeof(mreq)) < 0)

int addrlen = sizeof(addr);

sse_marketdata_stock recv_data;

int nbytes;

while(true)

cout<<"recv from ["《最後,cmakelists.txt如下:

cmake_minimum_required( version 3.8 fatal_error)

project(main version 1.0.0 languages cxx)

set(cmake_export_compile_commands on)

#set dirs

set(project_root $)

message("project dir:$")

set(bin_destination $/bin)

set(cmake_archive_output_directory $)

set(cmake_library_output_directory $)

set(cmake_runtime_output_directory $)

#set compile flags

set(cmake_cxx_flags "-g3 -rdynamic -std=c++11")

set(cmake_cxx_flags_debug "-o0 -fsanitize=address -fno-omit-frame-pointer -fsanitize=leak")

set(cmake_cxx_flags_release "-o3 -dndebug")

#include dirs

include_directories(./)

#link dirs

link_directories($)

#execute

set(send_src multi_cast_send.cpp)

add_executable(send $)

set_target_properties(send properties version $)

set (recv_src multi_cast_recv.cpp)

add_executable(recv $)

set_target_properties(recv properties version $)

在當前上當,建立build目錄,進入build目錄,執行:

cmake .. -dcmake_build_type=debug && make clean && make verbose=1

會在bin目錄生成send和recv可執行檔案,即可演示組播

UDP組播接收

網路中的一台主機如果希望能夠接收到來自網路中其它主機發往某乙個組播組的資料報,那麼這麼主機必須先加入該組播組,然後就可以從組位址接收資料報。在廣域網中,還涉及到路由器支援組播路由等,但本文希望以乙個最為簡單的例子解釋清楚協議棧關於組播的乙個最為簡單明瞭的工作過程,甚至,我們不希望涉及到 igmp包。...

UDP組播接收端解析

網路中的一台主機如果希望能夠接收到來自網路中其它主機發往某乙個組播組的資料報,那麼這麼主機必須先加入該組播組,然後就可以從組位址接收資料報。在廣域網中,還涉及到路由器支援組播路由等,但本文希望以乙個最為簡單的例子解釋清楚協議棧關於組播的乙個最為簡單明瞭的工作過程,甚至,我們不希望涉及到 igmp包。...

UDP通訊 聊天示例(接收端 傳送端)

1 傳送send 2 接收端receive 3 接收方獲取ip和埠號 4 傳送端 實現 public static void main string args throws exception5 接收端 實現 public static void main string args throws ex...