-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPServerStringReversal.c
74 lines (62 loc) · 1.55 KB
/
TCPServerStringReversal.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char* argv[]) {
int servSock, cliSock;
struct sockaddr_in servAdd;
struct sockaddr_in cliAdd;
int servPort;
char message[1024] = {0}, buffer[1024] = {0};
int cliSize, msgLen;
if(argc < 2) {
printf("Usage: %s <ServerPort>\n", argv[0]);
}
servPort = atoi(argv[1]);
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
memset(&servAdd, 0, sizeof(servAdd));
servAdd.sin_family = AF_INET;
servAdd.sin_addr.s_addr = inet_addr("127.0.0.1");
servAdd.sin_port = htons(servPort);
if ((bind(servSock, (struct sockaddr*)&servAdd, sizeof(servAdd))) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
if(listen(servSock, 5) < 0) {
perror ("Error");
exit(EXIT_FAILURE);
}
else {
printf("Listening...\n");
}
cliSize = sizeof(cliAdd);
if ((cliSock = accept(servSock, (struct sockaddr*) &cliAdd, &cliSize)) < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
msgLen = recv(cliSock, message, 1024, 0);
int i = 0;
for(i = 0; i < 10; i++) {
printf("%c", message[i]);
}
printf("\n");
message[strlen(message)] = '\0';
printf("%s\n", message);
printf("\n");
char reverse[1024];
int k = 0, n = strlen(message);
for (i = n-1; i >= 0; i--) {
reverse[k++] = message[i];
}
printf("%s\n", reverse);
// printf(">%s\n", message);
// printf("Type a message for client: ");
// scanf("%s", buffer);
// send(cliSock, buffer, strlen(buffer), 0);
return 0;
}