connection.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:47:18 +0200
changeset 27 81a574d60c15
parent 10 f19f44e2e863
child 29 7abf6146898e
permissions -rw-r--r--
typo in min2time format string committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * test/demos/connection.c
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 mussolini
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 * Copyright (C) 2008 mbroeker
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <unistd.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <string.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <netdb.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#include <arpa/inet.h>
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
int connection (char *ip, unsigned short port)
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    struct hostent *hs;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    18
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    struct sockaddr_in sock;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    20
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    int sockfd;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    memset (&sock, 0, sizeof (sock));
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    sock.sin_family = AF_INET;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    sock.sin_port = htons (port);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    if ((sock.sin_addr.s_addr = inet_addr (ip)) == -1) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        if ((hs = gethostbyname (ip)) == NULL) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
            perror ("[-] Error");
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
            return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        sock.sin_family = hs->h_addrtype;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        memcpy (&sock.sin_addr.s_addr, hs->h_addr, hs->h_length);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        perror ("[-] Error");
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    if (connect (sockfd, (struct sockaddr *)&sock, sizeof (sock)) < 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        perror ("[-] Error ");
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        return -1;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    return (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
}
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
int main (int argc, char **argv)
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
{
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    char buffer[1024];
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    52
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    int sockfd;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    54
10
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    int num;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    if (argc != 3) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        printf ("Usage: %s <ipaddr> <port>\n", argv[0]);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        return EXIT_SUCCESS;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    if ((sockfd = connection (argv[1], atoi (argv[2]))) < 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
        printf ("Connection error: %s:%d\n", argv[1], atoi (argv[2]));
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
        return EXIT_FAILURE;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    write (sockfd, "GET /\r\n", 7);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
    while ((num = read (sockfd, buffer, 1023)) != 0) {
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        buffer[num] = 0;
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        printf ("%s", buffer);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    }
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
    return close (sockfd);
f19f44e2e863 a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
}