C Sockets - recv() problem

This is a discussion on C Sockets - recv() problem within the Networking/Device Communication forums, part of the General Programming Boards category; I have a problem with the recv() function. I wrote a simple program studying Beej Guide and made a simple ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    C Sockets - recv() problem

    I have a problem with the recv() function. I wrote a simple program studying Beej Guide and made a simple program that connects to yahoo.com on port 80 and runs the GET HTML command. Now doing this with netcat or telnet displays a lot of garbage data, actually retrieves the webpage for me. But using my program just retrieves a small amount of data. Can anyone help? Here is the code

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <strings.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/wait.h>
    
    #define DEST_PORT 80
    #define DEST_IP "68.142.197.74"
    #define MAXDATASIZE 300
    
    int main()
    {
    	int numbytes;
    	char buff[MAXDATASIZE];
    	int my_socket; //creeaza variabila ce va stoca socket-ul
    	struct sockaddr_in destination_address; //stocheaza adresa de conectare
    	
    	if ((my_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		perror("socket");
    		exit(1);
    	}
    	
    	destination_address.sin_family = AF_INET; //host to byte order
    	destination_address.sin_port = htons(DEST_PORT); //short, network to byte order
    	destination_address.sin_addr.s_addr = inet_addr(DEST_IP); //destination ip
    	bzero(&(destination_address.sin_zero), 8); //zero the rest of the struct
    
    	if ((connect(my_socket, (struct sockaddr*)&destination_address, sizeof(struct sockaddr))) == -1)
    	{
    		perror("connect");
    		exit(1);
    	}
    	
    	if (send(my_socket, "GET HTML\n",9, 0) == -1)
    	{
    		perror("send");
    		exit(1);
    	}
    	
    	if ((numbytes = recv(my_socket, buff, MAXDATASIZE, 0)) == -1)
    	{
    		
    		perror("recv");
    	        exit(1);
    	}
    	
    	buff[numbytes] = '\0';
    	
    	printf("Received: %s", buff);
    	
    	close(my_socket);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You are only calling recv() once. You need to call it in a loop to get all the data from the server.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2
    If I use a loop I cannot do an error test, or can I use while(if( .... ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The connection will be closed by the server when all the data is sent so you just need to keep recieving data untill either you get SOCKET_ERROR or 0. Since recv returns the number of bytes recieved we use this value to incriment a pointer and decrease the maximum buffer size so we don't overrun.

    Code:
    ptr=buff;
    for{;;)
       int nSize=MAXDATASIZE;
       int nRet = recv(my_socket, ptr, nSize, 0)
       if (nRet==SOCKET_ERROR || nRet==0)
          {//error or done.
            break;
           }
       nSize-=nRet;
       ptr+=nRet;
    }
    Last edited by Quantum1024; 05-15-2006 at 08:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv data with sockets
    By r063r in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2008, 10:42 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  3. Raw Sockets problem.
    By Mad_guy in forum C Programming
    Replies: 3
    Last Post: 07-03-2005, 04:14 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21