[LUAU] Help needed with a c program
Tim Newsham
newsham at lava.net
Mon Dec 19 10:57:36 PST 2005
> 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).
> Matt
Tim Newsham
http://www.lava.net/~newsham/
/* 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 */
/* resovle an IPv4 host. Return 0 for success */
int
resolvHost(char *host, struct sockaddr_in *ad)
{
struct hostent *hp;
memset(ad, 0, sizeof ad);
ad->sin_family = AF_INET;
/* try it as a dotted-quad */
ad->sin_addr.s_addr = inet_addr(host);
if(ad->sin_addr.s_addr != INADDR_NONE)
return 0;
/* go to dns */
hp = gethostbyname(host);
if(!hp || hp->h_length != 4)
return -1;
memcpy(&ad->sin_addr.s_addr, hp->h_addr, 4);
return 0;
}
int
conUrl(char *host, int port, char *path)
{
char buf[1024];
struct sockaddr_in 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, &ad) == -1) {
fprintf(stderr, "bad address %s\n", host);
return -1;
}
ad.sin_port = htons(port);
s = socket(ad.sin_family, SOCK_STREAM, 0);
if(s == INVALID_SOCKET) {
perror("socket"); /* use WSAGetLastError in windows */
return -1;
}
if(connect(s, (struct sockaddr*)&ad, sizeof ad) == 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