spell checking: 'cannot set the limit' sounds better...
committer: Markus Bröker <mbroeker@largo.homelinux.org>
/**
* connection.c
* Copyright (C) 2008 mussolini
* Copyright (C) 2008 mbroeker
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
int connection (char *server, char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sockfd = -1;
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo (server, port, &hints, &result) != 0) {
perror ("getaddrinfo");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
if ((sockfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
continue;
if (connect (sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
break;
}
close (sockfd);
sockfd = -1;
}
if (result != NULL)
freeaddrinfo (result);
return (sockfd);
}
int main (int argc, char **argv)
{
char buffer[1024];
int sockfd;
int num;
if (argc != 3) {
printf ("Usage: %s <ipaddr> <service>\n", argv[0]);
return EXIT_FAILURE;
}
if ((sockfd = connection (argv[1], argv[2])) < 0) {
printf ("Connection error: %s:%s\n", argv[1], argv[2]);
return EXIT_FAILURE;
}
if (write (sockfd, "GET / HTTP/1.0\r\n\r\n", 18) == -1) {
perror ("write");
return EXIT_FAILURE;
}
while ((num = read (sockfd, buffer, 1023)) != 0) {
buffer[num] = 0;
printf ("%s", buffer);
}
return close (sockfd);
}