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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| Voici le serveur :
#include <stdlib.h>
#include <gtk/gtk.h>
#include "echec.h"
//#include "pion.h"
#include <pthread.h>
#include <winsock2.h>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")
void *Serveur (void *param)
{
sGraphic *pEchec=(sGraphic *)param;
WSADATA WSAData;
WSAStartup(MAKEWORD(2,0), &WSAData);
SOCKET sock;
SOCKET csock;
SOCKADDR_IN sin;
SOCKADDR_IN csin;
char infoE [10];
char infoR [10];
int posxd=0,posyd=0,posxa=0,posya=0;
memset(infoE,0,10);
memset(infoR,0,10);
char Tposxd [2];
char Tposyd [2];
char Tposxa [2];
char Tposya [2];
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr(pEchec->AdServer);//INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(pEchec->Port);
bind(sock, (SOCKADDR *)&sin, sizeof(sin));
listen(sock, 0);
while (1)
{
int sinsize = sizeof(csin);
if ((csock = accept(sock, (SOCKADDR *)&csin, &sinsize)) != INVALID_SOCKET)
{
printf("\n Client Connecte au Servur %s au Port %d \n",pEchec->AdServer,pEchec->Port);
if (pEchec->couleur==0 )
{
}
else
{
memset(infoE,0,10);
sprintf(infoE,"\n%c/%d/%d/%d/%d\r\n",pEchec->Recup_Car,pEchec->posxd_en,pEchec->posyd_en,pEchec->posxa_en,pEchec->posya_en);
send(csock,infoE,sizeof(infoE),0);
pEchec->couleur=2; // bloque le mode 2ème joueur sur la machine physique en attendant le coup du joueur distant
}
if (recv(csock, infoR, sizeof(infoR),0) != -1)
{
sprintf(Tposxd,"%c",infoR[2]); // Copie du caractère dans un tableau tampon
sprintf(Tposyd,"%c",infoR[4]); // Copie du caractère dans un tableau tampon
sprintf(Tposxa,"%c",infoR[6]); // Copie du caractère dans un tableau tampon
sprintf(Tposya,"%c",infoR[8]);
if (infoR[0] != NULL)
{
posxd = atoi(Tposxd); // transtypage du char en int
posyd = atoi(Tposyd);
posxa = atoi(Tposxa);
posya = atoi(Tposya);
pEchec->Car_re=infoR[0];
pEchec->posxd_re=infoR[2];
pEchec->posyd_re=infoR[4];
pEchec->posxa_re=infoR[6];
pEchec->posya_re=infoR[8];
// Prep_RecepSe (pEchec);
pEchec->couleur=0;
}
}
}
}
}
et le client :
#include <stdlib.h>
#include <gtk/gtk.h>
#include "echec.h"
//#include "pion.h"
#include <pthread.h>
#include <winsock2.h>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")
void *Client (void *param)
{
sGraphic *pEchec=(sGraphic *)param;
WSADATA WSAData;
WSAStartup(MAKEWORD(2,0), &WSAData);
SOCKET sock;
SOCKADDR_IN sin;
char infoE [10],infoR [10];
int posxd=0,posyd=0,posxa=0,posya=0;
memset(infoE,0,10);
memset(infoR,0,10);
pEchec->couleur=2;
char Tposxd [2],Tposyd [2],Tposxa [2],Tposya [2];
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr(pEchec->AdServer);
sin.sin_family = AF_INET;
sin.sin_port = htons(pEchec->Port);
connect(sock, (SOCKADDR *)&sin, sizeof(sin));
printf("\n Client Connecte au Servur %s au Port %d \n",pEchec->AdServer,pEchec->Port);
while (1)
{
if (recv(sock, infoR, sizeof(infoR),0) != -1)
{
printf("Message reçu : %s",infoR);
sprintf(Tposxd,"%c",infoR[2]);
sprintf(Tposyd,"%c",infoR[4]);
sprintf(Tposxa,"%c",infoR[6]);
sprintf(Tposya,"%c",infoR[8]);
}
if (infoR[0] != NULL)
{
posxd = atoi(Tposxd);
posyd = atoi(Tposyd);
posxa = atoi(Tposxa);
posya = atoi(Tposya);
pEchec->Car_re=infoR[0];
pEchec->posxd_re=posxd;
pEchec->posyd_re=posyd;
pEchec->posxa_re=posxa;
pEchec->posya_re=posyd;
// Prep_RecepCl (pEchec);
pEchec->couleur=1;
}
if (pEchec->couleur==1)
{
}
else
{
sprintf(infoE,"\n%c/%d/%d/%d/%d\r\n",pEchec->Recup_Car,pEchec->posxd_en,pEchec->posyd_en,pEchec->posxa_en,pEchec->posya_en);
send(sock,infoE,sizeof(infoE),0);
pEchec->couleur=2; // bloque le mode 2ème joueur sur la machine physique en attendant le coup du joueur distant
}
closesocket(sock);
WSACleanup();
}
} |
Partager