无法通过c#中的socket.connect()连接到具有外部IP的服务器

时间:2015-05-06 18:23:38

标签: c# sockets

我在内部网络上连接到我的服务器。但我无法连接到外部网络上的服务器。外部网络上的服务器防火墙已关闭。端口转发服务器是。

服务器端代码:

    private byte[] _buffer = new byte[1024];
    private List<Socket> _clientSockets = new List<Socket>();
    private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    public int PortNo;


    private void SetupServer()
    {
        try
        {
            _serverSocket.Bind(new IPEndPoint(IPAddress.Any, PortNo));
            _serverSocket.Listen(1);
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
        }
        catch(Exception hata)
        {
            MessageBox.Show(hata.Message);
        }

    }

    private void AcceptCallBack(IAsyncResult AR)
    {
        Socket socket = _serverSocket.EndAccept(AR);
        _clientSockets.Add(socket);

        try
        {
            socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
        }
        catch(Exception hata)
        {
            MessageBox.Show(hata.Message);
        }
    }


    private void ReceiveCallBack(IAsyncResult AR)
    {
        Socket socket = (Socket)AR.AsyncState;
        int received = socket.EndReceive(AR);
        byte[] dataBuf = new byte[received];
        Array.Copy(_buffer, dataBuf, received);

        string text = Encoding.ASCII.GetString(dataBuf);

        byte[] response = response = ekranGonderme();


        try
        {
            socket.BeginSend(response, 0, response.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
            socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
        }
        catch(Exception hata)
        {
            MessageBox.Show(hata.Message);

        }
    }

客户端代码:

private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    int portNo = 20000;   
    IPAddress[] server_ip =  Dns.GetHostAddresses(server's external ip address);

    private void Form1_Load(object sender, EventArgs e)
    {
        LoopConnect();
        LoopSendReceive()  
    }

    private void LoopSendReceive()
    {
        try
        {
            byte[] buffer = Encoding.ASCII.GetBytes("ekran_iste");
            _clientSocket.Send(buffer);

            byte[] receiveBuf = new byte[999999];
            int rec = _clientSocket.Receive(receiveBuf);
            byte[] data = new byte[rec];
            Array.Copy(receiveBuf, data, rec);

            MemoryStream ms = new MemoryStream(data);

        }
        catch
        {                 
        }  
    }

    private void LoopConnect()
    {
        int attempts = 0;

        while (!_clientSocket.Connected)
        {
            try
            {
                attempts++;
                _clientSocket.Connect(server_ip[0], portNo);                 
            }
            catch(SocketException)
            {        
            }
        }
    }
}

0 个答案:

没有答案