-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitserver.c
More file actions
96 lines (78 loc) · 3.38 KB
/
Copy pathitserver.c
File metadata and controls
96 lines (78 loc) · 3.38 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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
struct sockaddr_in serv_addr, cli_addr;
int listenfd, connfd, r, w, cli_addr_len;
unsigned short serv_port = 25020; // port number to be used by the server
char serv_ip[] = "192.168.21.25"; // server IP address
int main() {
// Initializing server socket address structure with zero values
bzero(&serv_addr, sizeof(serv_addr));
// Filling up the server socket address structure with appropriate values
serv_addr.sin_family = AF_INET; // address family
serv_addr.sin_port = htons(serv_port); // port number
inet_aton(serv_ip, &serv_addr.sin_addr); // IP address
printf("\nTCP ECHO SERVER.\n");
// Creating socket
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("SERVER ERROR: cannot create socket");
exit(1);
}
// Binding server socket address structure
if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("SERVER ERROR: Cannot bind");
close(listenfd);
exit(1);
}
// Listen to client connection requests
if (listen(listenfd, 5) < 0) {
perror("SERVER ERROR: Cannot listen");
close(listenfd);
exit(1);
}
cli_addr_len = sizeof(cli_addr);
for (;;) {
printf("\nSERVER: Listening for clients... Press Ctrl + C to stop the server.\n");
// Accept client connections
if ((connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &cli_addr_len)) < 0) {
perror("SERVER ERROR: Cannot accept client connections");
continue; // Continue to listen for other clients
}
printf("\nSERVER: Connection from client %s accepted.\n", inet_ntoa(cli_addr.sin_addr));
for (;;) { // For Iterative Chat
char buff[120]; // for reading client message
char sbuff[120]; // for sending server message
// Waiting for messages from client
if ((r = read(connfd, buff, sizeof(buff) - 1)) < 0) {
perror("SERVER ERROR: Cannot receive message from client");
break; // Exit the loop on error
} else if (r == 0) {
printf("Client disconnected.\n");
break; // Exit the loop if client disconnects
}
buff[r] = '\0'; // Null-terminate the received string
printf("Client Message: %s\n", buff); // print client message
// Stop the connection if the client sends "STOP"
if (strcmp(buff, "STOP") == 0) {
printf("Stopping the connection...\n");
break; // Exit the loop
}
printf("Enter Server's message: "); // scans the server's message
fgets(sbuff, sizeof(sbuff), stdin);
// Send back the message to client received from server
if ((w = write(connfd, sbuff, strlen(sbuff))) < 0) {
perror("SERVER ERROR: Cannot send message to the client");
break; // Exit the loop on error
} else {
printf("SERVER: Echoed back %s to %s.\n", sbuff, inet_ntoa(cli_addr.sin_addr));
}
}
close(connfd); // Close the connection with the client
}
return 0;
}