-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
80 lines (63 loc) · 2.6 KB
/
Copy pathserver.c
File metadata and controls
80 lines (63 loc) · 2.6 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
#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[] = "127.0.0.1"; // server IP address
char buff[120]; // buffer for sending and receiving messages
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) {
printf("\nSERVER ERROR: cannot create socket.\n");
exit(1);
}
// Binding server socket address structure
if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nSERVER ERROR: Cannot bind.\n");
close(listenfd);
exit(1);
}
// Listen to client connection requests
if (listen(listenfd, 5) < 0) {
printf("\nSERVER ERROR: Cannot listen.\n");
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) {
printf("\nSERVER ERROR: Cannot accept client connections\n");
close(listenfd);
exit(1);
} //upto this
printf("\nSERVER: Connection from client %s accepted.\n", inet_ntoa(cli_addr.sin_addr));
// Waiting for messages from client
if ((r = read(connfd, buff, sizeof(buff) - 1)) < 0) {
printf("\nSERVER ERROR: Cannot receive message from client.\n");
} else {
buff[r] = '\0';
// Echo back the message received from client
if ((w = write(connfd, buff, r)) < 0) {
printf("\nSERVER ERROR: Cannot send message to the client.\n");
} else {
printf("\nSERVER: Echoed back %s to %s.\n", buff, inet_ntoa(cli_addr.sin_addr));
}
}
close(connfd); // Close the connection after handling the client
} // for ends
} // main ends