linux socket UDP编程发送广播
2011-04-11 07:49
471 查看
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include
<arpa/inet.h>
#include<netdb.h>
#define PORT
7773
#define
MAXDATASIZE 256
int main(int
argc,char *argv[])
{
int socket_fd;
struct sockaddr_in my_addr,user_addr;
char buf[MAXDATASIZE];
int so_broadcast=1;
socklen_t size;
char my_ip[12];
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(PORT);
my_addr.sin_addr.s_addr=inet_addr("255.255.255.255");
bzero(&(my_addr.sin_zero),8);
user_addr.sin_family=AF_INET;
user_addr.sin_port=htons(PORT);
user_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(user_addr.sin_zero),8);
if((socket_fd=(socket(AF_INET,SOCK_DGRAM,0)))==-1)
{
perror("socket");
exit(1);
}
setsockopt(socket_fd,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast));
if((bind(socket_fd,(struct sockaddr
*)&user_addr,
sizeof(struct sockaddr)))==-1) {
perror("bind");
exit(1);
}
strcpy(buf,"Hello,I'm on
line!");
sendto(socket_fd,buf,strlen(buf),0,(struct
sockaddr *)&my_addr,sizeof(my_addr));
size=sizeof(user_addr);
recvfrom(socket_fd,buf,MAXDATASIZE,0,(struct
sockaddr *)&user_addr,&size);
strcpy(my_ip,inet_ntoa(user_addr.sin_addr));
printf("my_ip:%sn",inet_ntoa(user_addr.sin_addr));
while(1) {
bzero(buf, sizeof(buf));
size=sizeof(user_addr);
recvfrom(socket_fd,buf,MAXDATASIZE,0,(struct
sockaddr *)&user_addr,&size);
printf("%s:%sn",inet_ntoa(user_addr.sin_addr),buf);
bzero(buf, sizeof(buf));
if(strcmp(buf,"I'm off line,bye!")==0)
strcpy(buf,"ok,I
know,bye!");
else {
strcpy(buf,"send,Hello,I get
you!");
sleep(1);
}
if((sendto(socket_fd,buf,strlen(buf),0,
(struct sockaddr *)&user_addr,sizeof(user_addr)))==-1)
perror("sendto");
}
return 0;
}
执行:
gcc -o udp
udp.c
./udp
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include
<arpa/inet.h>
#include<netdb.h>
#define PORT
7773
#define
MAXDATASIZE 256
int main(int
argc,char *argv[])
{
int socket_fd;
struct sockaddr_in my_addr,user_addr;
char buf[MAXDATASIZE];
int so_broadcast=1;
socklen_t size;
char my_ip[12];
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(PORT);
my_addr.sin_addr.s_addr=inet_addr("255.255.255.255");
bzero(&(my_addr.sin_zero),8);
user_addr.sin_family=AF_INET;
user_addr.sin_port=htons(PORT);
user_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(user_addr.sin_zero),8);
if((socket_fd=(socket(AF_INET,SOCK_DGRAM,0)))==-1)
{
perror("socket");
exit(1);
}
setsockopt(socket_fd,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast));
if((bind(socket_fd,(struct sockaddr
*)&user_addr,
sizeof(struct sockaddr)))==-1) {
perror("bind");
exit(1);
}
strcpy(buf,"Hello,I'm on
line!");
sendto(socket_fd,buf,strlen(buf),0,(struct
sockaddr *)&my_addr,sizeof(my_addr));
size=sizeof(user_addr);
recvfrom(socket_fd,buf,MAXDATASIZE,0,(struct
sockaddr *)&user_addr,&size);
strcpy(my_ip,inet_ntoa(user_addr.sin_addr));
printf("my_ip:%sn",inet_ntoa(user_addr.sin_addr));
while(1) {
bzero(buf, sizeof(buf));
size=sizeof(user_addr);
recvfrom(socket_fd,buf,MAXDATASIZE,0,(struct
sockaddr *)&user_addr,&size);
printf("%s:%sn",inet_ntoa(user_addr.sin_addr),buf);
bzero(buf, sizeof(buf));
if(strcmp(buf,"I'm off line,bye!")==0)
strcpy(buf,"ok,I
know,bye!");
else {
strcpy(buf,"send,Hello,I get
you!");
sleep(1);
}
if((sendto(socket_fd,buf,strlen(buf),0,
(struct sockaddr *)&user_addr,sizeof(user_addr)))==-1)
perror("sendto");
}
return 0;
}
执行:
gcc -o udp
udp.c
./udp
相关文章推荐
- Java网络编程笔记--实现UDP广播发送与接收
- linux socket UDP编程发送广播
- linux socket UDP编程发送广播
- Android使用wifi通过UDP协议发送广播数据包给PC接收不到问题解决方法
- Java基础知识强化之网络编程笔记03:UDP之UDP协议发送数据 和 接收数据
- uip UDP 服务器广播模式(客户端可以任意端口,并且主动向客户端发送数据)
- Linux系统下UDP发送和接收广播消息小例子
- Android UDP广播 客户端循环发送与接收
- ios开发之发送UDP广播并接收数据
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
- Linux网络编程 之 UDP 广播 .
- UDP广播包的发送问题
- Linux系统下UDP发送和接收广播消息小例子
- Linux系统下UDP发送和接收广播消息小例子
- linux下网络编程3:UDP广播传输文件
- UDP编程补充——UDP广播程序的实现
- java发送udp广播包
- 第14章 UDP编程(3)_利用UDP实现广播功能
- C#实现局域网UDP广播------C#网络编程杂记[1]
- 猎豹MFC--Socket编程基础UDP发送端