[LUAU] Help needed with a c program
Antonio Querubin
tony at lava.net
Mon Dec 19 16:31:53 PST 2005
On Mon, 19 Dec 2005, Tim Newsham wrote:
> Date: Mon, 19 Dec 2005 08:57:36 -1000 (HST)
> From: Tim Newsham <newsham at lava.net>
> Reply-To: LUAU <luau at lists.hosef.org>
> To: LUAU <luau at lists.hosef.org>
> Subject: Re: [LUAU] Help needed with a c program
>
>> I am looking for sample c code that connects to a URL. I tried to cobble
>> something together but it isn't working out too well.
>>
>> All it needs to do is connect to a url like "
>> https://www.example.com/sample?option1=matt%option2=darnell%option3=please_help
>> "
>
> Does it have to be C code? If not, you can use a utility like curl
> or wget to do the work for you, or you can even use a general utility
> like netcat:
>
> (echo GET /url/here HTTP/1.0; echo) | nc hosthere 80
>
> Anyway, below is some C code. It's written for a unix system, but
> since it looks like you're coding this for windows, I tried to include
> hints for converting the code to windows (they use a socket API for
> networking access which is similar but not identical to the one used
> in unix).
Below is a slightly modified variation of Tim's code that is independent
of IP version (as long as you're compiling on an OS from this century :)).
Antonio Querubin
tony at lava.net
----------------
/* in windows you would include <windows.h> and <winsock.h> */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/*
* Things are slightly different in windows. They use different types
* for sockets than are used in Unix. To make it clearer I added the
* following defines for Unix. You wouldn't need these in windows.
**/
#define SOCKET int /* a socket handle */
#define INVALID_SOCKET -1 /* A bogus handle indicating error
*/
#define SOCKET_ERROR -1 /* error return value */
/* resolve a host. Return 0 for success */
int
resolvHost(char *host, char *port, struct sockaddr *ad)
{
struct addrinfo hints, *res0;
int error;
memset(&hints, 0, sizeof hints);
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* go to dns */
error = getaddrinfo(host, port, &hints, &res0);
if (error) {
perror(host);
return -1;
}
memcpy(ad, res0->ai_addr, res0->ai_addrlen);
return 0;
}
int
conUrl(char *host, char *port, char *path)
{
char buf[1024];
struct sockaddr_storage ad;
SOCKET s;
int len;
if(strlen(host) + strlen(path) > 900) {
fprintf(stderr, "url too long (%s: %s)\n", host, path);
return -1;
}
/* resolve the host then connect to it */
if (resolvHost(host, port, (struct sockaddr *)&ad) == -1) {
fprintf(stderr, "bad address %s\n", host);
return -1;
}
s = socket(ad.ss_family, SOCK_STREAM, 0);
if(s == INVALID_SOCKET) {
perror("socket"); /* use WSAGetLastError in windows */
return -1;
}
if(connect(s, (struct sockaddr*)&ad, ad.ss_len) == SOCKET_ERROR) {
perror(host); /* use WSAGetLastError in windows */
close(s);
return -1;
}
/* send our request */
sprintf(buf, "GET %s HTTP/1.0\n\n", path);
send(s, buf, strlen(buf), 0);
/*
* read back the result -- this is for testing since you
* indicated you don't need the resulting text.
*/
while((len = recv(s, buf, sizeof buf, 0)) > 0)
write(1, buf, len);
close(s); /* use closesocket in windows */
return 0;
}
int
main(int argc, char **argv)
{
/* initialize with WSAStartup in windows */
if(conUrl("www.lava.net", "80", "/~newsham/") == -1)
return 1;
return 0;
}
More information about the LUAU
mailing list