3

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);
        }
    }
jgauffin
  • 99,844
  • 45
  • 235
  • 372
alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
  • Are you behind a router? – Aren Jun 30 '10 at 23:51
  • How is your internet connection set up? Is that IP handled by your router? Are you port-forwarding 8001 from your router to your local PC? By local IP do you mean an IP on an internal network or localhost, 127.0.0.1? – Rup Jun 30 '10 at 23:52
  • yes..its a router..damn it, i dunno configure it, i've tried a lot. I use the 198.168.....not 127.0.0.1... – alansiqueira27 Jul 01 '10 at 00:04
  • 1
    "i dunno configure it..." Well *there's* your problem! – Charlie Salts Jul 01 '10 at 00:06
  • Asking the [same question](http://stackoverflow.com/questions/3121871/socket-c-local-ip-works-but-internet-ip-do-not-work) [multiple times](http://stackoverflow.com/questions/3111384/problem-with-socket-and-serialization-c) is a poor utilization of everyone's time. – Stephen Cleary Jul 01 '10 at 03:05

3 Answers3

4

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.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
2

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...

Never Quit
  • 2,072
  • 1
  • 21
  • 44
1

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.