-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.c
60 lines (52 loc) · 1.59 KB
/
tcp_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <netdb.h>
#define assert(p) if(!(p)){ perror("Assertion non vérifiée:\n\t->"#p"\n\tErreur"); exit(__LINE__); }
#define BUFLEN 256
/* TODO: simple telnet proxy. */
/* YET: just a telnet client */
int main(int argc, char **argv){
char buf[BUFLEN];
struct addrinfo *res, *it, hints;
int sock, i;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
hints.ai_addr = NULL;
hints.ai_next = NULL;
hints.ai_canonname = NULL;
assert(argc > 2);
assert((sock = socket(AF_INET, SOCK_STREAM, 0)) != 0);
assert(getaddrinfo(argv[1], argv[2], &hints, &res) == 0);
for(it = res; it != NULL; it = it->ai_next)
if(connect(sock, res->ai_addr, res->ai_addrlen) != 0)
break;
if(it == NULL){
printf("Erreur: impossible de se connecter.\n");
exit(-1); }
/* Si on lit sur stdin, on écrit sur sock. */
/* Si on lit sur sock, on écrit sur stdout. */
/* donc, on check sock et stdin pour lecture. */
fd_set readfds, writefds, errorfds;
while(1){
FD_ZERO(&readfds);
FD_ZERO(&errorfds);
FD_SET(sock, &readfds);
FD_SET(STDIN_FILENO, &readfds);
FD_SET(sock, &errorfds);
/* on lance la primitive select() */
select(sock+1, &readfds, NULL, &errorfds, NULL);
if(FD_ISSET(sock, &errorfds))
perror("socket");
if(FD_ISSET(sock, &readfds))
write(STDOUT_FILENO, buf, read(sock, buf, sizeof(buf)));
if(FD_ISSET(STDIN_FILENO, &readfds))
write(sock, buf, read(STDIN_FILENO, buf, sizeof(buf)));
}
return 0;
}