ObjFW  Check-in [b54cffa9db]

Overview
Comment:Use random port for test.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b54cffa9dbabc91d640f5bbfc13ab0e2cb20f547ed179d3b1f0bd8da2c92d790
User & Date: js on 2008-12-08 16:47:20
Other Links: manifest | tags
Context
2008-12-08
16:51
Don't allow connecting/binding on an already opened socket.
Also, free mem on accepted sockets when close is called.
check-in: c8990ecd12 user: js tags: trunk
16:47
Use random port for test. check-in: b54cffa9db user: js tags: trunk
2008-12-07
21:58
Server support for OFTCPSocket. check-in: 0d5b08e43e user: js tags: trunk
Changes

Modified tests/OFTCPSocket/OFTCPSocket.m from [35b6a575c0] to [5359cde91c].

1
2
3
4
5
6
7
8
9
10
11
12
13


14
15
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
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"



#import <string.h>

#import "OFTCPSocket.h"
#import "OFExceptions.h"













int
main()
{




	@try {
		OFTCPSocket *server = [OFTCPSocket new];
		OFTCPSocket *client = [OFTCPSocket new];
		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];













>
>




>
>
>
>
>
>
>
>
>
>
>
>




>
>
>
>




|


>


|




|




|

|








|






>


|




|



|
|

|

|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <time.h>
#import <stdlib.h>
#import <string.h>

#import "OFTCPSocket.h"
#import "OFExceptions.h"

inline uint16_t get_port()
{
	uint16_t port = (uint16_t)random();

	if (port < 1024)
		port += 1024;

	printf("Using port %d...\n", port);

	return port;
}

int
main()
{
	uint16_t port;

	srandom(time(NULL));

	@try {
		OFTCPSocket *server = [OFTCPSocket new];
		OFTCPSocket *client = [OFTCPSocket new];
		OFTCPSocket *accepted;
		char buf[7];

		puts("== IPv4 ==");
		port = get_port();

		[server bindOn: "localhost"
		      withPort: port
		     andFamily: AF_INET];
		[server listen];

		[client connectTo: "localhost"
			   onPort: port];

		accepted = [server accept];

		[client writeCString: "Hallo!"];
		[accepted readNBytes: 6
			  intoBuffer: (uint8_t*)buf];
		buf[6] = 0;

		if (!strcmp(buf, "Hallo!"))
			puts("Received correct string!");
		else {
			puts("Received INCORRECT string!");
			return 1;
		}

		memset(buf, 0, 7);
		
		[accepted free];
		[client close];
		[server close];

		puts("== IPv6 ==");
		port = get_port();

		[server bindOn: "localhost"
		      withPort: port
		     andFamily: AF_INET6];
		[server listen];

		[client connectTo: "localhost"
			   onPort: port];

		accepted = [server accept];

		[client writeCString: "IPv6:)"];
		[accepted readNBytes: 6
			  intoBuffer: (uint8_t*)buf];
		buf[6] = 0;

		if (!strcmp(buf, "IPv6:)"))
			puts("Received correct string!");
		else {
			puts("Received INCORRECT string!");
			return 1;
		}

		[accepted free];