【问题标题】:getting Socket Exception 10054 when trying to socket.Connect尝试 socket.Connect 时出现 Socket Exception 10054
【发布时间】:2013-12-28 11:35:22
【问题描述】:

1- 我有一个 GHIElectronics FEZ Spider 3 设备

2- 我已通过以太网电缆将设备连接到我的笔记本电脑并配置了网络设置

3- 我可以使用 cmd 从我的笔记本电脑 ping FEZ 设备:ping 172.16.43.193

4- 我编写了一个 Windows 窗体应用程序,可以在我的笔记本电脑上运行并充当 TCP 接收器

5- 我想将 TCP 数据包从 FEZ 设备发送到我笔记本电脑上的 win 应用程序

以下是win app代码:

            string[] dnsAddresses = { "172.16.40.2", "172.16.40.5" };
        const Int32 c_port = 12250;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                System.Threading.Thread t = new Thread(new ThreadStart(SendTCPMessage));
                txtStatus.Text = txtStatus.Text + "TCP listening established successfully\r\n";
            }
            catch (Exception ex)
            {
                txtStatus.Text = txtStatus.Text + "An error occured while trying to establish TCP listening : \r\n" + ex.Message + "\r\n";
                if (ex.InnerException != null)
                    txtStatus.Text = txtStatus.Text + ex.InnerException + "\r\n";
            }
        }

        private void SendTCPMessage()
        {

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);
            server.Bind(localEndPoint);
            server.Listen(1);
            while (true)
            {
                // Wait for a client to connect.
                Socket clientSocket = server.Accept();
                // Process the client request. true means asynchronous.
                new ProcessClientRequest(clientSocket, true);
            }
        }

internal sealed class ProcessClientRequest
        {

            private Socket m_clientSocket;
            /// <summary>
            /// The constructor calls another method to handle the request, but can
            /// optionally do so in a new thread.
            /// </summary>
            /// /// <param name="clientSocket"></param>
            /// <param name="asynchronously"></param>
            public ProcessClientRequest(Socket clientSocket, Boolean asynchronously)
            {
                m_clientSocket = clientSocket;
                if (asynchronously)
                    // Spawn a new thread to handle the request.
                    new Thread(ProcessRequest).Start();
                else ProcessRequest();
            }
            /// <summary>
            /// Processes the request.
            /// </summary>

            private void ProcessRequest()
            {
                const Int32 c_microsecondsPerSecond = 1000000;
                // 'using' ensures that the client's socket gets closed.
                using (m_clientSocket)
                {
                    // Wait for the client request to start to arrive.
                    Byte[] buffer = new Byte[1024];
                    if (m_clientSocket.Poll(5 * c_microsecondsPerSecond,
                    SelectMode.SelectRead))
                    {
                        // If 0 bytes in buffer, then the connection has been closed,
                        // reset, or terminated.
                        if (m_clientSocket.Available == 0)
                            return;
                        // Read the first chunk of the request (we don't actually do
                        // anything with it).
                        Int32 bytesRead = m_clientSocket.Receive(buffer,
                        m_clientSocket.Available, SocketFlags.None);
                        String result = "";
                        string FileContent = new string(Encoding.UTF8.GetChars(buffer));
                        MessageBox.Show("Text file with following content received :\r\n" + FileContent);
                        if (SaveFile(FileContent))
                        {
                            result = "1";
                        }
                        else
                        {
                            result = "0";
                        }

                        // Return a static string to the client.        
                        byte[] buf = Encoding.UTF8.GetBytes(result);
                        int offset = 0;
                        int ret = 0;
                        int len = buf.Length;
                        while (len > 0)
                        {
                            ret = m_clientSocket.Send(buf, offset, len, SocketFlags.None);
                            len -= ret;
                            offset += ret;
                        }
                        m_clientSocket.Close();
                    }
                }
            }

            private bool SaveFile(string FileContent)
            {
                bool returnValue = false;
                try
                {
                    string RootSaveDirectory = ConfigurationManager.AppSettings["FileSaveRootDirectory"].ToString();
                    string SaveDirectoryName = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + "   " + DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString();
                    string SaveDirectory = Path.Combine(RootSaveDirectory, SaveDirectoryName);
                    if (!Directory.Exists(SaveDirectory))
                    {
                        Directory.CreateDirectory(SaveDirectory);
                    }
                    string FileName = ConfigurationManager.AppSettings["FileName"].ToString();
                    File.WriteAllText(Path.Combine(SaveDirectory, FileName), FileContent);
                    returnValue = true;
                }
                catch { }
                return returnValue;
            }
        }

以下是我在 FEZ 设备上使用的代码,用于将 TCP 数据包发送到笔记本电脑上的 win 应用程序:

public bool SendFile()
    {
        bool returnValue = false;
        try
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ServerIP = new IPAddress(new byte[] { 172, 16, 43, 193 });
            IPEndPoint ServerEndPoint = new IPEndPoint(ServerIP, 12250);
            byte[] buf = Encoding.UTF8.GetBytes(FileContentText);
            socket.Connect(ServerEndPoint);
            socket.Send(buf);
            if (socket.Poll(5 * 1000000, SelectMode.SelectRead)) // wait for data from the server
            {
                byte[] inbuf = new byte[socket.Available];
                socket.Receive(inbuf);
                //string m = new string(Encoding.UTF8.GetChars(inbuf));

            }
            socket.Close();
        }
        catch (SocketException ex)
        {
            string s = ex.Message;
            if (ex.InnerException != null)
                s = s + ex.InnerException.Message;
            if (ex.StackTrace != null)
                s = s + ex.StackTrace;
        }
        return returnValue;
    }

问题是在 FEZ 设备上,当我尝试在以下代码行发送 tcp 数据包时

socket.Connect(ServerEndPoint);

抛出具有以下 Id 的 SocketException:

10054

【问题讨论】:

  • 那是“对等连接重置”(见msdn.microsoft.com/en-us/library/windows/desktop/…
  • @C.Evenhuis 我看到了那个页面,我是一个新的套接字编程,你能根据我的代码解释一下我可能有什么问题吗?谢谢
  • 我不确定,但问题可能出在 ProcessClientRequest 构造函数内部。您是如何接收消息的?
  • 将这个庞大的代码 sn-p 简化为两行:创建套接字,连接。然后你有一个很好的复制。不会出现意外错误。
  • @usr 你能用代码 sn-p 指导我吗?

标签: c# sockets tcp .net-micro-framework


【解决方案1】:

我假设在您的情况下,socket.Poll() 在数据到达之前立即返回 false,当它离开 using 块的范围时关闭套接字。

如果我的假设是正确的,我会重写ProcessRequest 方法——跳过Poll()Socket.Available 检查,直接使用socket.Receive()

socket.Receive() 将阻塞(您的工作线程)直到它收到响应,如果远程方已断开连接,则返回 0。

类似:

using (m_clientSocket)
{
    int bytes;
    while ((bytes = m_clientSocket.Receive(...)) > 0)
    {
        // process "bytes" bytes from the buffer
    }

    // other side has disconnected
}

并且不要忘记在该例程中捕捉SocketExceptions。

【讨论】:

    【解决方案2】:

    我知道问题所在了!

    page_load

    之后的事件:

    System.Threading.Thread t = new Thread(new ThreadStart(SendTCPMessage));
    

    我忘记写了:

    t.Start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2019-07-15
      • 2012-12-10
      相关资源
      最近更新 更多