如何在连接时读取套接字的响应?

时间:2014-03-26 09:40:16

标签: c# vb.net sockets

在我的项目中,我正在维护一个异步Socket连接,以便向手机发送消息。所以,我的问题是这个连接是异步的,所以当很长时间没有数据到达时,远程服务器套接字会自动关闭连接。因此,即使没有数据传输,我的要求是使连接始终保持24小时。我的客户也希望一直看到接收和发送状态。这样他们就能理解正在发生的事情。但我在这方面很新,所以我需要帮助来实现这一需求。任何帮助将不胜感激。

我的问题是:

  1. 当套接字连接到服务器套接字时,它是否可能收到一些字节?即使根本没有数据传输?

  2. 如果连接客户端套接字后收到一些数据,我该如何实现?

  3. 下面是我的BeginReceive()和Callback方法的代码:

      #region Receive & ReceiveCallBack
        public void Receive(Socket client)
        {
            if (m_clientSocket.Connected)
            {
                StateObject state = new StateObject();
                state.workSocket = client;
    
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            }
    
            else
            {
                Connect();
            }
        }
    
        public void ReceiveCallback(IAsyncResult ar)
        {
            if (m_clientSocket.Connected)
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;
                string receiveData = "";
                string sendData = "";
    
                int bytesRead = client.EndReceive(ar);
    
                if (bytesRead > 0)
                {
    
                    state.sb.Append(System.Text.Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
    
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
    
                    receiveData = state.sb.ToString();
                    this.Invoke((MethodInvoker)(() => IsBulk.Items.Add(receiveData)));
    
                    sendData = getReply(receiveData);
                    write_log(DateTime.Now.ToString() + " : " + receiveData, "bytesRead", "bytesRead");
    
                    try
                    {
                        Object objData = sendData;
                        byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                        if (m_clientSocket != null)
                        {
                            m_clientSocket.Send(byData);
                        }
    
                        write_log(DateTime.Now.ToString() + " : " + sendData, "sendData", "sendData");
                    }
                    catch (SocketException se)
                    {
                        logstr = se.Message;
                        write_log(logstr, "WaitForData", "waitfordata");
                    }
    
                }
                else
                {
    
                    receiveData = state.sb.ToString();
                    if (!string.IsNullOrEmpty(receiveData))
                    {
                        write_log(DateTime.Now.ToString() + " : " + receiveData, "receiveData2", "receiveData2");
                    }
    
    
                    receiveDone.Set();
                }
                state.sb.Clear();
            }
            else
            {
                Connect();
            }
        }
        #endregion
    
        private void Connect()
        {
    
            m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
    
            IPAddress ip = IPAddress.Parse("10.151.41.134");
    
            int iPortNo = System.Convert.ToInt16("6561");
    
            IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
    
            m_clientSocket.Connect(ipEnd);
    
            if (m_clientSocket.Connected)
            {
                this.stStatus.Text = "Server Connected";
    
                timer3.Enabled = true;
                timer4.Enabled = true;
            }
            else
            {
                this.stStatus.Text = "Server Failed to Connect";
                m_clientSocket.Shutdown(SocketShutdown.Both);
            }
    
        }
    

    更新:这是我必须在Windows程序中显示的内容:

    enter image description here

    谢谢。

0 个答案:

没有答案