资源简介
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
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- 通信软件的具体实例──基于Socket的
- 用Socket编程实现FTP
- websocket实现一对一聊天
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- 代码客:G-TcpServer(IOCP) 1.0 正式版及
- linux下用多进程同步方法解决生产者
- SOCKET 网络编程 计算机网络 作业 客户
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- 用Socket写的简易FTP服务器和客户端
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- socket小工具(可方便建立TCP UDP的soc
评论
共有 条评论