Socket m_clientSocket = null;
byte[] m_receiveBuffer = new byte[1024];
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(server), port);
try
{ m_clientSocket.Connect(remoteEndPoint);
if (m_clientSocket.Connected)
{
fwqzckf();
}
m_clientSocket.Close();
}
catch (Exception)
{
/*MessageBox.Show(e.ToString());*/
fwqyjgb();
m_clientSocket = null;
}
这段代码检测远程服务器 如果远程服务器没有开启 检测时间很长 才能返回错误信息
请问如何能够加快处理呢
byte[] m_receiveBuffer = new byte[1024];
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(server), port);
try
{ m_clientSocket.Connect(remoteEndPoint);
if (m_clientSocket.Connected)
{
fwqzckf();
}
m_clientSocket.Close();
}
catch (Exception)
{
/*MessageBox.Show(e.ToString());*/
fwqyjgb();
m_clientSocket = null;
}
这段代码检测远程服务器 如果远程服务器没有开启 检测时间很长 才能返回错误信息
请问如何能够加快处理呢
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 3000);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
private void connect(string ipAdd,string port)
{
try
{
SocketAsyncEventArgs e=new SocketAsyncEventArgs();
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(serverIp);
int iPortNo = System.Convert.ToInt16(serverPort);
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo); //m_clientSocket.
e.RemoteEndPoint = ipEnd;
e.UserToken = m_clientSocket;
e.Completed+=new EventHandler<SocketAsyncEventArgs>(e_Completed);
m_clientSocket.ConnectAsync(e); if (timer_connection != null)
{
timer_connection.Dispose();
}
else
{
timer_connection = new Timer();
}
timer_connection.Interval = 2000;
timer_connection.Tick+=new EventHandler(timer_connection_Tick);
timer_connection.Start();
}
catch (SocketException se)
{
lb_connectStatus.Text = "Connection Failed";
MessageBox.Show(se.Message);
}
}
private void e_Completed(object sender,SocketAsyncEventArgs e)
{
lb_connectStatus.Text = "Connection Established";
WaitForServerData();
}
private void timer_connection_Tick(object sender, EventArgs e)
{
if (!m_clientSocket.Connected)
{
MessageBox.Show("Connection Timeout");
//m_clientSocket = null; timer_connection.Stop();
}
} connect with timeout for socket.
原理是利用socket默认的超时时间要在16-25秒左右,你可以将等待超时的时间设置在这个值以下,比如1秒或者3秒等。
示例: TcpClient tcpClient = new TcpClient();
SetMSG("开始连接"); // connflag 连接是否成功的标志
bool connflag = false;
if (!tcpClient.Connected)
{
try
{
IAsyncResult result = tcpClient.BeginConnect(ipAddress, _port), null, null); connflag = result.AsyncWaitHandle.WaitOne(1000, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
}
catch (Exception ex)
{
SetMSG("连接时出错!错误详细信息:" + ex.Message);
connflag = false;
}
}
if (!connflag)
{
//如果超时无返回
if (!tcpClient.Connected)
{
//肯定是连接失败
}
else
{
//连接成功
}
}
private void connect(string serverIp, string serverPort)
{
try
{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(serverIp);
int iPortNo = System.Convert.ToInt16(serverPort);
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo); //m_clientSocket.
e.RemoteEndPoint = ipEnd;
e.UserToken = m_clientSocket;
m_clientSocket.ConnectAsync(e);
if (timer_connection != null)
{
timer_connection.Dispose();
}
else
{
timer_connection = new System.Windows.Forms.Timer();
}
timer_connection.Interval = 1000;
timer_connection.Tick += new EventHandler(timer_connection_Tick);
timer_connection.Start();
}
catch (SocketException) //se)
{
fwqyjgb();
//MessageBox.Show(se.Message);
}
}
private void timer_connection_Tick(object sender, EventArgs e)
{
if (!m_clientSocket.Connected)
{
fwqyjgb();
//m_clientSocket = null;
timer_connection.Stop();
}
else
{
fwqzckf();
timer_connection.Stop();
}
}
SetMSG("开始连接"); // connflag 连接是否成功的标志
bool connflag = false;
if (!tcpClient.Connected)
{
try
{
IAsyncResult result = tcpClient.BeginConnect(ipAddress, _port), null, null); connflag = result.AsyncWaitHandle.WaitOne(1000, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
}
catch (Exception ex)
{
SetMSG("连接时出错!错误详细信息:" + ex.Message);
connflag = false;
}
}
if (!tcpClient.Connected)
{
//肯定是连接失败
}
else
{
//连接成功
}
//SetMSG("开始连接"); // connflag 连接是否成功的标志
bool connflag = false;
if (!tcpClient.Connected)
{
try
{
IAsyncResult result = tcpClient.BeginConnect(IPAddress.Parse(server), port, null, null); connflag = result.AsyncWaitHandle.WaitOne(500, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
}
catch (Exception ex)
{
MessageBox.Show("连接时出错!错误详细信息:" + ex.Message);
connflag = false;
}
}
if (!tcpClient.Connected)
{
//肯定是连接失败
fwqyjgb();
}
else
{
//连接成功
fwqzckf();
}