c# - Xamarin.Forms TcpClient TtlExpired
问题描述
我在用着
TcpClient client = new TcpClient();
await client.ConnectAsync(IPAddress.Text, Convert.ToInt32(Port.Text));
然后client.Connected
是True
但是当我 ping 服务器时
Ping p = new Ping();
PingReply reply = p.Send(IPAddress.Text, 3000);
我得到状态:TtlExpired
你有什么想法可能是错的吗?谢谢
解决方案
ttl 指定了 Ping 数据包可以被转发的次数。默认值为 128,所以这个错误意味着,数据包在被转发 128 次(在网关/路由器中)后还没有到达目的地。
使用 Ttl 属性指定 ICMP 回显消息在到达其目的地之前可以转发的最大次数。如果报文转发指定次数后仍未到达目的地,则丢弃该报文,ICMP 回显请求失败。发生这种情况时,状态设置为 TtlExpired。
所以使用Ping.Send
接受 a 的重载PingOptions
,并Ttl
在PingOptions
.
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);
推荐阅读
- multithreading - 如何在 groovy 中使用多线程访问 1000 个端点?
- google-cloud-platform - 扳手 - 如何查找表大小
- google-chrome-extension - 如何从后台脚本更改 BadgeText
- react-native - 如何检测视图是否在反应原生的视口或窗口中可见?
- c# - 保护 API 免受 SQL 注入
- python - 当我将我的 numpy 数组转换为 Dataframe 时,它会将值更新为 Nan
- ios - 如何在 swift 中使用自定义视图?
- python - 填补火花中日期缺失值的优雅方法
- gradle - Firebase admin sdk 对 gradle 的依赖导致启动层错误
- python - Python:RuntimeWarning:在 double_scalars 中遇到除以零