Programmation des sockets sous windows

Patrice N -  
 Patrice N. -
Salut,
je desire savoir ce qu´il y´a lieu de faire pour que le serveur puisse accepter plus d´une fois l´envoie des données d´un même client.Quand le client le fait la première fois, le serveur récoit et malheureusement il ne recoit plus après.
J´utilise vc++ sous windows.

Merci d´avance
A voir également:

9 réponses

Patrice N.
 
Salut,
Si cette boucle existe à partir de for(;;),je te fais voir le programme en entier:

#include <stdio.h>
#include <sys/types.h>
#include <windows.h>
#include <winsock.h>
#define bufsize 50
#pragma comment(lib, "wsock32.lib")

/*This function is called when a system call fails*/
void error(char *msg)
{
perror(msg);
exit(1);
}


/*sockfd and newsockfd are file descriptors,portno stores the port number on which
the server accepts connections,clilen stores the size of the address of the client,
and n contains the number of characters read or written*/
int main(void)
{
int sockfd,newsockfd,clilen,n,threadID;
char *ch;
unsigned char buffer[bufsize];
struct sockaddr_in serv_addr,cli_addr;
WSADATA WSAData; //data for socket lib initialisation
WSAStartup(MAKEWORD(2,0),&WSAData);

/*The socket() system call creates a new socket*/

sockfd = socket(AF_INET,SOCK_STREAM,0);

if (sockfd < 0)
error("ERROR opening socket");

memset((char *) &serv_addr,0, sizeof(serv_addr)); //clear our address
serv_addr.sin_family =AF_INET;
serv_addr.sin_port = htons(3000);
serv_addr.sin_addr.s_addr = INADDR_ANY;

/*bind the socket to the internet address*/
if (bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
closesocket(sockfd);
return(INVALID_SOCKET);
}

/*The listen system call allows the process to listen on the socket for connections*/
listen(sockfd,3);
//la boucle commence ici
for(;;){
clilen=sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0){
fprintf(stderr,"Error waiting for new connection!\n");
exit(1);
}
strncpy(buffer,"",bufsize);
n= recv(newsockfd,buffer,bufsize,0);
if (n<0)
error("ERROR reading from socket");
printf("The values received are:%s\n", buffer);
printf("\n");
printf("Temperature\t");
printf("Pressure\t");
printf("Humidity\n");
printf("\n");
//here I break the content of the buffer into pieces
ch=strtok(buffer,"x");
while (ch!=NULL)
{
printf("%s\t",ch);
ch = strtok(NULL," x");
}
printf("\n");
printf("\n");
n= send(newsockfd,"I got your values",18,0);
if (n<0) error("ERROR writing to socket");
}
printf("This is the end of transmission");
closesocket(newsockfd);

WSACleanup();

return 0;

}

Merci
1
arth Messages postés 10414 Statut Contributeur 1 293
 
Si tu pouvais donner le bour du programme ou ca coince on pourra déjà voir ce qui ne va pas. sinon on ne sait pas ce que tu as fait.
0
Patrice N.
 
Salut,
le bout du programme serveur:

listen(sockfd,3);
for(;;){
clilen=sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0){
fprintf(stderr,"Error waiting for new connection!\n");
exit(1);
}
strncpy(buffer,"",bufsize);
n= recv(newsockfd,buffer,bufsize,0);
if (n<0)
error("ERROR reading from socket");
printf("The values received are:%s\n", buffer);
printf("\n");
printf("Temperature\t");
printf("Pressure\t");
printf("Humidity\n");
printf("\n");
//here I break the content of the buffer into pieces
ch=strtok(buffer,"x");
while (ch!=NULL)
{
printf("%s\t",ch);
ch = strtok(NULL," x");
}
printf("\n");
printf("\n");
n= send(newsockfd,"I got your values",18,0);
if (n<0) error("ERROR writing to socket");
}
printf("This is the end of transmission");
closesocket(newsockfd);

WSACleanup();

return 0;

}

Merci d´avance
0
arth
 
je suis pas sur de bien tout voir dans la globalité mais il me semble qu'il n'y a pas de boucle de repiquage dans le programme pour recevoir de nouvelles données via la socket client.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Patrice N.
 
Merci arth,
J´ai pu enfin créer un autre loop qui me permet de recevoir les données d´un même client autant de fois que je voudrais.Cependant je desire aussi recevoir simultanement les données de plus d´un client et ne sais pas vraiment comment m´y prendre.
J´ai appris que je pouvais me servir de la fonction CreateThread(), mais malheureusement, je n´ai pas trouver assez d´infos pour pouvoir bien l´utiliser et où dans mon programme.

Merci de me repondre.
0
arth Messages postés 10414 Statut Contributeur 1 293
 
Ui en effet tu peux utiliser la fonction create thread bin utile. je cherche et je te dis
0
Patrice N.
 
Salut Erth,

J´attends donc que ta réponse.

Merci,
0
arth Messages postés 10414 Statut Contributeur 1 293
 
voila j'ai trouvé ce lien :

https://khayyam.developpez.com/articles/cpp/multithreads/

je cherche si j'ai encore le source de mon serveur multithread c++
0
Patrice N.
 
Je pense que celui me sera d´un grand secours même comme je suis entrain de programmer en C.

Merci
0