如何配置插座连接超时 [英] How to configure socket connect timeout

查看:113
本文介绍了如何配置插座连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户端试图连接到断开连接的IP地址,还有很长的超时时间在15秒内......我们怎样才能减少这种超时?什么是配置方法?

在code我使用建立一个套接字连接如下:

 尝试
{
    m_clientSocket =新的Socket(
         AddressFamily.InterNetwork,
         SocketType.Stream,
         ProtocolType.Tcp);    ip地址IP = IPAddress.Parse(SERVERIP);
    INT iPortNo = System.Convert.ToInt16(serverPort);
    IPEndPoint IPEND =新IPEndPoint(IP,iPortNo);    m_clientSocket.Connect(IPEND);
    如果(m_clientSocket.Connected)
    {
        lb_connectStatus.Text =连接已​​建立;
        WaitForServerData();
    }
}
赶上(SocketException SE)
{
    lb_connectStatus.Text =连接失败;
    MessageBox.Show(se.Message);
}


解决方案

我用Socket.ConnectAsync方法而不是Socket.Connect方法解决了这个问题。
调用Socket.ConnectAsync(的SocketAsyncEventArgs)后,启动一个定时器(timer_connection),如果时间到了,检查套接字连接是否已连接(如果(m_clientSocket.Connected)),如果不,弹出超时错误。

 私人无效连接(字符串ipAdd,串口)
    {
        尝试
        {
            的SocketAsyncEventArgs E =新的SocketAsyncEventArgs();
            m_clientSocket =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            ip地址IP = IPAddress.Parse(SERVERIP);
            INT iPortNo = System.Convert.ToInt16(serverPort);
            IPEndPoint IPEND =新IPEndPoint(IP,iPortNo);            // m_clientSocket。
            e.RemoteEndPoint = IPEND;
            e.UserToken = m_clientSocket;
            e.Completed + =新的EventHandler<&的SocketAsyncEventArgs GT;(e_Completed);
            m_clientSocket.ConnectAsync(E);            如果(timer_connection!= NULL)
            {
                timer_connection.Dispose();
            }
            其他
            {
                timer_connection =新的Timer();
            }
            timer_connection.Interval = 2000;
            timer_connection.Tick + =新的EventHandler(timer_connection_Tick);
            timer_connection.Start();
        }
        赶上(SocketException SE)
        {
            lb_connectStatus.Text =连接失败;
            MessageBox.Show(se.Message);
        }
    }
私人无效e_Completed(对象发件人,让SocketAsyncEventArgs E)
    {
        lb_connectStatus.Text =连接已​​建立;
        WaitForServerData();
    }
    私人无效timer_connection_Tick(对象发件人,EventArgs的发送)
    {
        如果(!m_clientSocket.Connected)
        {
            的MessageBox.show(连接超时);
            // m_clientSocket = NULL;            timer_connection.Stop();
        }
    }

When the Client tries to connect to a disconnected IP address, there is a long timeout over 15 seconds... How can we reduce this timeout? What is the method to configure it?

The code I'm using to set up a socket connection is as following:

try
{
    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.Connect(ipEnd);
    if (m_clientSocket.Connected)
    {
        lb_connectStatus.Text = "Connection Established";
        WaitForServerData();
    }
}
catch (SocketException se)
{
    lb_connectStatus.Text = "Connection Failed";
    MessageBox.Show(se.Message);
}

解决方案

I solved the problem by using Socket.ConnectAsync Method instead of Socket.Connect Method. After invoking the Socket.ConnectAsync(SocketAsyncEventArgs), start a timer (timer_connection), if time is up, check whether socket connection is connected (if(m_clientSocket.Connected)), if not, pop up timeout error.

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();
        }
    }

这篇关于如何配置插座连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆