Beginner Socket Programming Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • autiger13
    New Member
    • Feb 2010
    • 2

    #1

    Beginner Socket Programming Error

    Hi I am working on a program that will connect to a server that I input from the command line and make a request for the HTTP response. I am currently using www.yahoo.com as a test site for my project. The problem I am running into is that in the recv() function I am receiving the error Bad value for the ai_flags. I also have done some more error checking and have found that my program is using a datagram connection rather than a socket_stream and I'm not sure why. If anyone could help me figure out why either of these things are happening I would greatly appreciate it.

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <errno.h>
    
    int main(int argc, char *argv[])
    {
    	int sockfd;
    	int status;
    	struct addrinfo hints, *res;
    	struct addrinfo *servinfo;
    	
    	if (argc != 2) {
            fprintf(stderr,"usage: webclient hostname\n");
            return 1;
        }
        
       memset(&hints, 0, sizeof(hints)); // make sure the struct is empty
       hints.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
       hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
       hints.ai_protocol = 6;
    	
    	
       // get ready to connect
       if ((status = getaddrinfo(argv[1], "http", &hints, &res)) != 0) {
            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
            return -1;
        }
        
       if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
       {
       	fprintf(stderr, "socket() error: %s %d\n", gai_strerror(sockfd), errno);
          return -1;
       }
    
    	if((status = connect(sockfd, res->ai_addr, res->ai_addrlen)) < 0)
    	{
    		fprintf(stderr, "connect() error: %s\n", gai_strerror(status));
          return -1;
       }
    	
    	printf ("%s \n", "Success!!!!!");
    	
    	//Start Sending Part of Code 
    	char *msg = "GET /index.html HTTP/1.1\r\nHOST: www.yahoo.com\r\n\r\n";
    	int len, bytes_sent;
    	len = strlen(msg);
    	bytes_sent = send(sockfd, msg, len, 0);
    	if(bytes_sent == -1)
    	{
    		printf("%s \n", "Error on send() function");
    		return 5;
    	}
    	
    	printf("length: %d, bytes_sent: %d \n", len, bytes_sent);
    	
    	//End of Sending Part of Code
    	
    	//Start of the recieving 
    	char *buf; //buffer to hold the recieved message
    	int bytes_recieved, rec_len;
    	rec_len = 4096; //Not sure what value to set this as???
    	bytes_recieved = recv(sockfd, buf, rec_len, 0);
    	
    	if(bytes_recieved == -1)
    	{
    		fprintf(stderr, "recv() error: %s\n res->ai_socktype: %d\n", gai_strerror(bytes_recieved), res->ai_family);
    		return 6;
    	}
    	
    	printf("length: %d, bytes_recieved: %d \n", rec_len, bytes_recieved);
    	
    	//End of recieving part of code
    	
    	close(sockfd); //close socket
    	
    	return 0;
    }
  • autiger13
    New Member
    • Feb 2010
    • 2

    #2
    any ideas.... bump???

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      did you debugged it. if you dont then I would suggest to try gdb

      gdb tutorial. If your program compile fine then debug it step by step. Through all the debugging steps you will have to watch almost everyline especially those lines where your program(may be) not working and look for
      1. What you have expected to get as return
      2. what you are getting.

      By these you will get a clear idea where you are getting your error.

      Regards
      Johny
      Last edited by johny10151981; Feb 9 '10, 03:03 AM. Reason: incomplete info

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Ok forget the previous link. Follow the link below.
        gdb tutorial

        Simplest suggestion to compile
        #gcc -g yourcode.c -o yourout
        after successful compilation and linking. you will get yourout in the same directory as you know already.
        now start debugging by
        #gdb yourout

        Comment

        • Arne Svennevik
          New Member
          • Feb 2010
          • 1

          #5
          Receive buffer not allocated

          You need to allocate memory space for the buffer to hold received bytes. Something like this:

          char *buf; //buffer to hold the recieved message
          buf = malloc(4096); // <--- add this line
          int bytes_recieved, rec_len;

          The number of bytes for the buffer depends on the expected size of the reply. If you get an reply exceeding the buffer size, you need to call recv() several times. Detect this by checking if(bytes_reciev ed == 4096)

          Comment

          Working...