Re(2): bsd sockets & http
Re(2): bsd sockets & http
- Subject: Re(2): bsd sockets & http
- From: Chaz <email@hidden>
- Date: Fri, 19 Jul 2002 21:01:26 -0600
Hello:
Thanks Philip & Jens. As it turns out, an incomplete request was the
problem. It did work with "\12\15" tacked on the end.
I have another question about the behavior of sockets, though. When I try
to connect() to a host that doesn't exist, connect() will return an error
after one or two minutes, while the beach ball spins. Is there a way to
shorten that time?
:: source
+ (void)getHTTPResponse
{
const char *hostAddress = "www.phat64.com";
const int hostPort = 80;
const char *serverRequest = "GET / HTTP/1.1\nHost:
www.phat64.com\nConnection: close\n User-Agent: MyClient\n\12\15";
struct sockaddr_in host;
struct hostent *hostAddr;
int sock;
int error, bytes, totalBytes;
char buffer[2*1024];
// Find the host address.
hostAddr = gethostbyname( hostAddress );
// Set up the host address.
host.sin_family = AF_INET;
host.sin_port = htons( hostPort );
memcpy( &host.sin_addr, hostAddr->h_addr, sizeof(hostAddr->h_addr) );
memset( &host.sin_zero, '\0', 8 );
// Make the socket.
sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock==-1 )
{
NSLog( @"socket failed" );
return;
}
// Make the connection.
error = connect( sock, (struct sockaddr *)&host, sizeof(struct sockaddr)
);
if ( error==-1 )
{
NSLog( @"connect failed" );
return;
}
// Send the request.
bytes = -1, totalBytes = 0;
while ( totalBytes<strlen(serverRequest) )
{
bytes = send( sock, serverRequest+totalBytes, strlen(serverRequest),
0 );
if ( bytes==-1 )
{
NSLog( @"send failed" );
close(sock);
return;
}
else
totalBytes += bytes;
}
// Read reply from server.
bytes = -1, totalBytes = 0;
while ( bytes!=0 )
{
bytes = recv( sock, &buffer[totalBytes], (2*1024)-totalBytes, 0 );
if ( bytes==-1 )
{
NSLog( @"read failed" );
close(sock);
return;
}
else if ( bytes==0 )
{
NSLog( @"read done" );
}
else
totalBytes += bytes;
}
buffer[totalBytes] = '\0';
NSLog( @"read (%i totalBytes)\n\n%s", bytes, buffer );
// Close the socket.
close(sock);
}
:: end
On Friday, July 19, 2002, at 06:37 PM, Jens Bauer wrote:
Hi Chaz and Philip,
On Fri, 19 Jul, 2002, Philip D. Wasson <email@hidden> wrote:
On Wednesday, July 17, 2002, at 11:54 , Chaz wrote:
I'm currently reading up on bsd sockets and I've run into a problem
while trying to make a sample http client. Here's what I've done:
socket()
bind() // to a random port - i don't think this is necessary for an
http client, but to my knowledge it shouldn't hurt.
connect() // to the http server on port 80
.
.
.
} // Might be a weird way to do this, but it is a sample after all.
The message sent is "GET /" which works in telnet.
read() // similarly to how I sent, making sure all the data has
arrived. But no data ever does arrive.
close()
I don't know HTTP, so I can't add any detail to Jens Bauer's comment
about sending the right HTTP header. Except I wonder if you sent a CR/LF
at the end of the request. I'm pretty sure HTTP likes there to be a
specific kind of line ending.
You are right about that. Most servers and clients are able to handle
"\n", which can be either CR or LF, however, some clients require
"\12\15" (CR followed by LF).
Oh, and you don't need the bind(). Better to skip it and have one less
possible error to deal with.
Now, this is the nice thing about going Unix... Try having a look at this
source-code snippet I found on the net:
<http://hoohoo.ncsa.uiuc.edu/beta-1.6/scripts/src/tmp>
It's for POSTing a request, which means it's not really a GET, as you
want, but you might be able to find out what's causing the trouble by
looking at the snippet.
( Thanks for finding the snippet, Jens. )
===================================================================
Chaz McGarvey email@hidden / email@hidden
http://www.phat64.com University of Utah
===================================================================
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.