只有每个套接字地址(协议/网络地址/端口)的一个用法通常被允许 [英] Only one usage of each socket address (protocol/network address/port) is normally permitted

查看:136
本文介绍了只有每个套接字地址(协议/网络地址/端口)的一个用法通常被允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到Asp.Net TCP / IP端口,基本上我有一个连接设备在该端口上到我读,它工作正常,但在第二次当TCP监听尝试启动,然后它会产生上述错误。
任何机构可以引导我如何能摆脱这里这个错误是我的code,我使用连接到TCP / IP端口:

I am connecting to TCP/IP port in Asp.Net, basically i have attached a device on this port to which i am reading, it is working fine but on second time when tcp listener tries to start then it generates the above error. can any body guide me how can i get rid of this error here is my code that i am using to connect to TCP/IP port:

       try
        {                
            byte[] ipaddress = new byte[4];
            string ip = ConfigurationManager.AppSettings["IP"].ToString();
            string[] ips = ip.Split('.');
            ipaddress[0] = (byte)Convert.ToInt32(ips[0]);
            ipaddress[1] = (byte)Convert.ToInt32(ips[1]);
            ipaddress[2] = (byte)Convert.ToInt32(ips[2]);
            ipaddress[3] = (byte)Convert.ToInt32(ips[3]);
            int portNumber = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
            tcpListener = new TcpListener(new IPAddress(ipaddress), portNumber);
            tcpListener.Start();
            tcpClient = new TcpClient();
            tcpClient.NoDelay = true;
            try
            {
                System.Threading.Thread.Sleep(60000);
                tcpClient.ReceiveTimeout = 10;
                tcpClient = tcpListener.AcceptTcpClient();
            }
            catch (Exception ex)
            {
                tcpClient.Close();
                tcpListener.Stop();
            }
            NetworkStream networkStream = tcpClient.GetStream();
            byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
            try
            {
                networkStream.ReadTimeout = 2000;
                networkStream.Read(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                tcpClient.Close();
                tcpListener.Stop();
            }
            string returndata = Encoding.Default.GetString(bytes);
            tcpClient.Close();
            tcpListener.Stop();

            return returndata.Substring(returndata.IndexOf("0000000036"), 170);
        }
        catch (Exception ex)
        {
            if (tcpClient != null)
                tcpClient.Close();
            tcpListener.Stop();
            LogError("Global.cs", "ReceiveData", ex);
            ReceiveData();
        }

当谈到在此行tcpListener.Start();对于第二次则产生错误
只有一个使用每个套接字地址(协议/网络地址/端口)通常允许

when it comes on this line tcpListener.Start(); for second time then it generates that error "Only one usage of each socket address (protocol/network address/port) is normally permitted"

推荐答案

如果这是一个循环类的东西,注意,如果您code不会触发任何异常,则该连接不会关闭。如果有一个例外,这将被终止抓捕获它只是关闭

If this is in a loop or something, note that if your code doesn't fire any exceptions, then the connection is not closed. It is only closed if there is an exception, which would be caught by the ending catch block

这篇关于只有每个套接字地址(协议/网络地址/端口)的一个用法通常被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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