-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer1.c
More file actions
143 lines (126 loc) · 4.13 KB
/
Server1.c
File metadata and controls
143 lines (126 loc) · 4.13 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
/*
* Reverse and Uppercase Function:
* - Reverses the given string in-place.
* - Converts all alphabetical characters to uppercase.
*
* This function demonstrates efficient string manipulation by
* processing the string directly in memory.
*/
void reverse_and_uppercase(char *str) {
size_t len = strlen(str);
/* Reverse the string in-place */
for (size_t i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
/* Convert alphabets to uppercase */
for (size_t i = 0; i < len; i++) {
if (isalpha((unsigned char)str[i])) {
str[i] = (char)toupper((unsigned char)str[i]);
}
}
}
int main(void) {
/*
* Initialize WinSock.
* - Required for using sockets on Windows.
* - MAKEWORD(2, 2) specifies version 2.2 of the API.
*/
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) {
fprintf(stderr, "Failed to initialize WinSock.\n");
return 1;
}
/*
* Create a listening socket.
* - Configured for TCP/IP communication (SOCK_STREAM).
* - If the creation fails, print an error and exit.
*/
SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listen_socket == INVALID_SOCKET) {
fprintf(stderr, "Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
/*
* Configure the server's address and port.
* - Use designated initializers for clarity and maintainability.
*/
struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = htons(8080), /* Convert port to network byte order */
.sin_addr.s_addr = INADDR_ANY /* Bind to all network interfaces */
};
/*
* Bind the listening socket to the specified address and port.
* - If the bind operation fails, the server cannot proceed.
*/
if (bind(listen_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
fprintf(stderr, "Bind failed: %d\n", WSAGetLastError());
closesocket(listen_socket);
WSACleanup();
return 1;
}
/*
* Start listening for incoming connection requests.
* - SOMAXCONN allows the maximum number of pending connections.
* - If listening fails, the server cannot accept connections.
*/
if (listen(listen_socket, SOMAXCONN) == SOCKET_ERROR) {
fprintf(stderr, "Listen failed: %d\n", WSAGetLastError());
closesocket(listen_socket);
WSACleanup();
return 1;
}
printf("[INFO] Server1: Waiting for a client to connect...\n");
/*
* Accept a client connection.
* - If `accept()` fails, clean up and exit.
*/
SOCKET client_socket = accept(listen_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET) {
fprintf(stderr, "Accept failed: %d\n", WSAGetLastError());
closesocket(listen_socket);
WSACleanup();
return 1;
}
printf("[INFO] Client connected.\n");
char buffer[1024];
for (;;) {
/*
* Receive data from the client.
* - If `recv()` returns <= 0, the client has disconnected.
*/
int n = recv(client_socket, buffer, (int)(sizeof(buffer) - 1), 0);
if (n <= 0) {
printf("[DEBUG] Client disconnected.\n");
break;
}
buffer[n] = '\0'; /* Null-terminate the received string */
/*
* Process the received message:
* - Reverse the string and convert it to uppercase.
*/
reverse_and_uppercase(buffer);
/* Send the processed string back to the client */
send(client_socket, buffer, (int)strlen(buffer), 0);
}
/*
* Cleanup resources before exiting.
* - Close the client and listening sockets.
* - Terminate WinSock.
*/
closesocket(client_socket);
closesocket(listen_socket);
WSACleanup();
printf("[DEBUG] Server1 shutdown complete.\n");
return 0;
}