我有一个基本的TCP客户端和服务器,都在计算机上本地运行。
如果在我调用ConnectAsync()时服务器已经在侦听,那么客户端就可以连接到服务器了。
如果我启动客户端并调用ConnectAsync(),然后在侦听客户端之后启动服务器,则它不会建立连接。在出现以下错误之前,ConnectAsync挂起大约85秒钟:System.Net.Sockets.SocketException(0x80004005):连接被拒绝。尽管事实上服务器已经开始监听。
不确定这是否影响TcpClient,但客户端正在Xamarin项目中运行。
这是我的代码:
客户:
public static class DataSource
{
private static TcpClient client;
private static NetworkStream networkStream;
public static bool Connected => client is null ? false : client.Connected;
public static async Task EstablishTcpConnection(string serverIP, int port)
{
CloseTcpConnection();
try
{
client = new TcpClient();
await client.ConnectAsync(IPAddress.Parse(serverIP), port);
networkStream = client.GetStream();
}
catch (Exception ex)
{
Console.WriteLine($"{nameof(EstablishTcpConnection)} Error: {ex}");
}
}
public static void CloseTcpConnection()
{
if (networkStream != null)
{
networkStream.Close();
networkStream.Dispose();
networkStream = null;
}
if (client != null)
{
client.Close();
client.Dispose();
client = null;
}
}
public static async Task SendTcp(string toSend)
{
if (client is null) return;
if (networkStream is null) return;
if (!Connected) return;
if (networkStream != null && networkStream.CanWrite)
{
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(toSend);
await networkStream.WriteAsync(bytesToSend, 0, bytesToSend.Length);
}
}
public static async Task TcpListener()
{
while (networkStream != null && networkStream.CanRead)
{
if (client is null) return;
if (networkStream is null) return;
if (!Connected) return;
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = await networkStream.ReadAsync(bytesToRead, 0, client.ReceiveBufferSize);
string received = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Console.WriteLine($"Received: {received}");
}
}
}
服务器:
internal class Program
{
private const string serverIP = "MyServerIp";
private const int port = myPort;
private static void Main(string[] args)
{
Listener();
Console.ReadLine();
}
public static async Task Listener()
{
//---listen at the specified IP and port no.---
TcpListener listener = new TcpListener(IPAddress.Parse(serverIP), port);
listener.Start();
Console.WriteLine("Listening...");
while (true)
{
//---incoming client connected---
ReceiveClient(await listener.AcceptTcpClientAsync());
}
}
public static async Task ReceiveClient(TcpClient client)
{
if (client is null) return;
Console.WriteLine("Client Connected");
//---get the incoming data through a network stream---
NetworkStream networkStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
while (client != null && client.Connected)
{
//---read incoming stream---
int bytesRead = await networkStream.ReadAsync(buffer, 0, client.ReceiveBufferSize);
if (bytesRead == 0) break;
//---convert the data received into a string---
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received : " + dataReceived);
//---write back the text to the client---
Console.WriteLine("Sending back : " + dataReceived);
networkStream.Write(buffer, 0, bytesRead);
}
client.Close();
client.Dispose();
Console.WriteLine("Client Disconnected");
}
}
答案 0 :(得分:1)
如果您在同一台计算机上运行两个程序,则可能在初始化服务器之前拒绝了ConnectAsync()请求。发生这种情况是因为没有进程监听传入端口。因此,操作系统拒绝了连接请求。
检查引发的异常类型。如果它是SocketException,则其SocketErrorCode属性将使我们更加了解连接失败的原因。
Microsoft网站https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2上提供了错误代码及其描述的完整列表。
答案 1 :(得分:1)
即使服务器在此之后开始侦听并且在超时之内,ConnectAsync()仍然可以正常工作吗?
否;那不是“超时”的意思。 “超时”不是不是暗示“重试”。
建立连接后,客户端应用程序将与服务器计算机上运行的服务器应用程序联系。 “超时”只是客户端应用程序等待响应的时间。如果服务器计算机正在运行,但没有用于该端口的侦听服务器应用程序,则服务器计算机将立即发送响应,指示没有服务器应用程序在运行。这将发送回您的客户端应用程序。这是有效的“响应”,因此超时不会起作用。
如果您想重试连接,等待服务器,则需要自己编写该逻辑。