Nico5779-2 dummy mioche

Inscrit le: 02 Mar 2003 Messages: 2
|
Posté le: Dim Mar 02, 2003 8:23 pm Sujet du message: Savoir si un packet est le dernier recu. |
|
|
Salut
tout d'abord désolé pour le mutlinick mais le mail arrivait pas...
J'ai un prob avec le code suivant (au niveau du commentaire, vers la fin)
Code: |
#ifndef WIN32
#define closesocket(s) close(s);
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pwd.h>
#else
#define WIN32
#include <windows.h>
#include <winsock.h>
#endif
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>
#define SIZEOFBUFFER 2000
#define WEBPORT 2000
#define LOCALPORT 80
#define QLEN 6
extern int errno;
char localhost[] = "192.168.0.81";
char * filename ="d:/mod/test/Debug/tmp.dat";
int visits = 0;
void main ()
{
ofstream file (filename, ios::in|ios::binary|ios::ate);
int size=0;int j = 0;
struct hostent *ptrh_cli;
struct protoent *ptr_serveur,*ptrp_client;
struct sockaddr_in serveur,sad_cli,sad_serv;
int port_cli,sd_cli,n_cli,alen,port,sd,sd2,n,i = 0;
char buffer[SIZEOFBUFFER],buf_cli[SIZEOFBUFFER];
char *host_cli;
while (i < SIZEOFBUFFER)
{
buffer[i] = '*';
buf_cli[i] = '*';
i++;
}
#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101 , &wsaData);
#endif
memset((char *)&sad_serv,0,sizeof(sad_serv));
sad_serv.sin_family = AF_INET;
sad_serv.sin_addr.s_addr = INADDR_ANY;
port = WEBPORT;
sad_serv.sin_port = htons((u_short) port);
if (((int)(ptr_serveur = getprotobyname("tcp"))) ==0)
{
cerr<<"failed to get prtocol by name"<<endl;
exit(1);
}
sd = socket(PF_INET, SOCK_STREAM , ptr_serveur->p_proto);
if (sd < 0)
{
cerr<<"erreur while initializing socket"<<endl;
exit(1);
}
if (bind(sd,(struct sockaddr *)&sad_serv, sizeof(sad_serv))<0)
{
cerr<<"error while binding socket"<<endl;
exit(1);
}
if (listen(sd ,QLEN) >= 0)
{
cout<<"Listening port "<<port<<"..."<<endl;
}
else
{
cerr<<"failed to listen port "<<port<<" ..."<<endl;
exit(1);
}
memset((char *) &sad_cli,0,sizeof(sad_cli));
sad_cli.sin_family = AF_INET;
port_cli = LOCALPORT;
sad_cli.sin_port = htons ((u_short)port_cli);
host_cli = localhost;
ptrh_cli = gethostbyname(host_cli);
if (((char *)ptrh_cli) == NULL)
{
cerr<<"host unknown"<<host_cli<<endl;
exit(1);
}else
{
cout<<"attempting to reach "<<(char *)ptrh_cli->h_name<<" ..."<<endl;
}
memcpy(&sad_cli.sin_addr , ptrh_cli->h_addr,ptrh_cli->h_length);
if (((int)(ptrp_client = getprotobyname("tcp"))) == 0)
{
cerr<<"failed to get prtocol by name"<<endl;
exit(1);
}
//listen(sd,QLEN);
while (1)
{
alen = sizeof(serveur);
if ((sd2=accept(sd,(struct sockaddr *)&serveur ,&alen))<0)
{
fprintf(stderr,"accept failed\n");
exit(1);
}else
{
cout<<endl<<"conection id: "<<visits<<endl;
}
visits++;
i = 0;
sd_cli = socket(PF_INET, SOCK_STREAM, ptrp_client->p_proto);
if(sd_cli < 0)
{
cerr<<"erreur while initializing socket"<<endl;
exit(1);
}
if (connect(sd_cli,(struct sockaddr *)& sad_cli,sizeof(sad_cli)) >= 0)
{
cout<<"*** connected to "<<localhost<<" ***"<<endl;
}else
{
cerr<<"attempt failed ... aborting"<<endl;
exit(1);
}
n = recv(sd2,buffer,sizeof(buffer),0);
send (sd_cli,buffer,n,0);
n_cli=5000;
while (n_cli != 0 && n_cli != -1)
{
n_cli = recv(sd_cli,buf_cli,sizeof(buf_cli),0);
/*
ici ca glande après le dernier buffer recu
logique, le client n'en envoit plus, mais comment
savoir que le dernier recu est bien le dernier.
*/
if (n_cli != 0)
send (sd2,buf_cli,n_cli,0);
cout<<n_cli<<"bytes transfered"<<endl;
}
closesocket(sd_cli);
closesocket(sd2);
//file.close();
cout<<endl<<"******************************"<<endl;
}
}
|
voila (ce code est sencé faire un tunnel comme l'exemple en c dans outils)
Ma question est donc comment savoir si un packet est le dernier recu, pour ne plus attendre et/ou comment peut on definir le délais d'attente max?Merci d'avance. |
|