diff --git a/tsock_test_Nathan.c b/tsock_test_Nathan.c index 7a7143b..7eb9d9a 100644 --- a/tsock_test_Nathan.c +++ b/tsock_test_Nathan.c @@ -62,16 +62,6 @@ void sendudp(int sock, char* message, int msg_len, void* addr, unsigned int addr } void recvtcp(int sock) { - char buffer[TCP_BUFFER_LEN]; - size_t nb_octed_read = read(sock, buffer, TCP_BUFFER_LEN - 1); - if (nb_octed_read < 0) { - perror("Could not read octed-stream: "); - exit(1); - } else { - buffer[nb_octed_read] = '\0'; - printf("message recieved: %s\n", buffer); - } - } void afficher_message(char* message, int lg) { @@ -173,18 +163,11 @@ int main (int argc, char **argv) } } - /* protocole = 0 (udp) 1(tcp) */ - if (!protocole) /* if (tcp activated) */ { - printf("TCP not implemented\n"); - return 0; + if (protocole) /* udp */ { + sock = socket(AF_INET, SOCK_DGRAM, 0); } else { - printf("using udp\n"); + sock = socket(AF_INET, SOCK_STREAM, 0); } - /* if (protocole) /\* udp *\/ { */ - sock = socket(AF_INET, SOCK_DGRAM, 0); - /* } else { */ - /* sock = socket(AF_INET, SOCK_STREAM, 0); */ - /* } */ if (sock < 0) { perror("Error creating socket:"); exit(1); @@ -209,7 +192,6 @@ int main (int argc, char **argv) memset(&addr_distant, 0, sizeof(addr_local)); addr_distant.sin_family = AF_INET; addr_distant.sin_port = DEFAULTPORT; - printf("%s\n", hostname); hp = gethostbyname(hostname); if (hp == NULL) { printf("hosname not found\n"); @@ -220,14 +202,37 @@ int main (int argc, char **argv) memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length); } else /* tcp activated */{ + if (source) { + memset(&addr_distant, 0, sizeof(addr_local)); + addr_distant.sin_family = AF_INET; + addr_distant.sin_port = DEFAULTPORT; + hp = gethostbyname(hostname); + if (hp == NULL) { + printf("hosname not found\n"); + /* herror("Error: "); */ + exit(1); + } + printf("hostname found\n"); - if (listen(sock, NB_CLIENTS) < 0) { - perror("Server could not listen: "); - exit(1); - } - if (accept(sock, (struct sockaddr*) &addr_distant, &addr_distant_len) < 0) { - perror("Server could not accept connection: "); - exit(1); + memcpy(&addr_distant.sin_addr.s_addr, hp->h_addr, hp->h_length); + + if (connect(sock,(struct sockaddr*)&addr_distant, sizeof(addr_distant)) == -1) { + printf("echec de la connexion\n"); + exit(1); + } + } else { + if (listen(sock, NB_CLIENTS) < 0) { + perror("Server could not listen: "); + exit(1); + } else { + printf("listening\n"); + } + if (accept(sock, (struct sockaddr*) &addr_distant, &addr_distant_len) < 0) { + perror("Server could not accept connection: "); + exit(1); + } else { + printf("accepting request\n"); + } } } @@ -236,9 +241,29 @@ int main (int argc, char **argv) message = malloc((MSG_LENGTH + 1) * sizeof(char)); if (protocole) /* tcp activated */ { if (source) { - ; + printf("nb de tampons a envoyer : %d\n", nb_message); + + for(int i=0;i