xref: /haiku/src/tests/system/network/icmp/tcp_unreachable.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2008-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Yin Qiu
7  */
8 
9 
10 #include <errno.h>
11 #include <netinet/in.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17 
18 
19 int
20 main(int argc, char **argv)
21 {
22 	int sockfd, status;
23 	struct sockaddr_in serverAddr;
24 
25 	if (argc != 3)
26 	{
27 		fprintf(stderr, "Usage: %s <ip-address> <port>\n", argv[0]);
28 		exit(1);
29 	}
30 
31 	memset(&serverAddr, 0, sizeof(struct sockaddr_in));
32 	serverAddr.sin_family = AF_INET;
33 	serverAddr.sin_port = htons(atoi(argv[2]));
34 
35 	sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
36 	if ((status = connect(sockfd, (struct sockaddr*)&serverAddr,
37 			sizeof(struct sockaddr_in))) < 0) {
38 		int e = errno;
39 		fprintf(stderr, "Connection failed. Status: %d\n", status);
40 		fprintf(stderr, "Error: %s\n", strerror(e));
41 		exit(1);
42 	} else {
43 		printf("Connected to remote server.\n");
44 		close(sockfd);
45 		printf("Socket closed.\n");
46 	}
47 	return 0;
48 }
49