首页 > 解决方案 > Xamarin.Forms TcpClient TtlExpired

问题描述

我在用着

TcpClient client = new TcpClient(); 
await client.ConnectAsync(IPAddress.Text, Convert.ToInt32(Port.Text));

然后client.ConnectedTrue但是当我 ping 服务器时

Ping p = new Ping(); 
PingReply reply = p.Send(IPAddress.Text, 3000);

我得到状态:TtlExpired

你有什么想法可能是错的吗?谢谢

标签: c#xamarin.formstcpclient

解决方案


ttl 指定了 Ping 数据包可以被转发的次数。默认值为 128,所以这个错误意味着,数据包在被转发 128 次(在网关/路由器中)后还没有到达目的地。

使用 Ttl 属性指定 ICMP 回显消息在到达其目的地之前可以转发的最大次数。如果报文转发指定次数后仍未到达目的地,则丢弃该报文,ICMP 回显请求失败。发生这种情况时,状态设置为 TtlExpired。

所以使用Ping.Send接受 a 的重载PingOptions,并TtlPingOptions.

Ping pingSender = new Ping ();

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 10 seconds for a reply.
int timeout = 10000;

// Set options for transmission:
// The data can go through 255 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (255, true);

// Send the request.
PingReply reply = pingSender.Send(IPAddress.Text, timeout, buffer, options);

推荐阅读