-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamLocalhostServer.c
86 lines (77 loc) · 1.8 KB
/
streamLocalhostServer.c
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
/*
* streamLocalhostServer.c
*/
// This program creates a stream Internet socket over localhost
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#define PORT "50000"
#define IP_ADDR "localhost"
#define BACKLOG 10
int main(int argc, char *argv[])
{
// create the socket
int sockFd = socket(AF_INET, SOCK_STREAM, 0);
if (sockFd < 0)
{
perror("socket");
exit(1);
}
// Bind it to a port
struct addrinfo hints;
struct addrinfo *res;
int rc;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((rc = getaddrinfo(NULL, PORT, &hints, &res)) != 0) {
printf("getaddrinfo failed: %s\n", gai_strerror(rc));
exit(1);
}
int retVal = bind(sockFd, res->ai_addr, res->ai_addrlen);
if (retVal < 0)
{
perror("bind");
exit(1);
}
// Start listening
retVal = listen(sockFd, BACKLOG);
if (retVal < 0)
{
perror("listen");
exit(1);
}
int readResult = 10;
char buf[100];
while (1)
{
// accept each connection and read the first
// message sent then close
int clientFd = accept(sockFd, NULL, NULL);
if (clientFd < 0)
{
perror("accept");
continue;
}
readResult = recv(clientFd, buf, sizeof(buf), 0);
if (readResult < 0)
{
perror("recv");
exit(1);
}
printf("Received: %s", buf);
memset(buf, 0, sizeof(buf));
clientFd = close(clientFd);
if (clientFd < 0)
{
perror("close");
exit(1);
}
}
}