c# - 如何为套接字c#设置超时
问题描述
这是一个工作正常的客户端连接。我尝试了几种实现超时和失败(如果客户端无法成功连接到服务器,则抛出异常并关闭套接字。我的代码:
public void connect(string IP, int port)
{
try
{
IPHostEntry host = Dns.GetHostEntry(ip);
IPAddress ipAddress = host.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
// Connect to Remote EndPoint
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
解决方案
推荐阅读
- c# - SQLite连接查询
返回 0 但查询 正确返回 - node.js - 当来自客户端(不同服务器)的请求时,Typescript Express.js API 中出现 CORS 错误
- node.js - NodeJS 的 Amazon S3 SignatureDoesNotMatch 错误
- html - 为什么在我的css网格中最后有额外的网格单元?
- javascript - 在网络服务器上存储和接收 3d 文件的最佳方式
- gnu-parallel - GNU Parallel 中的“--max-args”和“--max-replace-args”之间的区别?
- django - URL 中的“https”但谷歌搜索结果中的“http”
- amazon-web-services - Lambda 调用 update_item,错误:Invalid UpdateExpression:运算符或函数的操作数类型不正确;运算符或函数:+,操作数类型:M
- django - ChoiceFields 在 html 模板和 django 中给出两种不同的形式或不同的字段
- python - 在 Python 中创建数组 N x 1?