Re: Little problem with C program
- From: "Phlip" <phlipcpp@xxxxxxxxx>
- Date: Sun, 28 Aug 2005 19:58:44 GMT
Elekaj wrote:
> This is my problem, the function listed under this text does'nt seems to
> work correctly.
Dude, I was just looking for C-style code to ping one web page or return an
error. Thanks!
> printf("Sending request : %s\n",request);
> send(cddbSocket,&request,sizeof(request),0);
You need to learn more C before coding hard things in it. For example,
'sizeof request' returns the size of a pointer, not the length of the
pointed-to string. Use strlen().
send(cddbSocket, request, strlen(request),0);
And request has no &.
(Then consider using a softer language, like Ruby, which comes with
libraries that handle raw HTTP, so you can skip ahead to actually doing what
you need to do with HTTP.)
I used this tutorial to get your snip working:
http://www.codeproject.com/internet/winsockintro02.asp
> When I call the function requestHTTP("80","192.168.0.1","GET\n"), the
> function works fine until the read command (on the screen, there are the
> message "Reading response ..." and nothing else appens.
You need to use recv(), to await and receive bytes. I think "read()" is for
other situations where you already have the bytes and just need to pull them
out of the socket's internal buffer.
So I added WSAStartup(), and got stuff working with this loop:
printf("Sending request : %s\n",request);
sprintf(response,"GET %s\r\n\r\n",request);
send(cddbSocket,response,strlen(response),0);
printf("Reading response ...\n");
int y;
while(y=recv(cddbSocket,response, sizeof response,0))
{
puts (response);
}
That system puts the GET inside the function, so I call it like this:
requestHTTP("8888", "localhost", "/FrontPage")
Note the /. HTTP will kack for no reason without it.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.
- References:
- Little problem with C program
- From: Elekaj
- Little problem with C program
- Prev by Date: Re: GNU Public Licences Revisited (again)
- Next by Date: Re: GNU Public Licences Revisited (again)
- Previous by thread: Little problem with C program
- Index(es):
Relevant Pages
|