简体   繁体   English

在c#中重用TcpClient

[英]Reuse a TcpClient in c#

We have a device that accepts ascii messages over a wireless network, and then responds with a value and I have successfully implemented this in a Windows Forms 2010 solution in c#. 我们有一个设备通过无线网络接受ascii消息,然后用值响应,我已经在c#的Windows Forms 2010解决方案中成功实现了这一点。

I wrote this as a test solution to prove the technology and the actual calls to the device will be made on a Motorola MC55 handheld device. 我写这个作为测试解决方案来证明技术,并且设备的实际调用将在Motorola MC55手持设备上进行。

I have now written an additional test solution in Visual Studio 2008 and installed this onto a device, but the connection to the TcpClient takes around 30 seconds every time its called on the mobile device, where as on the windows solution it's almost instant. 我现在已经在Visual Studio 2008中编写了一个额外的测试解决方案并将其安装到设备上,但是每次在移动设备上调用时,与TcpClient的连接大约需要30秒,而在Windows解决方案上几乎是即时的。

This is far too slow, so I have started looking at reusing the TcpClient, but not had any success and wondered if anyone else has had similar issues and to ask how they got around it, or if anyone can offer any advice on how to reuse a TcpClient so I don't have to create a new one every time I want to make my call. 这太慢了,所以我开始考虑重用TcpClient,但没有取得任何成功,并想知道是否有其他人有类似的问题,并询问他们如何解决它,或者是否有人可以提供任何有关如何重用的建议一个TcpClient所以每次我想打电话时都不需要创建一个新的。

The code for the connection is shown below... 连接的代码如下所示......

     public static string GetValueFromDevice(string deviceIPAddress, int devicePort, string messageSentToDevice)
    {
        string returnValue = "";

        try
        {
            TcpClient client = new TcpClient(deviceIPAddress, devicePort);

            byte[] inData = System.Text.Encoding.ASCII.GetBytes(messageSentToDevice);
            NetworkStream stream = client.GetStream();
            stream.Write(inData, 0, inData.Length);

            byte[] outData = new Byte[256];
            int bytes = stream.Read(outData, 0, outData.Length);
            returnValue = System.Text.Encoding.ASCII.GetString(outData, 0, bytes);

            stream.Close();
            client.Close();
        }
        catch (Exception ex)
        {
            returnValue = ex.Message;
        }

        return returnValue;
    }

You can leave the TcpClient open as long as you like. 只要您愿意,您可以让TcpClient打开状态。 Keep the client and stream variables as part of the class (instead of in the function), and don't call Close() until you're done. 保持clientstream变量作为类的一部分(而不是在函数中),并且在完成之前不要调用Close()

You can persist and re-use the client and stream and place the code into a separate thread. 您可以持久化并重新使用客户端并流式传输并将代码放入单独的线程中。

But, I do not believe the client needs about 30 seconds to initialize. 但是,我不相信客户端需要大约30秒来初始化。 I see that slow init for TCP client when having an ActiveSync connection, but if you have a wireless only connection, it should init immediately. 我发现在使用ActiveSync连接时TCP客户端的启动速度很慢,但是如果你只有一个无线连接,它应该立即启动。

So also check your network setup. 所以还要检查你的网络设置。 After first VS deploy and debug session using Active Sync, Visual Studio is able to re-use the determined wireless IP address and runs subsequent deploy/debug sessions via the wireless TCP/IP (if ActiveSync on PC is set to allow Data connections during AS connections). 在使用Active Sync进行首次VS部署和调试会话后,Visual Studio能够重新使用确定的无线IP地址,并通过无线TCP / IP运行后续部署/调试会话(如果PC上的ActiveSync设置为允许AS期间的数据连接连接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM