1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
void ClientRobot::connecter()//creation socket
{
// Déclarations variables
WSADATA wsadata;
SOCKADDR_IN sin_Adresse_Serveur;
SOCKADDR* pSockAdd; // Pointeur sur structure SOCKADDR
WSAStartup( MAKEWORD( 2, 1), &wsadata );
int EtatConnect=0; // Etat primitive connect
sock = socket(AF_INET,SOCK_STREAM,0 );// Création de la socket en mémoire
if ( sock != INVALID_SOCKET )
{
sin_Adresse_Serveur.sin_family = AF_INET; // Domaine
sin_Adresse_Serveur.sin_port = htons(2000); // Port
sin_Adresse_Serveur.sin_addr.S_un.S_addr = inet_addr("192.168.100.46"); // Adresse IP
memset( sin_Adresse_Serveur.sin_zero, 0 , 8 );
pSockAdd = (SOCKADDR*)&sin_Adresse_Serveur;
EtatConnect = connect( sock,pSockAdd,sizeof(sin_Adresse_Serveur) );
if(EtatConnect==0)
printf("connecte au Robot\n");
else
printf("non connecte au robot\n");
}
else
closesocket( sock );
}
void ClientRobot::attendreReception(char buffer,WSAEVENT &Evt)
{
printf("attendre reception\n");
WSABUF DataBuf;
DataBuf.len=1;
DataBuf.buf = &buffer ;
overl.hEvent=Evt;
Flags = 0;
RecvBytes = 0;
if(WSARecv(sock,&DataBuf,1,&RecvBytes,&Flags,&overl,NULL)==SOCKET_ERROR)
{
int erreur = WSAGetLastError();
if(erreur != WSA_IO_PENDING)
printf("erreur occured at WSARecv\n");
}
else
{
switch(buffer)
{
//traitement caractere
}
}
}
void ClientRobot::lireReception()
{
printf("lire reception \n");
WSAGetOverlappedResult(sock,&overl,&BytesTransferred,FALSE,&Flags);
} |
Partager