linux socket UDP编程发送广播
2012-06-16 15:40
609 查看
郁闷了几天,终于弄了出来,现在已经能发送广播了。
#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:%s\n",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:%s\n",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
本文出自 “prothes blog” 博客,请务必保留此出处http://prothesman.blog.51cto.com/8610862/1411530
#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:%s\n",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:%s\n",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
本文出自 “prothes blog” 博客,请务必保留此出处http://prothesman.blog.51cto.com/8610862/1411530
相关文章推荐
- linux socket UDP编程发送广播
- 【Linux网络编程】原始套接字实例:发送 UDP 数据包
- linux下socket编程-UDP
- 黑马程序员--Java基础学习之网络编程(TCP、UDP、Socket、模拟发送和接收数据)
- Linux socket编程:udp
- Linux下Socket编程中用send发送结构体
- linux网络编程Socket之TCP与UDP
- Linux系统下UDP发送和接收广播消息小例子
- linux socket编程之TCP与UDP
- Linux编程之UDP SOCKET全攻略
- Linux下Socket编程中用send发送结构体
- Udp广播的发送和接收(iOS + AsyncUdpSocket)下篇
- Linux 编程之 【socket】 udp
- Linux Socket 学习笔记19-UDP广播服务器端
- 基于UDP协议的网络编程(使用DatagramSocket发送接收数据)
- linux socket编程示例——UDP
- 网络编程(5)—— 基于Linux系统的UDP协议socket服务器和客户端
- Linux C socket 编程之UDP
- Linux socket编程(TCP,UDP,RAW)
- linux socket编程之TCP与UDP