summaryrefslogtreecommitdiff
path: root/listen.c
blob: b6b4e36f9dc8ab908255da1e09c005a1c181fca6 (plain)
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>

// int listen(int sockfd, int backlog); 

#define BACKLOG 2

int main () {

	int sockfd;
	int gai_result;
	int bind_result;
	struct addrinfo *res;
	struct addrinfo hints;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE;

	gai_result = getaddrinfo(NULL, "62000", &hints, &res);
	if (gai_result != 0) {
		fprintf(stderr, "gai_result: %d\n", gai_result);
		exit(1);
	}

	sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if (sockfd == -1) {
		fprintf(stderr, "sockfd: %d\n", sockfd);
		exit(1);
	}

	bind_result = bind(sockfd, res->ai_addr, res->ai_addrlen);
	if (bind_result != 0) {
		fprintf(stderr, "bind_result: %d\n", bind_result);
		exit(1);
	}

	listen(sockfd, BACKLOG);
	socklen_t addr_size;
	struct sockaddr_storage their_addr;
	addr_size = sizeof(their_addr);
	int newfd;
	newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
	if (newfd == -1) {
		fprintf(stderr, "newfd: %d\n", newfd);
		exit(1);
	}

	char *msg = "Server says hi!\n";
	int len;
	int bytes_sent;
	len = strlen(msg);
	bytes_sent = send(newfd, msg, len, 0);

	size_t rlen = 1024;
	int recv_ret;
	size_t bytes_recieved = 0;
	while(true) {

		char recvmsg[rlen]; 
		recv_ret = recv(newfd, recvmsg, rlen, 0);
		bytes_recieved = bytes_recieved+recv_ret;
		if (recv_ret < 1) {
			break;
		}
		recvmsg[recv_ret] = '\0';
		fprintf(stdout, "%s", recvmsg);
		//fprintf(stdout, "loop");
		memset(&recvmsg, 0, rlen);
	}
	fprintf(stdout, "bytes recieved: %ld\nbytes sent: %d\n", bytes_recieved, bytes_sent);
 
	return 0;
}