-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuds-echo-server.c
More file actions
50 lines (34 loc) · 1.2 KB
/
Copy pathuds-echo-server.c
File metadata and controls
50 lines (34 loc) · 1.2 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
/*
Write your code here
This function is called everytime a new connection is accepted by the server
Use this function to write data to socket
void write_string_to_socket(int sock_descriptor, char* message, uint32_t length);
Use this function to read data from socket
void read_string_from_socket(int sock_descriptor, char** message, uint32_t *length);
*/
void * process_client_connection(void * ptr)
{
connection_t * conn;
if (!ptr) pthread_exit(0);
conn = (connection_t *)ptr;
printf("Connection received\n");
int terminate_client = 0;
do
{
char *message = NULL;
uint32_t message_length = 0;
/* read message */
read_string_from_socket(conn->sock, &message, &message_length);
printf("Received = %s\n", message);
/* End of operation on this clinet */
if (strcmp(message, "END") == 0)
terminate_client = 1;
write_string_to_socket(conn->sock, message, message_length);
free(message);
} while(!terminate_client);
/* close socket and clean up */
printf("Closing client on socket %d\n", conn->sock);
close(conn->sock);
free(conn);
pthread_exit(0);
}