Thread: socket programming recv function

  1. #1
    vicky
    Guest

    socket programming recv function

    on my lan 192.168.0.1 is running a webserver so when i wrote a simple socket application like



    char buffer[32];
    SOCKET sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct sockaddr_in * address= new (struct sockaddr_in);
    address->sin_family=AF_INET;
    address->sin_addr.s_addr=inet_addr("192.168.0.1");
    address->sin_port=htons(80);
    memset(address->sin_zero,'\0',8);
    printf("connect %d \n",connect(sock,(struct sockaddr *)address,sizeof(*address)));
    printf("%d ",send(sock,"GET /index.htm",14,0));
    recv(sock,buffer,32,0);


    till send everything is working fine but why i am not able to retreive the index.htm page back .

    i have tried the same program on my linux box too but to no avail .what is the prob am i missing something ...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("%d ",send(sock,"GET /index.htm",14,0));
    Do you need to send a newline with this as well?

    > recv(sock,buffer,32,0);
    This doesn't know how much data will be returned, and in particular, it can return the data in fragments. It's up to you to reassemble the data into its proper form.
    Code:
    int n;
    while ( (n=recv(sock,buffer,32,0)) > 0 ) {
        // do something with n bytes in buffer
    }
    if ( n == -1 ) {
        perror( "cant recv" );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    vicky
    Guest
    thanx it's working now ... i needed two new line characters ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. shutdown function socket programming
    By 256Doofus in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-26-2008, 04:47 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM