Voici le bout de code.
WSAStartup a été appelée
void envoi(char pse[],char adip[],unsigned short prt)
{
SOCKET c;
SOCKADDR_IN to;
BOOL sortie = FALSE;
BOOL change = FALSE;
char data[255];
char tosend[255];
char move[10];
char ping[10] = "ping ";
int n = 0;
do
{
c = socket(AF_INET,SOCK_STREAM,0);
if(c == INVALID_SOCKET)
{
printf("Unable to create socket.\n" );
printf("Press any key to terminate." );
getch();
fflush(stdin);
exit(1);
}
else
{
change = FALSE;
n = 0;
to.sin_family = AF_INET;
to.sin_addr.s_addr = inet_addr(adip); // IP Serveur
to.sin_port = htons(prt); // Port à utiliser
if (connect(c,(LPSOCKADDR)&to,sizeof(to)) == SOCKET_ERROR)
{
printf("Unable to connect server.\n" );
printf("Press any key to terminate." );
getch();
fflush(stdin);
exit(1);
}
else // Y a un blem : envoi que la première fois
{ // Il faudrait déconnecter & reconnecter, mais too long on net.
printf("Connected to %s (%d)\n\n",adip,prt);
do
{
n = 0;
printf (": " );
gets(data);
if (strcmp(data,"/end" ) == 0) //quitter
n = 1;
if (strstr(data,"/change" ) != NULL) // change serveur
n = 2;
if (strstr(data,"/name" ) != NULL) //change pseudo (juste pour ce chat)
n = 3;
if (strcmp(data,"/info" ) == 0)
n = 4;
if (strcmp(data,"/test" ) == 0)
n = 5;
switch(n)
{
case 0 : //envoyer data
strcpy(tosend,pse);
strcat(tosend,": " );
strcat(tosend,data);
if (send(c,tosend,sizeof(tosend),0) == SOCKET_ERROR)
printf("->Failed\n" );
else
printf("->Ok\n" );
//faire un accusé de reception avec recv() et le serveur send("->ok" );
//->Ok serait l'accusé
break;
case 1 : //quitter
strcpy(tosend,"Disconnection of " );
strcat(tosend,pse);
send(c,tosend,sizeof(tosend),0);
sortie = TRUE;
break;
case 2 : //change
if (sscanf(data,"%s %s %d",move,adip,&prt) == 3)
{
strcpy(tosend,"Disconnection of " );
strcat(tosend,pse);
strcat(tosend," toward another server." );
send(c,tosend,sizeof(tosend),0);
shutdown(c,SD_BOTH);
closesocket(c);
change = TRUE;
}
else
printf("/change IP Port\n\n" );
break;
case 3 : //pseudo
if (sscanf(data,"%s %s",move,pse) == 2)
printf("Name changed : %s\n\n",pse);
else
printf("/name Name\n\n" );
break;
case 4 : //infos
system("netstat -a" );
break;
case 5 : //test
strcat(ping,adip);
system(ping);
break;
default : //erreur
printf("\nUndefined error.\n" );
break;
}
}
while(sortie != TRUE && change != TRUE);
}
}
}
while(sortie != TRUE);
shutdown(c,SD_BOTH);
if (closesocket(c) == SOCKET_ERROR)
printf("Close error.\n" );
WSACleanup();
}
Amadeus, j'ai pas réussi async