Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sample_client_ip.c //
- // * compile : gcc -o sample sample_client_ip.c //
- // * stdout : //
- // client send : 200.201.202.1 //
- // recv data : 200.201.202.1 //
- #include <stdio.h>
- #include <string.h> // strtok
- #include <stdlib.h> // atoi
- typedef union _CLIENT_IP{
- char client_ip_c[4];
- unsigned int client_ip_i;
- }CLIENT_IP;
- unsigned int
- send_client_ip(char* client_ip)
- {
- CLIENT_IP client_union = {};
- char* ptr = strtok(client_ip, ".");
- client_union.client_ip_c[0] = atoi(ptr);
- for( int i = 0 ; (ptr = strtok(NULL, ".")) != NULL ; i++ )
- {
- client_union.client_ip_c[i+1] = atoi(ptr);
- }
- return client_union.client_ip_i;
- }
- int
- recv_client_ip(unsigned int data, char* client_ip)
- {
- CLIENT_IP client_union = {};
- client_union.client_ip_i = data;
- sprintf(client_ip, "%d.%d.%d.%d",
- (unsigned char)client_union.client_ip_c[0],
- (unsigned char)client_union.client_ip_c[1],
- (unsigned char)client_union.client_ip_c[2],
- (unsigned char)client_union.client_ip_c[3]);
- return 1;
- }
- int
- main()
- {
- char client_ip[] = "200.201.202.1";
- unsigned int send_data = send_client_ip(client_ip);
- char recv_data[100] = {0,};
- printf("client send : %d.%d.%d.%d \n",
- send_data & 0xFF,
- ( send_data >> 8 ) & 0xFF,
- ( send_data >> 16 ) & 0xFF,
- ( send_data >> 24 ) & 0xFF );
- recv_client_ip(send_data, recv_data);
- printf("recv data : %s\n", recv_data);
- return 1;
- }
Add Comment
Please, Sign In to add comment