-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.c
More file actions
117 lines (100 loc) · 3.43 KB
/
echo_server.c
File metadata and controls
117 lines (100 loc) · 3.43 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/******************************************************************************
* echo_server.c *
* *
* Description: This file contains the C source code for an echo server. The *
* server runs on a hard-coded port and simply write back anything*
* sent to it by connected clients. It does not support *
* concurrent clients. *
* *
* Authors: Athula Balachandran <abalacha@cs.cmu.edu>, *
* Wolf Richter <wolf@cs.cmu.edu> *
* *
*******************************************************************************/
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define ECHO_PORT 9999
#define BUF_SIZE 4096
int close_socket(int sock)
{
if (close(sock))
{
fprintf(stderr, "Failed closing socket.\n");
return 1;
}
return 0;
}
int main(int argc, char* argv[])
{
int sock, client_sock;
ssize_t readret;
socklen_t cli_size;
struct sockaddr_in addr, cli_addr;
char buf[BUF_SIZE];
fprintf(stdout, "----- Echo Server -----\n");
/* all networked programs must create a socket */
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Failed creating socket.\n");
return EXIT_FAILURE;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(ECHO_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
/* servers bind sockets to ports---notify the OS they accept connections */
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)))
{
close_socket(sock);
fprintf(stderr, "Failed binding socket.\n");
return EXIT_FAILURE;
}
if (listen(sock, 5))
{
close_socket(sock);
fprintf(stderr, "Error listening on socket.\n");
return EXIT_FAILURE;
}
/* finally, loop waiting for input and then write it back */
while (1)
{
cli_size = sizeof(cli_addr);
if ((client_sock = accept(sock, (struct sockaddr *) &cli_addr,
&cli_size)) == -1)
{
close(sock);
fprintf(stderr, "Error accepting connection.\n");
return EXIT_FAILURE;
}
readret = 0;
while((readret = recv(client_sock, buf, BUF_SIZE, 0)) >= 1)
{
if (send(client_sock, buf, readret, 0) != readret)
{
close_socket(client_sock);
close_socket(sock);
fprintf(stderr, "Error sending to client.\n");
return EXIT_FAILURE;
}
memset(buf, 0, BUF_SIZE);
}
if (readret == -1)
{
close_socket(client_sock);
close_socket(sock);
fprintf(stderr, "Error reading from client socket.\n");
return EXIT_FAILURE;
}
if (close_socket(client_sock))
{
close_socket(sock);
fprintf(stderr, "Error closing client socket.\n");
return EXIT_FAILURE;
}
}
close_socket(sock);
return EXIT_SUCCESS;
}