Guest User

Untitled

a guest
Nov 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. // sample_client_ip.c //
  2. // * compile : gcc -o sample sample_client_ip.c //
  3. // * stdout : //
  4. // client send : 200.201.202.1 //
  5. // recv data : 200.201.202.1 //
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <string.h> // strtok
  10. #include <stdlib.h> // atoi
  11.  
  12. typedef union _CLIENT_IP{
  13. char client_ip_c[4];
  14. unsigned int client_ip_i;
  15. }CLIENT_IP;
  16.  
  17.  
  18. unsigned int
  19. send_client_ip(char* client_ip)
  20. {
  21. CLIENT_IP client_union = {};
  22. char* ptr = strtok(client_ip, ".");
  23. client_union.client_ip_c[0] = atoi(ptr);
  24.  
  25. for( int i = 0 ; (ptr = strtok(NULL, ".")) != NULL ; i++ )
  26. {
  27. client_union.client_ip_c[i+1] = atoi(ptr);
  28. }
  29.  
  30. return client_union.client_ip_i;
  31. }
  32.  
  33. int
  34. recv_client_ip(unsigned int data, char* client_ip)
  35. {
  36. CLIENT_IP client_union = {};
  37. client_union.client_ip_i = data;
  38.  
  39. sprintf(client_ip, "%d.%d.%d.%d",
  40. (unsigned char)client_union.client_ip_c[0],
  41. (unsigned char)client_union.client_ip_c[1],
  42. (unsigned char)client_union.client_ip_c[2],
  43. (unsigned char)client_union.client_ip_c[3]);
  44. return 1;
  45. }
  46.  
  47. int
  48. main()
  49. {
  50. char client_ip[] = "200.201.202.1";
  51. unsigned int send_data = send_client_ip(client_ip);
  52.  
  53. char recv_data[100] = {0,};
  54.  
  55. printf("client send : %d.%d.%d.%d \n",
  56. send_data & 0xFF,
  57. ( send_data >> 8 ) & 0xFF,
  58. ( send_data >> 16 ) & 0xFF,
  59. ( send_data >> 24 ) & 0xFF );
  60. recv_client_ip(send_data, recv_data);
  61. printf("recv data : %s\n", recv_data);
  62.  
  63. return 1;
  64. }
Add Comment
Please, Sign In to add comment