-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpServer.c
executable file
·46 lines (37 loc) · 1.03 KB
/
tcpServer.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "myfunction.h"
#define MAX_BUF_SIZE 1024
int main(int argc, char *argv[]){
struct sockaddr_in client_addr; // struct containing client address information
int i;
ssize_t byteRecv; // Number of bytes received
ssize_t byteSent; // Number of bytes to be sent
socklen_t cli_size;
char receivedData [MAX_BUF_SIZE]; // Data to be received
char sendData [MAX_BUF_SIZE]; // Data to be sent
cli_size = sizeof(client_addr);
for(;;){
byteRecv = recv(0, receivedData, MAX_BUF_SIZE, 0);
if(byteRecv == -1){
perror("recvfrom");
exit(EXIT_FAILURE);
}
if(strncmp(receivedData, "exit", byteRecv) == 0){
printf("Command to stop server received\n");
break;
}
convertToUpperCase(receivedData, byteRecv);
byteSent = send(1, receivedData, byteRecv, 0);
if(byteSent != byteRecv){
perror("send");
exit(EXIT_FAILURE);
}
}
return 0;
}