I am new to network programming, and trying to read a response from a servers webpage.
I can connect, but cannot get the pages source. Heres what I have

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc,char **args){

if(argc < 2){
printf("2 args\n");
return 1;

}

int port = atoi(args[1]);

int sockfd = socket(AF_INET,SOCK_STREAM,0);

struct sockaddr_in client;

client.sin_addr.s_addr = inet_addr("192.168.1.3");
client.sin_port = htons(port);
client.sin_family = AF_INET;
printf("here\n");

int conn = connect(sockfd,(struct sockaddr *)&client,sizeof(client));
if(conn < 0)
return 1;

char m[1000];
read(sockfd,m,sizeof(m));

printf("%s\n",m);



close(sockfd);
return 0;

}

i can get my webpage from my other computer fine, theres no firewall blocking
connections to port 80.
But I cant read the page source from this program..
Works fine with ssh, port 22..I get the little ssh message.

Any clues to why I cannot read the servers response from port 80?

Hi, if you want to get a web page with http protocol, you will have to send a request first, and then the server will answer back with the web page.
When you conect to the server, your program will be the master, so you will have no answer before sending any request.

http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

This article might help you, you will have to do somthing like this:

int conn = connect(sockfd,(struct sockaddr *)&client,sizeof(client));
if(conn < 0)
return 1;

char request[] = "GET / HTTP/1.1\nHost: www.google.fr\nAccept: text/html";

write(sockfd,request,sizeof(request));
 
char m[1000];
read(sockfd,m,sizeof(m));
 
printf("%s\n",m);

For debuging you can use wireshark, it allow you to see what you send and what you get on an ethernet / wifi interface.

I hope it can help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.