Пример #1
0
        private void ReceiveDataFromSocketCallback(IAsyncResult asyncResult)
        {
            try
            {
                StateObjectDTO state  = (StateObjectDTO)asyncResult.AsyncState;
                Socket         client = state.workSocket;

                if (!client.Connected)
                {
                    client.EndReceive(asyncResult);

                    _receiveDone.Set();
                    return;
                }

                int bytesRead = client.EndReceive(asyncResult);
                if (bytesRead > 0)
                {
                    if (state.stringBuilder.Length == 0)
                    {
                        state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                        client.BeginReceive(state.buffer, 0, StateObjectDTO.BufferSize, 0, new AsyncCallback(ReceiveDataFromSocketCallback), state);

                        return;
                    }

                    string currentData = state.stringBuilder.ToString().Substring(state.stringBuilder.ToString().IndexOf("INIVARS0$"), state.stringBuilder.ToString().IndexOf("$FINVARS") + 8);

                    string[] currentDataArr = currentData.Split('$');

                    List <string> currentDataList = currentDataArr.ToList();
                    currentDataList.Remove("INIVARS0");
                    currentDataList.Remove("FINVARS");

                    for (int i = 0; i < currentDataList.Count; i++)
                    {
                        _dataTable.Rows.Add(null, currentDataList[i], DateTime.Now);
                        _dataBase.InserDataProcedure(currentDataList[i], DateTime.Now);
                    }

                    state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    client.BeginReceive(state.buffer, 0, StateObjectDTO.BufferSize, 0,
                                        new AsyncCallback(ReceiveDataFromSocketCallback), state);
                }
                else
                {
                    client.EndReceive(asyncResult);

                    _receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("A ocurrido un error recibiendo los datos");
            }
        }
Пример #2
0
        private void ReceiveDataFromSocket()
        {
            try
            {
                StateObjectDTO state = new StateObjectDTO();
                state.workSocket = _socket;

                _socket.BeginReceive(state.buffer, 0, StateObjectDTO.BufferSize, 0,
                                     new AsyncCallback(ReceiveDataFromSocketCallback), state);
            }
            catch (Exception e)
            {
                MessageBox.Show("A ocurrido un error recibiendo los datos");
            }
        }
Пример #3
0
        private void CloseConnection()
        {
            try
            {
                StateObjectDTO state = new StateObjectDTO();
                state.workSocket = _socket;

                _socket.BeginDisconnect(false,
                                        new AsyncCallback(CloseConnectionCallback), state);

                _closeDone.WaitOne();
            }
            catch (Exception ex)
            {
                MessageBox.Show("A ocurrido un error terminando la conexión");
            }
        }
Пример #4
0
        private void CloseConnectionCallback(IAsyncResult asyncResult)
        {
            try
            {
                StateObjectDTO state  = (StateObjectDTO)asyncResult.AsyncState;
                Socket         client = state.workSocket;

                client.EndDisconnect(asyncResult);

                lbl_con_result.Text = "Conexión cerrada";

                client.Disconnect(false);

                _closeDone.Set();
            }
            catch (Exception e)
            {
                MessageBox.Show("A ocurrido un error terminando la conexión");
            }
        }