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
| public class ClientAsyncTCPObject
{
#region variables globales
private Socket socketC;
private FormGAP parent;
#endregion
#region constructeur
public ClientAsyncTCPObject(FormGAP _parent)
{
this.parent = _parent;
}
#endregion
public void connection()
{
socketC = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress adresseServeur = IPAddress.Parse("169.254.221.190");
try
{
IAsyncResult ar = socketC.BeginConnect(new IPEndPoint(adresseServeur, 8001), new AsyncCallback(ConnexionRealisee), socketC);
this.parent.IsConnected = true;
socketC.EndConnect(ar);
}
catch (Exception ex)
{
MessageBox.Show("La connexion a échouée !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
this.parent.IsConnected = false;
}
}
public void ConnexionRealisee(IAsyncResult ar)
{
Socket sock = (Socket)ar.AsyncState;
}
public void EnvoyerRequete(RequeteGAP requete)
{
try
{
Stream streamSocket = new NetworkStream(socketC);
XmlSerializer xs = new XmlSerializer(typeof(RequeteGAP));
xs.Serialize(streamSocket,requete);
}
catch (SocketException ex)
{
MessageBox.Show("Erreur de connexion !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
public RequeteGAP ReceptionRequete()
{
byte[] buf = new byte[1024];
Boolean isComplete = false;
String msgServeur="";
try
{
Stream streamSocket = new NetworkStream(socketC);
XmlSerializer xs = new XmlSerializer(typeof(RequeteGAP));
RequeteGAP maReponse = xs.Deserialize(streamSocket) as RequeteGAP;
if (maReponse.ProtocoleGAP == ProtocolGAP.QUITTER)
{
socketC.Shutdown(SocketShutdown.Both);
socketC.Close();
}
return maReponse;
}
catch (SocketException ex)
{
MessageBox.Show("Erreur de connexion !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
return null;
}
}
public void EnvoiRealise(IAsyncResult ar)
{
}
public void ReceptionRealisee(IAsyncResult ar)
{
}
} |