Can't connect to my IP using Socket

牧云@^-^@ 提交于 2019-12-29 09:06:30

问题


the server throws this error "Attempting to deserialize an empty stream" at server side in this line when I run the server: this.tcpListener.Start();

This is my internet IP, if I use my local IP, it works. But i want the internet IP.

Client side:

TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("187.115.131.44", 8001);

Server side:

    public Server()
    {
        try
        {
            IPAddress ip = IPAddress.Parse("187.115.131.44");
            tcpListener = new TcpListener(ip, 8001);
            listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
            Console.ReadKey();
        } 

    }

    private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

回答1:


When you start your application and are listening on a port, run a port forward check here.

I've had this problem when writing network software - it's a common problem.

Once you've verified that its a port forwarding problem, head over to PortForward.com. There are great resources there to help you configure your router.




回答2:


The firewall and/or router/switch you are obviously behind is the cause. It would require a change or an addition of a NAT policy on said appliance.




回答3:


I've same problem and I'm also behind router and firewall. I found that with your code localhost (127.0.0.1) works perfect but as soon as we try to connect to remote host it gives an error. Solution is change your server side code as follows:

tcpListener = new TcpListener(IPAddress.Any, 8001);

Hope this will help you...



来源:https://stackoverflow.com/questions/3154230/cant-connect-to-my-ip-using-socket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!