16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
#import "OFTCPSocket.h"
#import "OFExceptions.h"
int
main()
{
@try {
OFTCPSocket *server = [OFTCPSocket new];
OFTCPSocket *client = [OFTCPSocket new];
OFTCPSocket *sock = [[OFTCPSocket new] connectTo: "webkeks.org"
onPort: 80];
[sock writeCString: "GET / HTTP/1.1\r\n"
"Host: webkeks.org\r\n\r\n"];
puts((char*)[sock readNBytes: 1024]);
[sock free];
OFTCPSocket *accepted;
char buf[8];
puts("== IPv4 ==");
[server bindOn: "localhost"
withPort: 12345
andFamily: AF_INET];
[server listen];
[client connectTo: "localhost"
onPort: 12345];
accepted = [server accept];
[client writeCString: "Hallo!"];
[accepted readNBytes: 7
intoBuffer: (uint8_t*)buf];
buf[7] = 0;
if (!strcmp(buf, "Hallo!"))
puts("Received correct string!");
else {
puts("Received INCORRECT string!");
return 1;
}
memset(buf, 0, 8);
[accepted free];
[client close];
[server close];
puts("== IPv6 ==");
[server bindOn: "localhost"
withPort: 12345
andFamily: AF_INET6];
[server listen];
[client connectTo: "localhost"
onPort: 12345];
accepted = [server accept];
[client writeCString: "IPv6 :)"];
[accepted readNBytes: 7
intoBuffer: (uint8_t*)buf];
buf[7] = 0;
if (!strcmp(buf, "IPv6 :)"))
puts("Received correct string!");
else {
puts("Received INCORRECT string!");
return 1;
}
[accepted free];
[client close];
[server close];
} @catch(OFException *e) {
printf("EXCEPTION: %s\n", [e cString]);
}
return 0;
}
|