connect() on socket does not return

Hi I jsut wrote a simple program to read a web page.
But the problem is connect() didn't return. Why was that ?
any idea?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>

void error ( char*msg)
{
  perror(msg);
  exit(0);
}

int main()
{
  /* open the socket */
  int fd=socket(PF_INET,SOCK_STREAM,0);
  
  hostent* record;
  /* gethostbyname for the google.com */
  record=gethostbyname("google.lk");
  
  struct sockaddr_in server ;
  memcpy((char*)record->h_addr,(char*)&server.sin_addr.s_addr,
	 record->h_length);
  server.sin_family=AF_INET;
  server.sin_port=htons(80);
  if(connect(fd,(sockaddr*)&server,sizeof(server))<0)
    error("Error connecting");
  char *buffer_in=new char [500];
  char *buffer_out = new char [500];
  
  /* write to the socket */
  strcpy(buffer_in,"GET /index.html HTTP/1.0\r\n\r\n");
  int n=write(fd,buffer_in,strlen(buffer_in));
  if(n<0)
    error("Write Error");
  n=read(fd,buffer_out,500);
  if(n<0)
    error("Read Error");
  printf("%s\n",buffer_out);
  return 0;
}



Thanks in advance.
sorry I had done a very simple mistake ,
I'd able to get rid of that.
thanks.


Topic archived. No new replies allowed.