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
| class ServeurReseauAsync
{
#region variables globales
public static byte[] buf = new byte[1024];
public static AutoResetEvent serveurArrete = new AutoResetEvent(false);
public static IPAddress adresseServeur;
public static int port;
public static FormGAPScanServer parent;
public static String protocole;
#endregion
[STAThread]
public static void main()
{
try
{
Socket socketEc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketEc.Bind(new IPEndPoint(adresseServeur, port));
socketEc.Listen(100);
IAsyncResult ar = socketEc.BeginAccept(new AsyncCallback(AcceptRealise), socketEc);
ar.AsyncWaitHandle.WaitOne();
serveurArrete.WaitOne();
}
catch (SocketException ex)
{
MessageBox.Show("Vérifier que vous êtes connecté à un réseau sans fil !", "Avertissement",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}
}
static void AcceptRealise(IAsyncResult ar)
{
Socket socketC = (Socket)ar.AsyncState;
Socket sockC = socketC.EndAccept(ar);
IPEndPoint ep = (IPEndPoint)sockC.RemoteEndPoint;
Boolean fin = false;
try
{
while (!fin)
{
IAsyncResult e = sockC.BeginReceive(buf, 0, buf.Length, 0, new AsyncCallback(ReceptionRealisee), sockC);
e.AsyncWaitHandle.WaitOne();
String msgCli = ASCIIEncoding.Default.GetString(buf, 0, buf.Length);
MessageBox.Show("Message : " + msgCli);
if (msgCli.CompareTo("2$#") == 0)
{
fin = true;
}
}
}
catch (SocketException ex)
{
MessageBox.Show("Perte de connexion hôte.", "Avertissement", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
sockC.Shutdown(SocketShutdown.Both);
sockC.Close();
//serveurArrete.Set();
}
static void ReceptionRealisee(IAsyncResult ar)
{
Socket socketServ = (Socket)ar.AsyncState;
int n = socketServ.EndReceive(ar);
String msgClient = ASCIIEncoding.Default.GetString(buf, 0, n);
String msgServeur = "nogapprotocol";
#region analyse de la trame recue par le client
List<String> maListe = DecoupeTrame.decoupage(msgClient);
protocole = maListe[0];
int IDProtocole = Int32.Parse(protocole);
#region protocole RECHERCHERARTICLE
if (IDProtocole == ProtocolGAP.RECHERCHEARTICLE)
{
String idArticle = maListe[1];
try
{
if (ConfigurationManager.AppSettings["cheminBDMercator"] == null)
throw new ExceptionPropertiesNotFound("Fichier de properties non disponible !");
else
{
String cheminMercator = ConfigurationManager.AppSettings["cheminBDMercator"];
ManipulationDBMercator manip = new ManipulationDBMercator(cheminMercator);
String responseRequete = manip.getStockDispoViaIDArticle(idArticle);
if (responseRequete == null)
{
msgServeur = "null";
}
else
{
msgServeur = responseRequete;
}
}
}
catch (ExceptionPropertiesNotFound ex)
{
}
}
#endregion
#region Protocole QUITTER
if (IDProtocole == ProtocolGAP.QUITTER)
{
}
#endregion
#endregion
byte[] tb = ASCIIEncoding.Default.GetBytes(msgServeur);
IAsyncResult e = socketServ.BeginSend(tb, 0, tb.Length, 0, new AsyncCallback(EnvoiRealise), socketServ);
e.AsyncWaitHandle.WaitOne();
}
static void EnvoiRealise(IAsyncResult ar)
{
Socket socketServ = (Socket)ar.AsyncState;
int n = socketServ.EndSend(ar);
}
} |
Partager