Esempio n. 1
0
        private void ReadCallback(IAsyncResult ar)
        {
            try
            {
                string     message;
                HelpObject help   = ((HelpObject)ar.AsyncState);
                Socket     client = help.socket;
                int        length = client.EndReceive(ar);
                if (length > 1)
                {
                    help.sb.Append(Encoding.ASCII.GetString(help.buffer, 0, length)); //einlesen
                    message = help.sb.ToString();

                    if (message.IndexOf("<EOF>") < 0)
                    {
                        //wenn noch nicht alles eingelesen wurde, vorgang wiederholen
                        client.BeginReceive(help.buffer, 0, HelpObject.BufferSize, SocketFlags.None, new AsyncCallback(ReadCallback), help);
                    }
                    else
                    {
                        //Antwort senden
                        ArduinoIP = client.RemoteEndPoint.ToString().Split(':')[0];
                        string reponse = Processing(message.Substring(0, message.IndexOf("<EOF>")));
                        Console.WriteLine("Answer: " + reponse);
                        Send(client, reponse);
                    }
                }
            }
            catch (Exception) { }
        }
Esempio n. 2
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         HelpObject help   = (HelpObject)ar.AsyncState;
         Socket     client = help.socket;
         int        length = client.EndReceive(ar); //Empfangen beenden
         if (length > 0)
         {
             help.sb.Append(Encoding.ASCII.GetString(help.buffer, 0, length));
             string message = help.sb.ToString();
             if (message.IndexOf("<EOF>") < 0)
             {
                 //Wenn Nachricht noch nicht komplett ausgelesen, Vorgang wiederholen
                 client.BeginReceive(help.buffer, 0, HelpObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), help);
             }
             else
             {
                 //Sockets schließen
                 Console.WriteLine("Reponse: " + message + "\n");
                 client.Shutdown(SocketShutdown.Both);
                 client.Close();
             }
         }
     }
     catch (Exception) { new Client().StartClient(IP, me); }
 }
Esempio n. 3
0
        private void AcceptCallback(IAsyncResult ar)
        {
            manuelresetevent.Set(); //server wieder freistellen
            Socket     client = ((Socket)ar.AsyncState).EndAccept(ar);
            HelpObject help   = new HelpObject {
                socket = client
            };                                                                                                                   // Socket für die Übergabe speichern

            client.BeginReceive(help.buffer, 0, HelpObject.BufferSize, SocketFlags.None, new AsyncCallback(ReadCallback), help); //Empfang der Daten beginnen
        }
Esempio n. 4
0
 private void Receive(Socket client)
 {
     try
     {
         HelpObject help = new HelpObject {
             socket = client
         };
         client.BeginReceive(help.buffer, 0, HelpObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), help); //Empfangen starten
     }
     catch (Exception) { }
 }
Esempio n. 5
0
 private void ConnectCallback(IAsyncResult ar)
 {
     try
     {
         HelpObject help   = (HelpObject)ar.AsyncState;
         Socket     client = help.socket;
         client.EndConnect(ar);
         Console.WriteLine("Socket connected to " + client.RemoteEndPoint.ToString());
         Send(help.socket, help.sb.ToString()); //Nachricht senden
     }
     catch (Exception) { }
 }
Esempio n. 6
0
        public void StartClient(string IPAddress_, string message) //Nachricht an einen Netzwerkteilnehmer senden
        {
            IP = IPAddress_; me = message;
            IPAddress  address  = IPAddress.Parse(IPAddress_);
            IPEndPoint endPoint = new IPEndPoint(address, HelpObject.arduinoport);
            Socket     client;

            try
            {
                client = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                HelpObject help = new HelpObject {
                    socket = client
                };
                help.sb.Append(message);
                client.BeginConnect(endPoint, new AsyncCallback(ConnectCallback), help); //Verbindung versuchen aufzubauen
            }
            catch (Exception) { }
        }