TCPListener-保持连接状态还是按需连接? [英] TCPListener - Stay Connected or Connect-on-Demand?

查看:91
本文介绍了TCPListener-保持连接状态还是按需连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个关于TCP侦听器和客户端的问题,可能没有一个答案,但是我认为无论如何我都会张贴它以查看人们的想法.

项目描述:
我最近开始尝试使用Arduino硬件,特别是以太网Sheild.我当时正在考虑制造一个IP温度传感器,该温度传感器将连接到运行TCP侦听器的PC并每分钟发送包含当前温度的数据字符串.

问题:
保持与服务器的TCP连接永久保持活动状态或连接到TCP侦听器,发送命令,然后在每次我想交流的东西时终止连接,是否更好?


代码:
下面的代码似乎能够与arduino连接和通信.

Hi Guys,
I have a question regarding TCP Listeners and Clients that probably doesn''t have one answer but i thought i would post it anyway to see what people think.

Project Description:
I have recently started playing around with the Arduino Hardware, in particular the Ethernet Sheild. I was thinking about making an IP Temperature Sensor that would connect to a PC running a TCP Listener and send a data string containing the current temperature every minute.

Question:
Is it better to keep a TCP connection to a server permantly alive or connect to the TCP Listener, send the command and then terminate the connection every time i wish to communicate something?


Code:
The Code below seems to be able to connect and communicate with the arduino.

try
{
    /// Local IP Address.
    IPAddress ip = IPAddress.Parse("127.0.0.1");
    /// Initializes the Listener.
    TcpListener myListener = new TcpListener(ip, 8001);
    /// Start Listeneting at the specified port.
    myListener.Start();
    listbox1.Items.Add("The Listener is running on " + myListener.LocalEndpoint);
    listbox1.Items.Add("Waiting for a connection...");
    Socket mySocket = myListener.AcceptSocket();
    listbox1.Items.Add("Connection accepted from " + mySocket.RemoteEndPoint);

    /// Receives Message From Client.
    byte[] rData = new byte[100];
    int k = mySocket.Receive(rData);
    listbox1.Items.Add("Recieved...");
    for (int i = 0; i < k; i++)
        listbox1.Items.Add(Convert.ToChar(rData[i]));
    ASCIIEncoding tData = new ASCIIEncoding();
    mySocket.Send(tData.GetBytes("response"));
    listbox1.Items.Add("\nSent Response");

    /// Close Connection.
    mySocket.Close();
    myListener.Stop();
}

catch
{
    listbox1.Items.Add("You broke something...");
}



更多信息:
在Arduino方面,我只是使用arduino-0018软件随附的默认"Web Client"示例,并更改了IP地址和端口.

预先感谢,
Alex.



More Information:
On the Arduino side of things i am just using the default "Web Client" example that comes with the arduino-0018 software and changed the IP address and port.

Thanks in advance,
Alex.

推荐答案

好吧,客户端几乎可以断开连接,但是如果您想强制该问题,则可以在此断开连接.服务器端.这实际上取决于您的喜好.如果您也编写了客户端代码,我认为让客户端发送信息然后断开连接会更好.服务器上的侦听器将自行重置(或应重置),并等待下一个连接.
Well, it''s pretty much up the client to break the connection, but if you wanted to force the issue, you could break it on the server side. It''s really a matter of what you prefer. If you wrote the client code as well, I think it would be better to have the client send the info and then break the connection. The listener on the server will reset itself (or should), and wait for the next connection.


这篇关于TCPListener-保持连接状态还是按需连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆