更新时间:2023-02-08 09:29:26
Socket套接字=新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType .Tcp);
socket.RecieveTimeout = 1000;
socket.SendTimeout = 1000; //不是需要
// socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,1000);
ip地址ip地址= IPAddress.Parse(192.168.2.2);
INT端口= 9000;
试
{
//可能需要15 - 30秒超时,不使用线程,有
//似乎是没有办法改变这个
socket.Connect(ip地址,端口);
//由于发送超时这个现在将第二个
socket.Send后超时(bytesSent,bytesSent.Length,0);
//这应该现在第二个
字节= socket.Receive后超时(与BytesReceived,bytesReceived.Length,0);
}
终于
{
socket.Close();
}
Socket socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
...
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
...
socket.Send(bytesSent, bytesSent.Length, 0);
...
bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
After socket has sent the data, server does not respond so that program waits for response. How to stop receiving data after 1000 miliseconds? Ы
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.RecieveTimeout = 1000;
socket.SendTimeout = 1000;
// Not needed
// socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
IPAddress ipAddress = IPAddress.Parse("192.168.2.2");
int port = 9000;
try
{
// could take 15 - 30 seconds to timeout, without using threading, there
// seems to be no way to change this
socket.Connect(ipAddress, port);
// Thanks to send timeout this will now timeout after a second
socket.Send(bytesSent, bytesSent.Length, 0);
// This should now timeout after a second
bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
}
finally
{
socket.Close();
}