- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当使用 TcpClient 和 TcpListener 时抛出这个异常,我有点不知所措。它第一次工作,然后我再次运行它,我得到以下异常:
每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次 127.0.0.1:8086
我已检查以确保关闭所有打开的连接。我已经尝试在 TcpClient 上手动调用 close 以及使用 IDisposable using 模式,但仍然遇到同样的问题。
这是代码,如果您将其复制粘贴到 Visual Studio 中,它应该会运行(前提是您已添加以下 using 语句)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
internal class Program
{
private static void tcpClientConnection()
{
Console.WriteLine("Ready");
Console.ReadKey();
IPAddress address = IPAddress.Parse("127.0.0.1");
using (TcpClient client = new TcpClient(new IPEndPoint(address, 8087)))
{
client.Connect(new IPEndPoint(address, 8086));
using (NetworkStream ns = client.GetStream())
{
ns.Write(System.Text.Encoding.ASCII.GetBytes("Hello"), 0, "Hello".Length);
ns.Flush();
}
Console.WriteLine("Closing client");
}
}
internal static void Main(string[] args)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(new IPEndPoint(address, 8086));
server.Start();
using (Task task2 = new Task(tcpClientConnection))
{
task2.Start();
using (TcpClient client = server.AcceptTcpClient())
{
using (NetworkStream ns = client.GetStream())
{
using (MemoryStream ms = new MemoryStream())
{
ns.CopyTo(ms);
byte[] data = ms.ToArray();
Console.WriteLine(System.Text.Encoding.ASCII.GetString(data));
}
}
}
Console.WriteLine("Server stop");
Console.ReadKey();
server.Stop();
}
Console.WriteLine("END");
Console.ReadKey();
}
}
请注意,我查看了类似问题中提供的解决方案,但没能看出问题所在...
最佳答案
对于 TcpClient,不要指定本地端点。我认为这解决了问题,无论如何你都不应该这样做,因为它什么也做不了。 为传出连接指定本地端点会使操作系统选择适当的可用值。通常,99.9% 的程序都在这样做。
一些程序需要指定本地端点以便只监听特定的 IP 或接口(interface)。看来你不需要那个。
If all the connections are being closed, should it not be ok to continue using the same address and port?
TCP 有一些不直观的行为会导致连接在关闭后停留一段时间。通过让操作系统为您选择一个新的端口,问题就会得到缓解。
您可能还想查看 Task
的用法并获得一些最佳实践:使用 Task.Run
并且不要处置。这是毫无意义的。
关于c# - TcpClient 套接字 - 每个套接字地址异常的唯一用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118639/
要关闭 TcpClient,必须关闭流。通常的做法是: client.GetStream().Close(); client.Close(); 因此仅使用 client
我正在阅读 the documentation on TcpClient.Close()并注意到这一点: Calling this method will eventually result in t
用于同步消息交换的我的客户端类: public class AsClient { private TcpClient connection; public AsClient(int s
我正在制作一个向许多 TCP 监听器发送数据的程序。如果不再使用其中一个 TCP 发送 channel ,我们是否需要使用 TCPClient.close 将其关闭。如果我们让它打开会怎样? 谢谢!
.NET 允许两种非常相似的方式从网络“读取”(假设 TCP 连接): 1. TcpClient.GetStream().Read() 2. TcpClient.Client.Receive() 通过
我找到了一些关于如何用 C# 编写 TCP/IP 客户端-服务器应用程序的代码服务器 Main 以此开头: TcpListener serverSocket = new TcpListener(888
我正在尝试连接到本地网络内的路由器。到目前为止,我已经使用了 TcpClient。 检查我的代码: public static void RouterConnect() {
服务器 : public class TcpServer { private TcpListener tcpListener; private static ManualResetEv
我想使用 TcpClient 发送 HTTP 请求。考虑以下代码: byte[] buf = new byte[1024]; string header = "GET
我在半关闭 TcpClient 时遇到了严重问题。我想做的是: 在客户端: 发送消息 关闭用于发送的底层套接字 收到回复 关闭用于读取的底层套接字(或者,此时,直接关闭它) 在服务器上: 接收消息 关
我有一个客户端和服务器,都有接收和发送方法,并且都使用相同的数据包类、数据包头大小和反序列化等。 如果我只发送较小的文本消息包,一切正常,但是当我尝试发送图像时,缓冲区溢出并且出现内存不足异常。奇怪的
在我的 C# 应用程序中,我有一个线程,它基本上不断地从 TcpClient 读取数据,直到被告知停止。为此,我使用 WaitHandles,例如: private ManualResetEvent
我有一个客户端-服务器程序。 我正在发送这样的数据: private void Sender(string s,TcpClient sock) { try { byte[] b
这个问题在这里已经有了答案: C# network connection running from shared drive (1 个回答) 关闭 7 年前。 我几天前问过类似的问题。那时,我正在尝
我有一个 TCPClient,它创建一个流,当 DataAvailable 时我从中读取流。 每 20 秒 !DataAvailable 我会使用 ACK 消息对套接字执行 ping 操作,以防止流关
我正在使用 SwifSockets:https://github.com/swiftsocket/SwiftSocket :按照有关如何使用它的说明后,我在堆栈溢出上找到了此代码:Sending Me
我在继承的一些代码中有一个奇怪的行为 - 下面的简化示例演示了如果内置到普通控制台应用程序中的问题。 WhoIs 在第 5 次调用时达到其使用限额 - 并返回一条消息 + 关闭套接字。使用 ReadL
我正在用 C# 开发一个 Tcp 客户端,我正在使用 TcpClient 类。我无法连接到服务器。 调试应用程序我可以看到对 Connect 的调用是成功的,但是在我这样做之后,我用 netstat
我有一个我似乎无法弄清楚的问题,请帮忙。我创建了一个类来处理使用 TcpClient 的某些硬件的接口(interface)。我希望此类在 HW 被销毁之前向 HW 发送最后一个命令。 为了解决这个问
您好,感谢您的帮助。这次想问一下TcpClient。我有一个服务器程序,我正在编写一个客户端程序。此客户端使用 TcpClient。首先是创建一个新客户端 clientSocket=new TcpCl
我是一名优秀的程序员,十分优秀!