-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseServer.c
executable file
·62 lines (56 loc) · 1.72 KB
/
ReverseServer.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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define S_PORT 43454
#define C_PORT 43455
#define ERROR -1
#define IP_STR "127.0.0.1"
void strrev(char *str, int len) {
int i, j;
char temp;
for (i = 0, j = len -1; i < j; ++i, --j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main(int argc, char const *argv[]) {
int sfd, len;
char *str_buf;
struct sockaddr_in servaddr, clientaddr;
sfd = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (sfd == ERROR) {
perror("Could not open a socket");
return 1;
}
memset((char *) &servaddr, 0, sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(S_PORT);
memset((char *) &clientaddr, 0, sizeof(clientaddr));
clientaddr.sin_family=AF_INET;
clientaddr.sin_addr.s_addr=inet_addr(IP_STR);
clientaddr.sin_port=htons(C_PORT);
if((bind(sfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))!=0) {
perror("Could not bind socket");
return 2;
}
printf("Server is running on %s:%d\n", IP_STR, S_PORT);
while(1) {
recvfrom(sfd, &len, sizeof(len), 0, (struct sockaddr *)&clientaddr, (socklen_t *)&clientaddr);
str_buf = (char *) malloc(len*sizeof(char));
recvfrom(sfd, str_buf, len, 0, (struct sockaddr *)&clientaddr, (socklen_t *)&clientaddr);
printf("Client: %s\t", str_buf);
strrev(str_buf,len); //String Reversal
sendto(sfd, &len, sizeof(len), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
sendto(sfd, str_buf, len, 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
printf("\nReverse: %s\n", str_buf);
free(str_buf);
}
return 0;
}