当我还是新手的时候,我就已经懂得ip是个啥玩意了。
但是当很多人还是新手的时候,不懂啥是ip。
也不知道cb的IP是多少。
于是我写了个程序,通过向同样ip区段的ip的特定端口发送socket以确定是否是cb
It's called "FindCb"(这句话有语法错误不……)
好了好了,上代码,这是cb端的,可以在/etc/rc.local里面的exit 0前加上这个的启动程序:- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- const char txt[] = "I am a Cb\n";
- int main()
- {
- int sfp,nfp;
- struct sockaddr_in s_add,c_add;
- int sin_size;
- unsigned short portnum=5910;
- printf("FindCb:Start FindCb program!\n");
- sfp = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == sfp)
- {
- printf("socket fail ! \n");
- return -1;
- }
- //printf("socket ok !\n");
-
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr=htonl(INADDR_ANY);
- s_add.sin_port=htons(portnum);
-
- if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
- {
- printf("bind fail !\n");
- return -1;
- }
- //printf("bind ok !\n");
-
- if(-1 == listen(sfp,5))
- {
- printf("listen fail !\n");
- return -1;
- }
- //printf("listen ok\n");
- while(1)
- {
- sin_size = sizeof(struct sockaddr_in);
-
- nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
- if(-1 == nfp)
- {
- printf("accept fail !\n");
- return -1;
- }
- printf("FindCb:Get a connect from %x : %d\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
-
- if(-1 == write(nfp,txt,sizeof(txt)))
- {
- printf("write fail!\n");
- return -1;
- }
- //printf("write ok!\n");
- close(nfp);
- }
- close(sfp);
- return 0;
- }
复制代码 然后这是服务端的,我用的是mac系统,其中获取本机ip用的是ipconfig的手法,popen去执行ipconfig和grep等,返回无线网络的ip,有线mac网络改成en0,linux的话改成 ifconfig eth0|grep 'inet addr:'|cut -d: -f2 | awk '{ print $1}'
,好了,code:- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #define TIMEOUT_TIME 1
- /*
- By TLL,but most of the codes are from the web
- */
- int cfd;
- void alarm_func(){
- printf("Timeout!\n");
- close(cfd);
- }
- int main()
- {
- int recbytes;
- int sin_size;
- int i = 100;
- int hb = 0;
- char buffer[1024]={0};
- struct sockaddr_in s_add,c_add;
- unsigned short portnum=5910;
- signal(SIGALRM, alarm_func);
- printf("Hello,welcome to client !\n");
- FILE *ipf=popen("ifconfig en1|grep broadcast|awk '{ print $2}'","r");
- //For mac,wireless network
- char search_ip[16] = "";
- char get_ip[16] = "";
- char qz[16] = "";
- char delim[4] = "";
- fgets(get_ip, sizeof(get_ip), ipf);
- pclose(ipf);
- get_ip[strlen(get_ip)-1] = '\0';
- printf("Your IP:%s\n",get_ip);
- strcat(qz,strtok(get_ip,"."));
- qz[strlen(qz)]='.';
- strcat(qz,strtok(NULL,"."));
- qz[strlen(qz)]='.';
- strcat(qz,strtok(NULL,"."));
- qz[strlen(qz)]='.';
- qz[strlen(qz)]='\0';
- //printf("qz:%s\n",qz);
- /*
- ping 100 first,then to 255,then 1,then to 99,then exit
- */
- while(hb<2){
- while(255 > i){
- sprintf(search_ip,"%s%d",qz,i++);
- //if(i == 106) continue;
- //For test(skip my Cb and test ...)
- printf("Trying:%s\n",search_ip);
- cfd = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == cfd)
- {
- printf("socket fail ! \n");
- return -1;
- }
- //printf("socket ok !\n");
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr= inet_addr(search_ip);
- s_add.sin_port=htons(portnum);
- //printf("s_addr = %x ,port : %d\n",s_add.sin_addr.s_addr,s_add.sin_port);
- alarm(TIMEOUT_TIME);
- if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct
- sockaddr)))
- {
- alarm(0);
- printf("connect fail !\n");
- if(i == 100 && hb == 1){
- return 1;
- }
- continue;
- }
- printf("Connected!\nSpend Time:%d\n",TIMEOUT_TIME-alarm(0));
- //printf("connect ok !\n");
- if(-1 == (recbytes = read(cfd,buffer,1023)))
- {
- printf("read data fail !\n");
- continue;
- }
- //printf("read ok\n");
- buffer[recbytes]='\0';
- if(strcmp(buffer,"I am a Cb\n") == 0){
- printf("Find a Cb!\n");
- printf("It's IP is:%s\n",search_ip);
- return 0;
- }
- printf("%s\n",buffer);
- close(cfd);
- if(i == 100 && hb == 1){
- return 1;
- }
- }
- hb++;
- i=1;
- }
- return 0;
- }
复制代码 编译执行~
另外client我特别让他从100开始搜索,因为100一般是dhcp的开始数,所以比较容易找到。
log(client):- TlldeMacBook-Pro:findcb tonylianlong$ ./findcb
- Hello,welcome to client !
- Your IP:192.168.1.103
- Trying:192.168.1.100
- connect fail !
- Trying:192.168.1.101
- Timeout!
- connect fail !
- Trying:192.168.1.102
- Timeout!
- connect fail !
- Trying:192.168.1.103
- connect fail !
- Trying:192.168.1.104
- Timeout!
- connect fail !
- Trying:192.168.1.105
- Connected!
- Spend Time:0
- Find a Cb!
- It's IP is:192.168.1.105
- TlldeMacBook-Pro:findcb tonylianlong$
复制代码 Server:- root@cubieboard:~/source_code/findcb# ./findcb
- FindCb:Start FindCb program!
- FindCb:Get a connect from c0a80167 : 53419
复制代码 |