udp recvfrom 获取对方ip,ioctl获取本机ip source code

前题: 以下两段代码来自网络,个人整理后,分享。其中用的是socket基本知识,个人将不写注释了,需要注释的地方已经用空白留出。各函数请参考man手册。

socket基础编程可以参考wiki或者下面这个链接,感谢作者的辛劳分享!

http://blog.csdn.net/tigerjibo/article/details/6764613

 

1、获取对方ip,客户端不写,客户端将服务端稍作修改即可。

 1 #include <sys/types.h>

 2 #include <sys/socket.h>

 3 #include <stdio.h>

 4 #include <stdlib.h>

 5 #include <string.h>

 6 #include <errno.h>

 7 #include <unistd.h>

 8 #include <arpa/inet.h>

 9 #include <netinet/in.h>

10 

11 #define PORT 9876 

12 #define BUFFSIZE 1024

13 

14 

15 int main(void)

16 {

17     struct sockaddr_in ser_addr,cli_addr;

18     int sock_fd,rec_len,rec;

19     char rec_buf[BUFFSIZE];

20     char sen_buf[BUFFSIZE];

21     char * cli_ip;

22 

23     memset(&ser_addr,0,sizeof(ser_addr));

24     ser_addr.sin_family = AF_INET;

25     ser_addr.sin_port = htons(PORT);

26     ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);

27 

28     sock_fd = socket(AF_INET,SOCK_DGRAM,0);

29     if(sock_fd < 0){

30         perror("sockfd!\n");

31         exit(1);

32     }else {

33         printf("sock_fd = %d\n",sock_fd);

34     }

35     

36     if(bind(sock_fd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr_in)) < 0){

37         perror("bind");

38         exit(1);

39     }else{

40         printf("bind success!\n");

41     }

42 

43     rec_len = sizeof(struct sockaddr_in);

44     memset(&cli_addr,0,sizeof(struct sockaddr_in));

45     memset(rec_buf,0,sizeof(BUFFSIZE));

46     memset(sen_buf,0,sizeof(BUFFSIZE));

47     strcpy(sen_buf,"Hello boys!");

48 

49     while(1){

50         rec = recvfrom(sock_fd,rec_buf,BUFFSIZE,0,(struct sockaddr*)&cli_addr,&rec_len);

51         if(rec < 0){

52             perror("recvfrom");

53             exit(1);

54         }

55         cli_ip = inet_ntoa(cli_addr.sin_addr);

56 //        if(cli_ip != NULL){

57             if(sendto(sock_fd,sen_buf,BUFFSIZE,0,(struct sockaddr*)&cli_addr,rec_len) < 0){

58                 perror("sendto");

59                 exit(1);

60             }else{

61                 printf("sendto success!\n");

62             }

63 //        }

64         printf("client ip add is :\n%s\n",cli_ip);

65     }

66     close(sock_fd);

67 

68     return 0;

69 }

2、
获取本机ip方法

 1 #include <stdio.h>

 2 #include <sys/ioctl.h>

 3 #include <sys/socket.h>

 4 #include <sys/types.h>

 5 #include <netdb.h>

 6 #include <net/if.h>

 7 #include <arpa/inet.h>

 8 #define ERRORIP "cannot find host ip"

 9 char *ip_search(void)

10 {

11     int sfd, intr;

12     struct ifreq buf[16];

13     struct ifconf ifc;

14 

15     sfd = socket (AF_INET, SOCK_DGRAM, 0); 

16     if (sfd < 0)

17         return ERRORIP;

18     ifc.ifc_len = sizeof(buf);

19     ifc.ifc_buf = (caddr_t)buf;

20 

21     if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc))

22         return ERRORIP;

23     intr = ifc.ifc_len / sizeof(struct ifreq);

24     while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr]));

25     close(sfd);

26     return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr);

27 }

28 int main(void)

29 {

30     printf("%s\n", ip_search());

31     return 0;

32 }

 

你可能感兴趣的:(source)