Ticket #15081: peerless.c

File peerless.c, 767 bytes (added by leorize, 5 years ago)
Line 
1#include <netinet/in.h>
2#include <sys/socket.h>
3
4#include <errno.h>
5#include <stdio.h>
6#include <string.h>
7
8#define panic(n, str) if (n == -1) { perror(str); return 1; }
9
10int main() {
11 int sock;
12 struct sockaddr_in serv, peer;
13 socklen_t peerlen = (socklen_t)sizeof(peer);
14
15 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
16 panic(sock, "socket");
17
18 memset(&serv, 0, sizeof(serv));
19 serv.sin_family = AF_INET;
20 serv.sin_port = 9123;
21 serv.sin_addr.s_addr = INADDR_ANY;
22
23 panic(bind(sock, (struct sockaddr *)&serv, sizeof(serv)), "bind");
24 panic(listen(sock, 128), "listen");
25
26 if (getpeername(sock, (struct sockaddr *)&peer, &peerlen) != -1 ||
27 errno != ENOTCONN) {
28 printf("Expected ENOTCONN, got %d\n", errno);
29 return 1;
30 }
31
32 return 0;
33}