-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.c
More file actions
103 lines (84 loc) · 2.95 KB
/
Client.c
File metadata and controls
103 lines (84 loc) · 2.95 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
static void init_winsock() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed\n");
exit(EXIT_FAILURE);
}
}
static void cleanup_winsock() {
WSACleanup();
}
#define PORT 8080
int main() {
init_winsock();
int client_fd;
struct sockaddr_in server_address;
char buffer[1024] = {0};
char message[1024] = {0}; // Allocate and initialize memory for the message
// Create the socket
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == INVALID_SOCKET) {
fprintf(stderr, "Socket creation failed: %d\n", WSAGetLastError());
cleanup_winsock();
exit(EXIT_FAILURE);
}
// Configure server address
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
// Connect to the server
if (connect(client_fd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
fprintf(stderr, "Connection failed: %d\n", WSAGetLastError());
closesocket(client_fd);
cleanup_winsock();
exit(EXIT_FAILURE);
}
printf("Connected to the server.\n");
// Communicate with the server
while (1) {
// Input message
printf("Enter your message (Enter Exit Server to exit): ");
if (!fgets(message, sizeof(message), stdin)) {
fprintf(stderr, "Error reading input\n");
break;
}
// Remove trailing newline from fgets
message[strcspn(message, "\n")] = '\0';
// Send message to the server
if (send(client_fd, message, strlen(message), 0) < 0) {
fprintf(stderr, "Failed to send message\n");
break;
}
printf("Sent to server: %s\n", message);
// Check for exit condition
if (strcmp(message, "Exit Server") == 0) {
printf("Exiting as per user request.\n");
break;
}
// Receive response from the server
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0); // Leave space for null-terminator
if (bytes_received < 0) {
fprintf(stderr, "Failed to receive data\n");
break;
} else if (bytes_received == 0) {
printf("Server disconnected.\n");
break;
}
// Null-terminate received string
buffer[bytes_received] = '\0';
printf("Received from server: %s\n", buffer);
// Clear buffers for the next message
memset(buffer, 0, sizeof(buffer));
memset(message, 0, sizeof(message));
}
// Close the socket and clean up
closesocket(client_fd);
cleanup_winsock();
return 0;
}