|
本帖最后由 soloforce 于 2014-10-6 19:30 编辑
对于无显示器CB Linux系统,且IP地址为动态获取方式时,获取CB系统的IP地址可能是一个令人头疼的问题;然而利用局域网广播方法,则可以让与CB系统位于同一个局域网的其他机器得到CB系统的IP地址,仅仅需要Linux下socket编程的一些知识以及若干行代码。此外,为了适合各种类型的局域网,采用255.255.255.255的广播地址;当然,广播包不会被路由器转发。
===== CB系统代码 =====
- /* filename: brdip.c
- *
- * IP broadcast via UDP
- * by soloforce, 2014
- */
- #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 7777
- #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);
- }
- // broadcast a message
- strcpy(buf,"Cubieboard on line!");
- sendto(socket_fd,buf,strlen(buf),0,(struct sockaddr *)&my_addr,sizeof(my_addr));
- // receive the message broadcast just now, to get user_addr which contains the IP info.
- size=sizeof(user_addr);
- recvfrom(socket_fd,buf,MAXDATASIZE,0,(struct sockaddr *)&user_addr,&size);
- // extract the IP info
- bzero(buf, sizeof(buf));
- sprintf(buf, "Cubieboard IP: %s\n",inet_ntoa(user_addr.sin_addr));
- // keep broadcast the IP info in 3 secs.
- while(1) {
- sendto(socket_fd,buf,strlen(buf),0,(struct sockaddr *)&my_addr,sizeof(my_addr));
- sleep(3);
- }
- return 0;
- }
复制代码 ===== 客户机(Linux系统)代码 =====
- /* filename: udprecv.c
- *
- * receive IP info via UDP
- * by soloforce, 2014
- */
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define PORT 7777
- #define MAXDATASIZE 256
- int main(int argc, char**argv)
- {
- int sockfd,n;
- struct sockaddr_in servaddr,cliaddr;
- socklen_t size;
- char buf[MAXDATASIZE];
- sockfd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(PORT);
- bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
- while(1){
- size = sizeof(cliaddr);
- bzero(buf, sizeof(buf));
- n = recvfrom(sockfd,buf,1000,0,(struct sockaddr *)&cliaddr,&size);
- buf[n] = 0;
- printf("%s",buf);
- }
- }
复制代码 ===== 编译与执行 =====
分别在CB系统和客户系统中编译程序
CB $ gcc brdip.c -o brdip
GUEST $ gcc udprecv.c -o udprecv
分别执行之
CB $ ./brdip
GUEST $ ./udprecv
在客户系统中可以看到如下显示
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
Cubieboard IP: 192.168.1.124
...
|
评分
-
查看全部评分
|