473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner Socket Programming Error

2 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <arpa/inet.h>
  8. #include <errno.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     int sockfd;
  13.     int status;
  14.     struct addrinfo hints, *res;
  15.     struct addrinfo *servinfo;
  16.  
  17.     if (argc != 2) {
  18.         fprintf(stderr,"usage: webclient hostname\n");
  19.         return 1;
  20.     }
  21.  
  22.    memset(&hints, 0, sizeof(hints)); // make sure the struct is empty
  23.    hints.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
  24.    hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
  25.    hints.ai_protocol = 6;
  26.  
  27.  
  28.    // get ready to connect
  29.    if ((status = getaddrinfo(argv[1], "http", &hints, &res)) != 0) {
  30.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
  31.         return -1;
  32.     }
  33.  
  34.    if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  35.    {
  36.        fprintf(stderr, "socket() error: %s %d\n", gai_strerror(sockfd), errno);
  37.       return -1;
  38.    }
  39.  
  40.     if((status = connect(sockfd, res->ai_addr, res->ai_addrlen)) < 0)
  41.     {
  42.         fprintf(stderr, "connect() error: %s\n", gai_strerror(status));
  43.       return -1;
  44.    }
  45.  
  46.     printf ("%s \n", "Success!!!!!");
  47.  
  48.     //Start Sending Part of Code 
  49.     char *msg = "GET /index.html HTTP/1.1\r\nHOST: www.yahoo.com\r\n\r\n";
  50.     int len, bytes_sent;
  51.     len = strlen(msg);
  52.     bytes_sent = send(sockfd, msg, len, 0);
  53.     if(bytes_sent == -1)
  54.     {
  55.         printf("%s \n", "Error on send() function");
  56.         return 5;
  57.     }
  58.  
  59.     printf("length: %d, bytes_sent: %d \n", len, bytes_sent);
  60.  
  61.     //End of Sending Part of Code
  62.  
  63.     //Start of the recieving 
  64.     char *buf; //buffer to hold the recieved message
  65.     int bytes_recieved, rec_len;
  66.     rec_len = 4096; //Not sure what value to set this as???
  67.     bytes_recieved = recv(sockfd, buf, rec_len, 0);
  68.  
  69.     if(bytes_recieved == -1)
  70.     {
  71.         fprintf(stderr, "recv() error: %s\n res->ai_socktype: %d\n", gai_strerror(bytes_recieved), res->ai_family);
  72.         return 6;
  73.     }
  74.  
  75.     printf("length: %d, bytes_recieved: %d \n", rec_len, bytes_recieved);
  76.  
  77.     //End of recieving part of code
  78.  
  79.     close(sockfd); //close socket
  80.  
  81.     return 0;
  82. }
  83.  
Feb 7 '10 #1
4 4697
autiger13
2 New Member
any ideas.... bump???
Feb 9 '10 #2
johny10151981
1,059 Top Contributor
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
Feb 9 '10 #3
johny10151981
1,059 Top Contributor
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
Feb 9 '10 #4
Arne Svennevik
1 New Member
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_recieved == 4096)
Feb 21 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
13719
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
5
2472
by: Panama Red | last post by:
I would like to learn to program in c++ on Linux and AIX systems...mainly socket and fifo type stuff. Can someone recommend a book for someone with experience only with Perl, shell, and Pick/Basic...
1
1967
by: mirandacascade | last post by:
Version of python: 2.4 O/S: Win2K I will be writing some python scripts to do some client-side programming that involves socket.py. I expect that I will be making calls to the following...
2
2864
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
2
5170
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
2
7533
by: Ben | last post by:
I need to send data from a client to a server. In the server code I have: s = ServerSocket.Accept() If (s.Connected = False) Then MsgBox("Unable to connect", , "Server Error") Exit Sub End...
11
8579
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
1888
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
3
2756
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
2
3642
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.