forked from rubena/telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
45 lines (37 loc) · 1.12 KB
/
client.c
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
#include "mt.h"
void send_cmd(int sock, int pid) {
char str[MAX_MSG_LENGTH] = {0};
printf("> ");
while (fgets(str, MAX_MSG_LENGTH, stdin) == str) {
if(strncmp(str, END_STRING, strlen(END_STRING)) == 0) break;
if(send(sock, str, strlen(str)+1, 0) < 0) perro("send");
}
kill(pid, SIGKILL);
printf("Goodbye.\n");
}
void receive(int sock) {
char buf[MAX_MSG_LENGTH] = {0};
int filled = 0;
while(filled = recv(sock, buf, MAX_MSG_LENGTH-1, 0)) {
buf[filled] = '\0';
printf("%s", buf);
fflush(stdout);
}
printf("Server disconnected.\n");
}
int main(int argc, char **argv) {
if(argc != 2) perro("args");
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1) perro("socket");
struct in_addr server_addr;
if(!inet_aton(argv[1], &server_addr)) perro("inet_aton");
struct sockaddr_in connection;
connection.sin_family = AF_INET;
memcpy(&connection.sin_addr, &server_addr, sizeof(server_addr));
connection.sin_port = htons(PORT);
if (connect(sock, (const struct sockaddr*) &connection, sizeof(connection)) != 0) perro("connect");
int pid;
if(pid = fork()) send_cmd(sock, pid);
else receive(sock);
return 0;
}