#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define IP "195.154.182.137"
#define BUFFER_REQUEST 100
#define BUFFER_RESPONSE 1000
void print_ip(char *response);
int main()
{
int socket_desc;
struct sockaddr_in server;
char request[BUFFER_REQUEST];
char response[BUFFER_RESPONSE];
bzero(request, BUFFER_REQUEST);
bzero(response, BUFFER_RESPONSE);
//create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("/nError: can\'t create socket.\n\n");
exit(-1);
}
server.sin_addr.s_addr = inet_addr(IP);
server.sin_family = AF_INET;
server.sin_port = htons(80);
//make request string
strcat(request, "GET /ipme/ipme.php");
strcat(request, " HTTP/1.1\r\n");
strcat(request, "Host: ");
strcat(request, "www.pirotski.com");
strcat(request, "\r\nConnection: close");
strcat(request, "\r\n\r\n");
//connect to server
if(connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("\nError: Can\'t connect to server!\n\n");
exit(-1);
}
//send request
if(send(socket_desc, request, strlen(request), 0) < 0)
{
printf("\nError: Can\'t send request!\n");
exit(-1);
}
//receive resposne
recv(socket_desc, response, BUFFER_RESPONSE, 0);
//parse and print ip address
print_ip(response);
close(socket_desc);
}
void print_ip(char *response)
{
char *p1 = strstr(response, "[ip]")+4;
int i=-1;
while(p1[++i] != '[')
printf("%c", p1[i]);
printf("\n");
}
It prints your external ip address in console.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.