سؤال

I'm building a C# chat program, yet I'm facing a problem with outside connection. When the same computer connects both as server and as client, there seems to be no problem, yet when I try to host the connection on one computer, the other can't connect as a client. here's the relevant code: class Server: public void Connect(string ipAddr, string port) { server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port));

        server.Bind(ipLocal);//bind to the local IP Address...
        server.Listen(5);//start listening...

        // create the call back for any client connections...
        server.BeginAccept(new AsyncCallback(OnClientConnect), null);
    }
    public void Disconnect()
    {
        server.Close();
        server = null;
        tempSocket = null;
    }

    public void OnClientConnect(IAsyncResult asyn)
    {
        try
        {
            if (server != null)
            {
                tempSocket = server.EndAccept(asyn);

                WaitForData(tempSocket);

                server.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
        }
        catch (ObjectDisposedException)
        {
            Debugger.Log(0, "1", "OnClientConnect: Socket has been closed.");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "OnClientConnect Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

Client class: public void Connect(string ipAddr, string port) { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ipAddr), Convert.ToInt32(port)); client.Connect(ipe);

        clientListener = new Thread(OnDataReceived);
        isEndClientListener = false;
        clientListener.Start();
    }

I have no idea what's wrong here. Hope you can tell me what's wrong.

هل كانت مفيدة؟

المحلول

Your issue is probably not code related. In order for other people outside your network to conenect to you, you need to port forward the port that you are connecting through on your router. You can find many tutorials here. You may also check to see if your connection is open through this tool.

From Wikipedia:

Port forwarding allows remote computers (for example, computers on the Internet) to connect to a specific computer or service within a private local-area network (LAN).

You must allow connections through your router to be able to connect to your chat server.

نصائح أخرى

You need to give your computer a public IP address (maybe your router has this option) or implement port forwarding on your router.

A Public IP address would be the one of your router. Check out this site to find out your public IP whatismyipaddress.com. Your router can or cannot support the option to give its public IP address to your computer, however, your router should be able to do port forwarding. (Forwarding the data from a specific port to a specific computer, so when someone connects to your public IP. For example 93.93.93.93:3333 will be forwarded to your PC.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top