jay_t55
jay_t55

Reputation: 11652

Why won't my client/server code work outside of my local machine?

I am trying to create a very basic little client server application, but I can only get it to work locally on my own machine.

Server code:

        int d = 0;
        try
        {
            for (AdministratorPort = 8000; d < 1; AdministratorPort++)
            {
            IPAddress ipAddress = IPAddress.Parse("220.101.27.107");

            TcpListener tcpListener = new TcpListener(ipAddress, AdministratorPort);
            tcpListener.Start();

            // Display results.
            Label label = new Label();

            label.Text = "The server is connected to, and running on Port: 8001." + Environment.NewLine +
                tcpListener.LocalEndpoint + Environment.NewLine +
                "Team Share Server is now awaiting new connections.";

            label.AutoSize = true;
            label.Location = new Point(4, 15);
            panel.Controls.Add(label);

            panel.AutoScroll = true;

            Socket socket = tcpListener.AcceptSocket();
            label.Text += Environment.NewLine +
            "Connection accepted from: " + socket.RemoteEndPoint;

            byte[] Message = new byte[100];

            int k = socket.Receive(Message);
            label.Text += Environment.NewLine +
                "Message received from server.";

            for(int i = 0; i < k; i++)
                label.Text += Environment.NewLine +
                    Convert.ToChar(Message[i]);

            ASCIIEncoding asciiEncoding = new ASCIIEncoding();
            socket.Send(asciiEncoding.GetBytes("The string was received by the server."));

            label.Text += Environment.NewLine +
                "Acknowledgement sent to client.";

            socket.Close(10);
            tcpListener.Stop();
            d = 1;
            }
        }
        catch (Exception e) {
            d = 0;
            File.WriteAllText("this.txt", e.StackTrace);
        }

Client code:

        int d = 0;
        try
        {
            for (int port = 8000; d < 1; port++)
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("TRYING to connect...");

                tcpclnt.Connect("220.101.27.107", port); // use the ipaddress as in the server program

                Console.WriteLine("Connected, finally.");
                Console.Write("Enter the string to be transmitted : ");

                String str = Console.ReadLine();
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                Console.WriteLine("Transmitting.....");

                stm.Write(ba, 0, ba.Length);

                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);

                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(bb[i]));

                tcpclnt.Close();
                d = 1;
            }
        }

        catch (Exception e)
        {
            d = 0;
            Console.Write("oh no..... " + e.StackTrace + "      " + e.Data + "          " + e.Message.ToString());
            Console.Read();
        }

What am I doing wrong?

EDIT: the errors i am getting "connection timed out cos host didnt respond" or most of the time i get "host machine actively refused connection" and also the most recent error was : "connection attempt failed because the connected party did not respond properly after a period of time, or established connection failed because connected host has failed to respond 220.101.27.107".

Upvotes: 1

Views: 928

Answers (2)

erdave
erdave

Reputation: 11

You can erase the IPAddress object on the server and also :

Replace

TcpListener tcpListener = new TcpListener(ipAddress, AdministratorPort);

with

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

Upvotes: 1

Chad
Chad

Reputation: 3008

Have you tried unlocking the port?

Upvotes: 1

Related Questions