开发者

c# .net opening an external tcp port

I have made a TCP server which I have been testing locally and it works great, and now I have opened the port on my firewall but I still cant see the port externally. I think this is down to the configuration of my socket, how do I correct this?

        System.Net.IPHostEntry localhost = System.N开发者_运维技巧et.Dns.GetHostEntry(System.Net.Dns.GetHostName());

        #region Bind Socket & Listen for connections, accepting Asynchronously

        System.Net.IPEndPoint serverEndPoint;

        try
        {
            serverEndPoint = new System.Net.IPEndPoint(localhost.AddressList[0], _port);
        }
        catch (System.ArgumentOutOfRangeException e)
        {
            throw new ArgumentOutOfRangeException("Port number entered would seem to be invalid, should be between 1024 and 65000", e);
        }

        try
        {
            //_serverSocket = new System.Net.Sockets.Socket(serverEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            //_serverSocket = new System.Net.Sockets.Socket(serverEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            //_serverSocket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _serverSocket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        catch (System.Net.Sockets.SocketException e)
        {
            throw new ApplicationException("Could not create socket, check to make sure not duplicating port", e);
        }

        try
        {
            _serverSocket.Bind(new IPEndPoint(IPAddress.Any/*.Parse("127.0.0.1")*/, _port));//serverEndPoint);
            //_serverSocket.Bind(new System.Net.IPEndPoint(System.Net.Dns.GetHostEntry("localhost").AddressList[0],12345));
            _serverSocket.Listen(_backlog);
        }
        catch (Exception e)
        {
            throw new ApplicationException("Error occured while binding socket, check inner exception", e);
        }

        try
        {
            //warning, only call this once, this is a bug in .net 2.0 that breaks if 
            // you're running multiple asynch accepts, this bug may be fixed, but 
            // it was a major pain in the ass previously, so make sure there is only one 
            //BeginAccept running 
            _serverSocket.BeginAccept(new AsyncCallback(acceptCallback), _serverSocket);
        }
        catch (Exception e)
        {
            throw new ApplicationException("Error occured starting listeners, check inner exception", e);
        }

        #endregion


The socket configuration is correct - I had not configured port forwarding using the correct subnet ip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜