Tag: tcpclient

等待TcpClient数据可用的最佳方法?

while (TcpClient.Client.Available == 0) { Thread.Sleep(5); } 有一个更好的方法吗?

接受多个tcp客户端的最佳方式?

我有一个客户端/服务器基础设施。 目前,他们使用TcpClient和TcpListener在所有客户端和服务器之间发送接收数据。 我目前所做的是当收到数据时(在它自己的线程上),它被放入一个队列中供另一个线程处理,以释放套接字,使其准备好并打开以接收新数据。 // Enter the listening loop. while (true) { Debug.WriteLine(“Waiting for a connection… “); // Perform a blocking call to accept requests. using (client = server.AcceptTcpClient()) { data = new List(); // Get a stream object for reading and writing using (NetworkStream stream = client.GetStream()) { // Loop to receive all the data […]

C#当我准备好程序结束时,如何停止tcpClient.Connect()进程? 它就在那里坐了10秒钟!

这是我的第一个问题之一。 每当我退出程序时,tcpClient.Connect()都会永远关闭。 我尝试了很多东西,但似乎都没有。 看看CreateConnection()线程,如果客户端还没有连接……我关闭程序,它需要永远关闭。 如果已连接,则立即关闭。 我知道这可以通过某种超时技巧来完成,但我尝试了一些,但没有一个工作。 如果可以,请提供代码示例。 另外,C#在读取/写入实际字节时是否有任何好的教程用缓冲区而不是只有masterServer.writeLine()和masterServer.readline()的版本,或者它们都同样有效吗? 如果你看到其他任何东西可以帮助我改善这一点……无论如何,请继续。 我正在努力教自己如何做到这一点,我没有任何帮助,所以如果你看到它,不要让我继续做错事! 多谢你们! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace RemoteClient { public partial class Form1 : Form { private int MyPort = 56789; private IPAddress myIp = IPAddress.Parse(“210.232.115.79”); […]

C# – TcpClient – 检测流的结束?

我试图将一个古老的网络摄像机连接到我的计算机,我陷入了一个非常根本的问题 – 检测流的结束。 我正在使用TcpClient与摄像头通信,我实际上可以看到它传输命令数据,这里没有问题。 List incoming = new List(); TcpClient clientSocket = new TcpClient(); clientSocket.Connect(txtHost.Text, Int32.Parse(txtPort.Text)); NetworkStream serverStream = clientSocket.GetStream(); serverStream.Flush(); byte[] command = System.Text.Encoding.ASCII.GetBytes(“i640*480M”); serverStream.Write(command, 0, command.Length); 回读响应是问题的开始。 我最初认为像下面的一些代码一样简单: while (serverStream.DataAvailable) { incoming.Add(serverStream.ReadByte()); } 但它没有,所以这次使用ReadByte()进行了另一个版本。 描述说明: 从流中读取一个字节并使流中的位置前进一个字节,如果在流的末尾则返回-1。 所以我想我可以实现以下几点: Boolean run = true; int rec; while (run) { rec = serverStream.ReadByte(); if (rec == -1) […]

在C#中指定用于TCPClient / Socket的传出IP地址

我有一个服务器,其中有几个IP地址分配给网络适配器。 在该服务器上是一个客户端应用程序,通过TCPClient连接到另一个服务器应用程序。 对于所有传出通信,我的服务器使用默认IP地址,但是对于这个应用程序,我希望传出通信在另一个本地IP地址上发送。 是否可以在通信时指定另一个本地分配的IP? 我正在尝试使远程服务器应用程序认为它来自另一个IP,因此它将通过防火墙等…. 提前致谢

c#检测tcp断开连接

我有两个简单的应用程序: 等待特定tcp端口以供客户端连接的服务器应用程序。 然后倾听他的意见,发回一些反馈并断开该客户端的连接。 表单应用程序连接到服务器应用程序,然后说出一些内容,然后等待反馈并断开与服务器的连接,然后在表单中显示反馈。 虽然服务器应用程序似乎行为正常(我已经使用Telnet对其进行了测试,并且我看到反馈并且我看到反馈后直接发生了断开连接),但是表单应用程序似乎并没有注意到与服务器的断开连接 。 (即使在服务器断开连接后,TcpClient.Connected仍然保持为真) 我的问题是:为什么TcpClient.Connected保持正确,我怎么知道服务器是否/何时断开连接? 这是我的完整代码: 表格申请: using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Windows.Forms; using System.Threading; namespace Sender { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void sendButton_Click(object sender, EventArgs e) { TcpClient tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse(“127.0.0.1”), 81); responseLabel.Text = “waiting for […]

tcpclient上的序列化数据需要状态量吗?

我使用TcpClient以字节forms发送数据,我想发送自己的类而不是数据字节。 按字节数据,我的意思是我将数据转换为字节,如下所示: using (MemoryStream bufferStream = new MemoryStream()) { using (BinaryWriter bufferData = new BinaryWriter(bufferStream)) { // Simple PONG Action bufferData.Write((byte)10); } _logger.Info(“Received PING request, Sending PONG”); return bufferStream.ToArray(); } 相反,我想像这样发送它,而不必声明它的大小或w / e public class MyCommunicationData { public ActionType Action { get; set; } public Profile User { get; set; } … } 通常,当我将数据作为字节发送时,前5个字节用于指示操作和消息大小。 但是如果我迁移到将所有数据序列化为单个类,我是否仍然需要发送它的动作和大小,或者使用序列化消息,客户端和服务器将知道要读取的内容等,或者有没有办法这样做我可以发送它而不必指定序列化对象的东西? […]

TCP客户端\服务器 – 客户端并不总是读取

客户代码: TcpClient client = new TcpClient(); NetworkStream ns; private void Form1_Load(object sender, EventArgs e) { try { client.Connect(“127.0.0.1”, 560); ns = client.GetStream(); byte[] buffer = ReadFully(ns, client.Available); //working with the buffer… } catch { //displaying error… } } public static byte[] ReadFully(NetworkStream stream , int initialLength) { // If we’ve been passed an unhelpful initial […]

TcpClient.GetStream()。DataAvailable返回false,但stream有更多数据

因此,似乎阻塞的Read()可以在接收到发送给它的所有数据之前返回。 反过来,我们用一个循环包装Read(),该循环由相关流中的DataAvailable值控制。 问题是你可以在这个while循环中接收更多数据,但是没有幕后处理让系统知道这一点。 我在网上找到的大部分解决方案都不适用于我。 我最后做的是作为循环的最后一步,我从流中读取每个块后做一个简单的Thread.Sleep(1)。 这似乎给系统提供了更新的时间,而且我没有得到准确的结果,但这对于解决方案来说似乎有点苛刻且有点“间接”。 以下列出了我正在处理的情况:IIS应用程序和独立应用程序之间的单个TCP连接,都是用C#编写的,用于发送/接收通信。 它发送请求然后等待响应。 此请求由HTTP请求启动,但我没有从HTTP请求中读取数据这个问题,事后是这样。 以下是处理传入连接的基本代码 protected void OnClientCommunication(TcpClient oClient) { NetworkStream stream = oClient.GetStream(); MemoryStream msIn = new MemoryStream(); byte[] aMessage = new byte[4096]; int iBytesRead = 0; while ( stream.DataAvailable ) { int iRead = stream.Read(aMessage, 0, aMessage.Length); iBytesRead += iRead; msIn.Write(aMessage, 0, iRead); Thread.Sleep(1); } MemoryStream msOut = […]

循环直到TcpClient响应完全读取

我写了一个简单的TCP客户端和服务器。 问题在于客户。 我在阅读服务器的整个响应时遇到了一些麻烦。 我必须让线程hibernate以允许发送所有数据。 我已经尝试过几次将此代码转换为一个循环,直到服务器完成发送数据为止。 // Init & connect to client TcpClient client = new TcpClient(); Console.WriteLine(“Connecting…..”); client.Connect(“192.168.1.160”, 9988); // Stream string to server input += “\n”; Stream stm = client.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(input); stm.Write(ba, 0, ba.Length); // Read response from server. byte[] buffer = new byte[1024]; System.Threading.Thread.Sleep(1000); // […]