Пример #1
0
        private void Nastr_Click(object sender, RoutedEventArgs e)
        {
            pause = false;
            NastrOkn nastr = new NastrOkn();
            nastr.ShowDialog();
            if (DialogOk)
            {
                if (UseComPort)
                {
                    portRecv = 0;
                    serialPort.Close();
                    if (clientSocket!=null)
                    {
                        clientSocket.Close(0);
                    }
                    serialPort.PortName = ComPortName;
                    try
                    {
                        serialPort.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Не открывается порт " + ComPortName+ "\n" + ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        portRecv = 0;
                        serialPort.Close();
                        if (clientSocket != null)
                        {
                            clientSocket.Close(0);
                        }
                        clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        //clientSocket.LingerState.LingerTime = 0;
                        ipAddress = IPAddress.Parse(EthernetServerIp);
                        //Server is listening on port 1000
                        ipEndPoint = new IPEndPoint(ipAddress, 12345);

                        //Connect to the server
                        allDone.Reset();
                        /*IAsyncResult rezultConnect = */clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
                        allDone.WaitOne();
                        //rezultConnect.AsyncWaitHandle.WaitOne();
                        Data msgToSend = new Data();
                        msgToSend.textsend = EthernetUserName;
                        byteData = msgToSend.ToByte();
                        clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                        byteData = new byte[1024];
                        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            pause = true;
        }
Пример #2
0
 private void OnReceive(IAsyncResult ar)
 {
     try
     {
         int read = 0;
         try
         {
             read =clientSocket.EndReceive(ar);
         }
         catch (ArgumentException)
         {
             read = 0;
             //MessageBox.Show(argumentException.Message);   //Object ISyncRezult не был возвращен при повторном подключении к Ethernet
         }
         if(read > 0)
         {
             Data msgReceived = new Data(byteData);
             if (msgReceived.textsend != null)
                 portRecv = Convert.ToInt32(msgReceived.textsend);
             byteData = new byte[1024];
             clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
         }
     }
     catch (ObjectDisposedException)
     { }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }