Pour que ce soit plus clair, voici l'exemple de code du serveur :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
struct sockaddr_in SockLocale;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock==-1) exit(-1);
memset(&SockLocale,0,sizeof(struct sockaddr_in));
SockLocale.sin_family = AF_INET;
SockLocale.sin_addr.s_addr = htonl(INADDR_ANY);
SockLocale.sin_port = htons(25);
if(bind(sock,(struct sockaddr*)&SockLocale,sizeof(struct sockaddr_in)) != -1) {
if(listen(sock, 100)<0) {
printf("Echec de Listen()\n");
exit(-1);
}
}
else {
printf("echec de bind()\n");
exit(-1);
}
while(1) {
struct sockaddr_in SockDistant;
char buffer[512];
socklen_t TailleSock = sizeof(SockDistant);
memset(&SockDistant,0,sizeof(struct sockaddr_in));
int s_distant = accept(sock,(struct sockaddr*)&SockDistant,&TailleSock);
if(s_distant!=-1) {
printf("CONNEXION ETABLIE\r\n");
strcpy(buffer,"Hello world !\r\n");
write(s_distant,buffer,strlen(buffer));
printf("Chaine \"Hello world !\" envoyée\r\n");
close(s_distant);
}
}
}
Je teste en me connectant avec telnet :
Si je le mets sur le port 2525 (par ex), tout marche bien.
Sur le port 25, tout marche bien aussi à partir de linux.
Par contre, à partir de windows, j'obtiens (coté serveur) :
CONNEXION ETABLIE
Chaine "Hello world !" envoyée
CONNEXION ETABLIE
Chaine "Hello world !" envoyée
Vous voyez, il me l'affiche en double.
Je ne sais pas si ca ne vient pas la configuration de linux (debian) ou bien du programme.
Je ne comprends pas pourquoi ca marche avec telnet sous linux...