xref: /haiku/src/tests/system/network/at_srv.c (revision 5ac9b506412b11afb993bb52d161efe7666958a5)
1 #include <stdio.h>
2 #include <kernel/OS.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <errno.h>
6 
7 #include "sys/socket.h"
8 #include "netinet/in.h"
9 #include "arpa/inet.h"
10 #include "sys/select.h"
11 
12 // #include "ufunc.h"
13 
14 #define PORT 7772
15 #define HELLO_MSG "Hello from the server\n"
16 #define RESPONSE_MSG "Hello from the client!\n"
17 
18 int main(int argc, char **argv)
19 {
20 	int sock = socket(AF_INET, SOCK_STREAM, 0);
21 	struct sockaddr_in sin;
22 	int salen = sizeof(sin);
23 	int rv, on = 1;
24 	int newsock;
25 	char buffer[50];
26 
27 	// test_banner("Accept Test - Server");
28 
29 	memset(&sin, 0, sizeof(sin));
30 	sin.sin_family = AF_INET;
31 	sin.sin_port = htons(PORT);
32 	sin.sin_len = salen;
33 	sin.sin_addr.s_addr = htonl(INADDR_ANY); // LOOPBACK);
34 	rv = bind(sock, (const struct sockaddr*)&sin, salen);
35 	if (rv < 0)
36 		perror("bind");
37 	printf("Bound\n");
38 
39 	rv = listen(sock, 5);
40 	if (rv < 0)
41 		perror("listen");
42 	printf("Listening\n");
43 
44 	newsock = accept(sock, (struct sockaddr*)&sin, &salen);
45 	if (newsock < 0)
46 		perror("accept");
47 	printf("Accepted socket %d\n", newsock);
48 
49 	rv = write(newsock, HELLO_MSG, strlen(HELLO_MSG));
50 	if (rv < 0)
51 		perror("write");
52 	printf("Written hello\n");
53 
54 	close(newsock);
55 	close(sock);
56 
57 	return (0);
58 }
59 
60 
61