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. -
#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;
-
}
-
4 4697
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
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
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)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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:
...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |