#include <stdio.h>
#include <stdlib.h> /* exit */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* sockaddr_in */
#include <strings.h> /* bzero */
#include <string.h> /* strlen */
#include <netdb.h> /* hostent, gethostbyname */
#define BUFSIZE 1024
#define DESTSERV "dev.furoom.net"
#define DESTPORT 80
#define MESSAGE \
"POST /sandbox/socket-http/request.php?g=1 HTTP/1.1\r\n\
Host: dev.furoom.net\r\n\
Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n\
Content-Type: application/x-www-form-urlencoded\r\n\
Content-Length: 7\r\n\
\r\n\
a=1&b=2"
int main(int argc, char *argv[])
{
struct hostent *hostent;
struct sockaddr_in server;
char buf[BUFSIZE];
int fd;
int len;
int ret = 1; /* failure */
/* initial */
bzero(&server, sizeof(server));
bzero(&buf, sizeof(buf));
/* lookup ip */
hostent = gethostbyname(DESTSERV);
if (hostent == NULL) {
herror(DESTSERV);
return ret;
}
/* create socket */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return ret;
}
/* connect */
server.sin_family = AF_INET;
bcopy(hostent->h_addr, &server.sin_addr, hostent->h_length);
server.sin_port = htons(DESTPORT);
if (connect(fd, (struct sockaddr *)&server, sizeof(server)) == -1) {
perror("connect");
goto close_exit;
}
/* request */
if (write(fd, MESSAGE, strlen(MESSAGE)) < 0) {
perror("write");
goto close_exit;
}
/* response */
if (read(fd, buf, sizeof(buf)) < 0) {
perror("read");
goto close_exit;
}
printf("%s\n", buf);
ret = 0; /* success */
close_exit:
close(fd);
return ret;
}