Client/Serveur Linux

imothepe20012002 Messages postés 1 Date d'inscription mardi 29 novembre 2005 Statut Membre Dernière intervention 30 novembre 2005 - 30 nov. 2005 à 13:36
juki_webmaster Messages postés 947 Date d'inscription mercredi 19 novembre 2003 Statut Membre Dernière intervention 5 avril 2008 - 30 nov. 2005 à 14:29
Salut a tous !

Voila, j'ai un tp a rendre, je dois faire sous linux (terminale) un
bibliothéque en ligne. Je m'explique, un serveur posséde des files et
lorsque le client ce connecte, il doit lui proposer les files (pour
cela le serveur fait un recheche dans sont répertoire d'execution). Le
client recupére alors les fichiers qu'il veux.



J'arrive a faire dialoguer le serveur et les clients. Mais je c pas
comment faire pour faire la biblio, pouvez vous m'aidez....merci



Voic le code source serveur:



/* include fig01 */

#include "unp.h"



int

main(int argc, char **argv)

{

int
i, maxi,
maxfd, listenfd, connfd, sockfd;

int
nready,
client[FD_SETSIZE];

ssize_t n;

fd_set rset, allset;

char buf[MAXLINE];

socklen_t clilen;

struct sockaddr_in cliaddr, servaddr;



listenfd = Socket(AF_INET, SOCK_STREAM, 0);



bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);



Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));



Listen(listenfd, LISTENQ);



maxfd = listenfd; /* initialize */

maxi = -1;
/* index into
client[] array */

for (i = 0; i < FD_SETSIZE; i++)

client[i] = -1;
/* -1 indicates available entry */

FD_ZERO(&allset);

FD_SET(listenfd, &allset);

/* end fig01 */



/* include fig02 */

for ( ; ; ) {

rset = allset; /* structure assignment */

nready = Select(maxfd+1, &rset, NULL, NULL, NULL);



if (FD_ISSET(listenfd, &rset)) { /* new client connection */

clilen = sizeof(cliaddr);

connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

#ifdef NOTDEF

printf("new client: %s, port %d\n",


Inet_ntop(AF_INET,
&cliaddr.sin_addr, 4, NULL),

ntohs(cliaddr.sin_port));

#endif



for (i = 0; i < FD_SETSIZE; i++)

if (client[i] < 0) {


client[i] =
connfd; /* save descriptor */

break;

}

if (i == FD_SETSIZE)

err_quit("too many clients");



FD_SET(connfd,
&allset); /* add new descriptor to set */

if (connfd > maxfd)


maxfd = connfd;
/* for select */

if (i > maxi)


maxi = i;
/* max index in client[] array */



if (--nready <= 0)


continue;
/* no more readable descriptors */

}



for (i = 0; i <= maxi; i++) { /* check all clients for data */

if ( (sockfd = client[i]) < 0)

continue;

if (FD_ISSET(sockfd, &rset)) {

if ( (n Read(sockfd, buf, MAXLINE)) 0) {


/*4connection
closed by client */

Close(sockfd);

FD_CLR(sockfd, &allset);

client[i] = -1;

} else

Writen(sockfd, buf, n);



if (--nready <= 0)


break;
/* no more
readable descriptors */

}

}

}

}

/* end fig02 */





##############################################################################



Client:

using namespace std;

#include <string.h>//pour memcpy au moins

#include <stdio.h>//perror

#include <sys/types.h>

#include

#include <netinet/in.h>

#include <netdb.h>

#include

#include <sys/socket.h>

#include <fstream>

#include "sock.h"

#include "sockdist.h"



main(int argc, char*argv[]){

static const int MAXLINE=128;

if (argc<2){

cout<<"utilisation: "< maxfd)


maxfd = connfd;
/* for select */

if (i > maxi)


maxi = i;
/* max index in client[] array */



if (--nready <= 0)


continue;
/* no more readable descriptors */

}



for (i = 0; i <= maxi; i++) { /* check all clients for data */

if ( (sockfd = client[i]) < 0)

continue;

if (FD_ISSET(sockfd, &rset)) {

if ( (n read(sockfd, buf, MAXLINE)) 0) {


/*4connection
closed by client */

close(sockfd);

FD_CLR(sockfd, &allset);

client[i] = -1;


cout<<"close client:
"<<endl;

} else {



if(strcmp(buf,"quit\n")==0){

char bb[]="bye-bye\n";


maBr.writen(sockfd, bb,
MAXLINE);

close(sockfd);


FD_CLR(sockfd, &allset);

client[i] = -1;


cout<<"close client a
la main: "<<endl;

break;

}


char
charClient[MAXLINE]="from server: ";



int nn=strlen(charClient);



for(int i=0;i<strlen(buf);i++)



charClient[nn+i]=buf[i];



maBr.writen(sockfd, charClient, MAXLINE);




char
one_to_one[MAXLINE]="from client1";


for(int i=0;i<strlen(one_to_one);i++)



one_to_one[nn+i]=buf[i];


maBr.writen(sockfd+1,
charClient, MAXLINE);


cout<<"from client: ";

fputs(buf,stdout);



}



if (--nready <= 0)


break;
/* no more
readable descriptors */

}

}

}

}

1 réponse

juki_webmaster Messages postés 947 Date d'inscription mercredi 19 novembre 2003 Statut Membre Dernière intervention 5 avril 2008 3
30 nov. 2005 à 14:29
Salut,

Tu doit gerer une fil d'attente pour gerer un nombre maximum de connecté simultanement ?

->


struct struct_client
{
int numero;
struct sockaddr_in client_csin;
char ip[12];
int sock;
};

struct struct_client *s_client[MAX_CLIENT];
int prochain_client=0;

int main(int argc, char *argv[]) {
// ...
while(1)
{
if(prochain_client==(MAX_CLIENT+1))
{
printf("Nombre de clients maximal atteint, recherche des places libres\n");
for(i=0;i<=MAX_CLIENT;i++)
{
if(s_client[i]==NULL)
{
printf("La place %d est libre\n",i);
prochain_client=i;
break;
}
}
}
else
{
// ont accepte la connexion..
// si bon:
s_client[prochain_client]->numero = prochain_client;
// ici tu lance ce que tu desire
prochain_client++;
}
}
}

et a la coupure de la socket/fin de processus:

s_client[num] = NULL;
0
Rejoignez-nous