-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientServer.c
More file actions
208 lines (182 loc) · 4.34 KB
/
Copy pathClientServer.c
File metadata and controls
208 lines (182 loc) · 4.34 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define MAX_MSG_SIZE 1024
#define DEFAULT_PORT 8080
void SendMsg(int sockfd, char *msg)
{
int bytes_sent = send(sockfd, msg, strlen(msg), 0);
if (bytes_sent < 0)
{
perror("Error sending message");
exit(EXIT_FAILURE);
}
}
void RecvMsg(int sockfd, char *buffer)
{
memset(buffer, 0, MAX_MSG_SIZE); // clear the buffer before receiving data
int bytes_received = recv(sockfd, buffer, MAX_MSG_SIZE, MSG_DONTWAIT);
if (bytes_received < 0)
{
if (errno == EWOULDBLOCK || errno == EAGAIN)
{
// no data available, do nothing
return;
}
else
{
perror("Error receiving message");
exit(EXIT_FAILURE);
}
}
else if (bytes_received == 0)
{
// connection closed
printf("Connection closed by peer\n");
exit(EXIT_SUCCESS);
}
else
{
buffer[bytes_received] = '\0';
printf("Received message: %s\n", buffer);
}
}
void TestInput(int sockfd)
{
char buffer[MAX_MSG_SIZE];
fd_set set;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
FD_SET(sockfd, &set);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 10 milliseconds
int ready = select(sockfd + 1, &set, NULL, NULL, &timeout);
if (ready == -1)
{
perror("select error");
exit(EXIT_FAILURE);
}
else if (ready == 0)
{
// no data available to be read
return;
}
else
{
// data available to be read
if (FD_ISSET(STDIN_FILENO, &set))
{
fgets(buffer, MAX_MSG_SIZE, stdin);
SendMsg(sockfd, buffer);
}
if (FD_ISSET(sockfd, &set))
{
RecvMsg(sockfd, buffer);
}
}
}
void RunClient(char *ip_addr, int port)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {0}};
if (inet_pton(AF_INET, ip_addr, &server_addr.sin_addr) < 0)
{
perror("Invalid IP address");
exit(EXIT_FAILURE);
}
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Error connecting to server");
exit(EXIT_FAILURE);
}
char buffer[MAX_MSG_SIZE];
while (true)
{
TestInput(sockfd);
RecvMsg(sockfd, buffer);
}
}
void RunServer(int port)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_addr = {INADDR_ANY},
.sin_port = htons(port)};
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Error binding socket");
exit(EXIT_FAILURE);
}
if (listen(sockfd, 1) < 0)
{
perror("Error listening on socket");
exit(EXIT_FAILURE);
}
printf("Waiting for client to connect...\n");
int client_sockfd = accept(sockfd, NULL, NULL);
if (client_sockfd < 0)
{
perror("Error accepting client connection");
exit(EXIT_FAILURE);
}
printf("Client connected!\n");
char buffer[MAX_MSG_SIZE];
while (true)
{
TestInput(client_sockfd);
RecvMsg(client_sockfd, buffer);
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s [server|client] [ip address (for client)] [port]\n", argv[0]);
exit(EXIT_FAILURE);
}
int port = DEFAULT_PORT;
if (argc == 3)
{
port = atoi(argv[2]);
}
if (strcmp(argv[1], "server") == 0)
{
RunServer(port);
}
else if (strcmp(argv[1], "client") == 0)
{
if (argc < 3)
{
printf("Please specify IP address\n");
exit(EXIT_FAILURE);
}
char *ip_addr = argv[2];
RunClient(ip_addr, port);
}
else
{
printf("Invalid argument: %s\n", argv[1]);
exit(EXIT_FAILURE);
}
return 0;
}