/// <summary> /// établie la communication avec le serveur /// </summary> /// <param name="nomdecarte">Nom stocker sur la carte FPGA pour vérifier si c'est la bonne carte</param> /// <returns>Renvoie le message d'erreur ou un ok_com</returns> public static string StartClient(string nomdecarte) { cartefpga = new StateObject(); // Connect to a remote device. try { // Establish the remote endpoint for the socket. IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");//se connecte sur un serveur qui tourne sur le pc IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); //attends que la connection ai eu lieu cartefpga.workSocket = client; return(Verif_si_bonne_carte(nomdecarte)); //renvoi si c'est la bonne carte } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString());//inscrit l'erreur dans le log cartefpga.etat_com = e.ToString(); return("error"); } }
static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Fenetre1()); } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString()); } }
/// <summary> /// indique quand la connection fonctionne /// </summary> /// <param name="ar">Statut de la connection</param> private static void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = client.EndSend(ar); // Signal that all bytes have been sent. sendDone.Set(); } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString()); } }
/// <summary> /// indique quand la connection fonctionne /// </summary> /// <param name="ar">Statut de la connection</param> private static void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); // Signal that the connection has been made. connectDone.Set(); } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString()); } }
/// <summary> /// Initialise le début de la réception /// Créer une callback pour savoir quand la donnée a été reçu /// </summary> /// <param name="client"></param> private static void Receive(Socket client) { try { // Create the state object. StateObject state = new StateObject() { workSocket = client, }; // Begin receiving the data from the remote device. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString()); } }
/// <summary> /// Tourne en récursive jusqu'a ce que la valeur a été reçu en entier /// appelé par la réception de donnée /// le retour se fera sur la variable local response /// </summary> /// <param name="ar">Statut de la connection</param> private static void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); // Check if there is anymore data on the socket if (client.Available > 0) { // Get the rest of the data. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } } // si pas de donné lu, ou si le client n'a plus rien à dire if (bytesRead == 0 || client.Available == 0) { // All the data has arrived; put it in response. if (state.sb.Length > 1) { response = state.sb.ToString(); } // Signal that all bytes have been received. receiveDone.Set(); } } catch (Exception e) { GestionLog.Log_Write_Time(e.ToString()); } }
public static void MAJ_Data_puis_envoi(Gestionaire_denvoi gestion_envoi) { Etat_co.Etat_de_connection_actuel = 20;//envoi en cour foreach (IGB_Spéciaux gb in Li_Gb_Spéciaux) { if (gb.A_changé) { gb.MAJ_Datafpga(); try { gestion_envoi.Envoi_data_adresse(gb.Index_de_départ_du_DGV, gb.Nombre_dadresse); } catch (Exception e) { Etat_co.Etat_de_connection_actuel = 14; GestionLog.Log_Write_Time(e.ToString()); } } } Etat_co.Etat_de_connection_actuel = 12; // état de l'envoi terminé }