Ojs
Ojs

Reputation: 954

C: sending GET requests but wireshark doesnt catch anything

I'm new in C and with wireshark too. So I'm trying to send http GET request to localhost and simultaneously running wireshark. But it seems that I'm doing something wrong because wireshark doesn't show any headers. So here is my C code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[]) 
{
 int yes = 1;
 char buffer[1024];

 int newsockfd, portno, number, recv_length, sockfd;
 socklen_t clilen;
 struct sockaddr_in serv_addr, cli_addr;

 if (sockfd < 0) 
    error("ERROR opening socket");
 bzero((char *) &serv_addr, sizeof(serv_addr));

 serv_addr.sin_family = AF_INET;
 serv_addr.sin_addr.s_addr = 0;
 serv_addr.sin_port = htons(80);

 sockfd = socket(AF_INET, SOCK_STREAM, 0);

 connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
 number = send(sockfd, "GET /1/ HTTP/1.0\r\n\r\n", strlen("GET /1/ HTTP/1.0\r\n\r\n"), 0);

 recv_length = recv(sockfd, buffer, 1024, 0);
 printf("%s\n", buffer);
}

(I'm using some additional variables that I don't use but don't pay attention on that and /1/ is just my website) This program prints out this I'm running wireshark as root. When I try to catch another websites packets (for example my friends website) it works ok. And I have another question.Is he response that web server sends me back just a text like one text file starting with headers and then the body(html)? Sorry if my questions sound silly. Thanks

Upvotes: 2

Views: 134

Answers (1)

smyatkin_max
smyatkin_max

Reputation: 365

Your question isn't silly. You just are not using any real networking while sending packets to the localhost. It's quite possible that wireshark won't catch them on your system. Read this: https://wiki.wireshark.org/CaptureSetup/Loopback

Try to send them to your external IP, or to some other host.

Yes, the response to GET request is just a text of standart format: http://www.tcpipguide.com/free/t_HTTPResponseMessageFormat.htm

Upvotes: 3

Related Questions