le 17 Mar 2005 13:09
voila le code , en fait je lui demande d'imprimer ce qu'il reçoit ....
et chez d'autre il ont du text en clair :
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <sys/file.h>
#define TAILLE_BUFFER 4096
extern int sys_nerr, errno;
char client_hostname[64];
void set_nonblock(int fd){
int fl;
int x;
x = fcntl(fd, F_SETFL, &fl);
}
int creer_server_sock(char *addr, int port){
int taille_addr, s, on = 1, x;
static struct sockaddr_in client_addr;
s = socket(AF_INET, SOCK_STREAM, 0);
taille_addr = sizeof(client_addr);
memset(&client_addr, ' ', taille_addr);
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr(addr);
client_addr.sin_port = htons(port);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, 4);
x = bind(s, (struct sockaddr *) &client_addr, taille_addr);
x = listen(s, 5);
return s;
}
int open_remote_host(char *host, int port){
struct sockaddr_in rem_addr;
int taille, s, x;
struct hostent *H;
int on = 1;
H = gethostbyname(host);
taille = sizeof(rem_addr);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return s;
taille = sizeof(rem_addr);
memset(&rem_addr, ' ', taille);
rem_addr.sin_family = AF_INET;
memcpy(&rem_addr.sin_addr, H->h_addr, H->h_length);
rem_addr.sin_port = htons(port);
x = connect(s, (struct sockaddr *) &rem_addr, taille);
if (x < 0) {
close(s);
return x;
}
// set_nonblock(s);
return s;
}
int wait_for_connection(int s){
static int newsock, taille;
static struct sockaddr_in peer;
taille = sizeof(struct sockaddr);
newsock = accept(s, (struct sockaddr *) &peer, &taille);
// set_nonblock(newsock);
return (newsock);
}
int mywrite(int fd, char *buf, int *len){
int x ;
x = write(fd, buf, *len);
if ((x < 0)||(x==0)) return x;
if (x != *len) memmove(buf, buf+x, (*len)-x);
*len -= x;
return x;
}
void service_client(int cfd, int sfd){
int maxfd;
char *sbuf;
char *cbuf;
int x, n;
int cbo = 0;
int sbo = 0;
fd_set R;
sbuf = malloc(TAILLE_BUFFER);
cbuf = malloc(TAILLE_BUFFER);
maxfd = cfd > sfd ? cfd : sfd;
maxfd++;
while (1) {
struct timeval to;
mywrite(sfd, cbuf, &cbo);
mywrite(cfd, sbuf, &sbo);
FD_ZERO(&R);
if (cbo < TAILLE_BUFFER)
FD_SET(cfd, &R);
if (sbo < TAILLE_BUFFER)
FD_SET(sfd, &R);
to.tv_sec = 0;
to.tv_usec = 1000;
x = select(maxfd+1, &R, 0, 0, &to);
if (x > 0) {
if (FD_ISSET(cfd, &R)) {
n = read(cfd, cbuf+cbo, TAILLE_BUFFER-cbo);
printf( "read %d bytes from CLIENT (%d)", n, cfd);
printf( "read %s from CLIENT ", cbuf);
if (n > 0) {
cbo += n;
} else {
close(cfd);
close(sfd);
_exit(0);
}
}
if (FD_ISSET(sfd, &R)) {
n = read(sfd, sbuf+sbo, TAILLE_BUFFER-sbo);
printf("read %d bytes from SERVER (%d)
", n, sfd);
printf( "read %s bytes from server", sbuf);
if (n > 0) {
sbo += n;
} else {
close(sfd);
close(cfd);
_exit(0);
}
}
} else if (x < 0 && errno != EINTR) {
close(sfd);
close(cfd);
_exit(0);
}
}
}
int
main(int argc, char *argv[]){
char *localaddr = NULL;
int localport = -1;
char *remoteaddr = NULL;
int remoteport = -1;
int client = -1;
int server = -1;
int master_sock = -1;
if (5 != argc) {
fprintf(stderr, "usage: %s laddr lport rhost rport
", argv[0]);
exit(1);
}
//initialisation des 4 arguments necessaire a une connection client/server
localaddr = strdup(argv[1]);
localport = atoi(argv[2]);
remoteaddr = strdup(argv[3]);
remoteport = atoi(argv[4]);
master_sock = creer_server_sock(localaddr, localport);
while(1) {
client = wait_for_connection(master_sock);
server = open_remote_host(remoteaddr, remoteport);
if (!fork()) {
printf("connection from %s fd=%d
", client_hostname, client);
printf("connected to %s:%d fd=%d
", remoteaddr, remoteport, server);
service_client(client, server);
}
close(client);
close(server);
}
}