资源简介
Linux下组播源码,含客户端和服务器
接收端 zb_recv.c
发送端 zb_recv.c
代码片段和文件信息
#include
#include
#include
#include
#include
#include
//#define PORTNUM 5000
//#define GROUPIP “224.0.1.1“
#define PORTNUM 1900
#define GROUPIP “239.255.255.250“
#define BUFLEN 1024
#define TESTNUM 10
int main()
{
int sock_id;
struct sockaddr_in addr sender;
struct ip_mreq ipmr;
char buf[BUFLEN];
socklen_t len;
int ret;
int count;
/* Step 1: open a socket and bind */
if ((sock_id = socket(AF_INET SOCK_DGRAM 0)) < 0)
{
perror(“socket error“);
exit(1);
}
memset((void*)&addr 0 sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(PORTNUM);
if (bind(sock_id (struct sockaddr *)&addr sizeof(addr)) < 0)
{
perror(“bind error“);
exit(1);
}
/* Step 2: fill in a struct ip_mreq */
memset((void*)&ipmr 0 sizeof(ipmr));
ipmr.imr_multiaddr.s_addr = inet_addr(GROUPIP); /* multicast group ip */
ipmr.imr_interface.s_addr = htonl(INADDR_ANY);
printf(“ip :%s\n“inet_ntoa(ipmr.imr_interface.s_addr));
/* Step 3: call setsockopt with IP_ADD_MEMBERSHIP to support receiving multicast */
if (setsockopt(sock_id IPPROTO_IP IP_ADD_MEMBERSHIP &ipmr sizeof(ipmr)) < 0)
{
perror(“setsockopt:IP_ADD_MEMBERSHIP“);
exit(1);
}
/* Step 4: call recvfrom to receive multicast packets */
len = sizeof(sender);
count = 0;
while (count < TESTNUM)
{
ret = recvfrom(sock_id buf BUFLEN 0 (struct sockaddr *)&sender &len);
buf[ret] = ‘\0‘;
if (ret < 0)
{
perror(“recvfrom error“);
exit(1);
}
printf(“addr ip :%s\n“inet_ntoa(ipmr.imr_interface.s_addr));
printf(“ip :%s\n“inet_ntoa(ipmr.imr_interface.s_addr));
printf(“%d. Receive from %s:%u\n“ count
inet_ntoa(sender.sin_addr.s_addr) ntohs(sender.sin_port));
printf(“\tpacket data: %s\n“ buf);
}
/* Step 5: call setsockopt with IP_DROP_MEMBERSHIP to drop from multicast */
if (setsockopt(sock_id IPPROTO_IP IP_DROP_MEMBERSHIP &ipmr sizeof(ipmr)) < 0)
{
perror(“setsockopt:IP_DROP_MEMBERSHIP“);
exit(1);
}
/* Step 6: close the socket */
close(sock_id);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7671 2013-05-12 10:01 zb_socket\zb_recv
文件 2227 2013-05-12 10:01 zb_socket\zb_recv.c
文件 7469 2013-05-12 09:36 zb_socket\zb_send
文件 1150 2013-04-24 12:56 zb_socket\zb_send.c
文件 132 2010-08-07 11:05 zb_socket\说明.txt
目录 0 2013-07-15 17:15 zb_socket
----------- --------- ---------- ----- ----
18649 6
- 上一篇:计算机毕业论文参考文献(中英文对照)
- 下一篇:libsvm-3.20.zip
相关资源
- 温湿度传感器DHT11在mini2440上基于lin
- UBUNTU LINUX教程
- 使用socket编程模拟滑动窗口协议
- Socket 进程通信(类似QQ的聊天程序)
- winsocket函数大全-中文
- 向指定ip端口发送数据并等待接收
- linux内核裁剪具体步骤的图解及细解
- socket编程 TCP文件的传输实现 客户端和
- LINUX-FTP服务包 vsftpd-2.0.1-5.src.rpm
- 大恒相机QT例程LINUX
- vnc-3.3.7-x86_linux
- SecureCRTTomcat工程发布到linux系统上
- dnw-linux-v1.01.tar.gz
- Linux下基于epoll_线程池高并发服务器实
- udp 多线程实现多客户端并发,并采用
- .net websocket 消息推送
- AD9361驱动
- Linux/Windows局域网远程开机唤醒的例程
- linux系统调用表
- ap6255 firmware
- Linux内核视频教程.txt
- linux 语音实时对讲
- Linux网络编程(四)——epoll+多线程实
- LINUX操作系统文件系统设计实验代码
- QT Qudpsocket 传送结构体数据
-
Socket xm
l 测试 - Socket通信.zip
- TCP/UDP Socket调试工具
- WSL2使用xrdp连接xfce4桌面详细配置教程
- chat_room.zip
评论
共有 条评论