author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:03 +0100 | |
changeset 10 | f19f44e2e863 |
child 27 | 81a574d60c15 |
permissions | -rw-r--r-- |
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; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
18 |
struct sockaddr_in sock; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
int sockfd; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
memset (&sock, 0, sizeof (sock)); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
sock.sin_family = AF_INET; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
23 |
sock.sin_port = htons (port); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
25 |
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
|
26 |
if ((hs = gethostbyname (ip)) == NULL) { |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
perror ("[-] Error"); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
return -1; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
} |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
} |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
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
|
35 |
perror ("[-] Error"); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
return -1; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
} |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
39 |
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
|
40 |
perror ("[-] Error "); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
return -1; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
} |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
44 |
return (sockfd); |
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 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
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
|
48 |
{ |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
char buffer[1024]; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
int sockfd; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
int num; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
if (argc != 3) { |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
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
|
55 |
return EXIT_SUCCESS; |
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 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
return EXIT_FAILURE; |
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 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
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
|
64 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
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
|
66 |
buffer[num] = 0; |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
printf ("%s", buffer); |
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 |
|
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
70 |
return close (sockfd); |
f19f44e2e863
a nice connection demo from an irc user
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
71 |
} |