views:

120

answers:

1

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

+1  A: 

Have you tried unlocking the port?

Chad
i have no idea how to do that? by unlocking MY port? or my friends port? i have tried disabling my firewall and my antivirus and that made no difference. i also tried about 50 different port numbers.
baeltazor
You will need to open the port on each others machine. If your over a LAN (Local Area Network) it will suffice by disabling the firewall just to double check.Over the interweb it becomes harder and depending on what router you have. If you are trying to connect over the Internet, then you will need to open the listening port your friend will try to connect over.This also goes with Windows Firewall.
Chad
you need to ensure that both the port (8001 going by your code) is unlocked on both your client machine, and as well as the server. You may also want to consider using an unused TCP port from this list http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
jlech
Let me try again. Lets say if i try to create a WinSocket TCP/IP under a used port it will fail to create, with an error message saying the port is in use. He will need to increament the port number and try again until he finds a free port. His client should try to connect to the default port then start increasing the port number just incase the server is on another port eg.. try 10 ports, then give up.
Chad
hmmm thank you very much chad i will give that ago
baeltazor
@chad: thanks for that. now i have it in a for loop until int d is set to 1. so it keeps trying until d is set to 1.
baeltazor