-2

Possible Duplicate:
tcp/ip client server not working over internet

i spent the last week working on a simple windows form application that' s supposed to send a couple of integer numbers from client to server or from server to client. So far i have only managed to make it work for lanns, any idea about how to make it work on the open internet ? Here' s the code in case you need it (also call me noob but i can' t get how this site handles code, ... does not do what i thought it did so feel free to edit my post in case i fail2format)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace client_server_penta
{
    public class Client
    {
        #region Fields 
        private int turno = 1;
        private TcpClient[] clients = new TcpClient[1]; //used to remember the active client
        private int CoordinateX, CoordinateY; //coordinates, they are used by the application
        #endregion     

        public Client(string address)
        {            

                TcpClient client = new TcpClient();
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(address), 3000);
                client.Connect(serverEndPoint);
                clients[0] = client;              
                Thread ReadFromServer = new Thread(new ParameterizedThreadStart(this.ReadHandler));
                ReadFromServer.Start(client);
        } 

        public void SendData(int a, int b)
        {
            NetworkStream clientStream = clients[0].GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(a.ToString() + '$' + b.ToString() + '$');
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }

        public int ReadCoordinateX()
        {
            return this.CoordinateX;
        }

        public int ReadCoordinateY()
        {
            return this.CoordinateY;
        }

        private void ReadHandler(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] buffer;
            int bytesRead;

            while (true)
            {
                buffer = new byte[10];
                bytesRead = 0;
                try
                {                    
                    bytesRead = clientStream.Read(buffer, 0, buffer.Length);
                }
                catch
                {
                    //an uknown error has occurred
                    break;
                }
                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
                //data received
                ASCIIEncoding encoder = new ASCIIEncoding();
                OnDataReceived(encoder.GetString(buffer, 0, buffer.Length));
            }
            tcpClient.Close();            
        }

        private void OnDataReceived(string text)
        {
            string first_number = "";
            string second_number = "";
            int index = 0;
            while (text[index] != '$')
                first_number += text[index++];
            index++;
            while (text[index] != '$')
                second_number += text[index++];
            this.CoordinateX = Convert.ToInt32(first_number);
            this.CoordinateY = Convert.ToInt32(second_number);
            var myForm = Application.OpenForms["Form2"] as Form2;
            myForm.Text = "Turno N." + Convert.ToString(this.turno++);
        }
    }
}

//and here' s the server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace client_server_penta
{
    public class Server
    {        
        private Thread listenThread;
        private int turno = 1;
        private TcpListener tcpListener;
        private TcpClient[] clients = new TcpClient[1]; //used to remember the active client
        private int CoordinateX, CoordinateY; //coordinates, they are used by the application

        public Server(int port)
        {
            this.tcpListener = new TcpListener(IPAddress.Any, 3000);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();

        }


        public void SendData(int a, int b)
        {
            NetworkStream clientStream = clients[0].GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(a.ToString()+'$'+b.ToString()+'$');
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }

        public int ReadCoordinateX()
        {
            return this.CoordinateX;
        }


        public int ReadCoordinateY()
        {
            return this.CoordinateY;
        }

        private void ListenForClients()
        {

                this.tcpListener.Start();
                TcpClient client = this.tcpListener.AcceptTcpClient();                              
                clients[0] = client;
                Thread ReadFromClient = new Thread(new ParameterizedThreadStart(this.ReadHandler));
                ReadFromClient.Start(client);


        }

        private void ReadHandler(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] buffer = new byte[10];
            int bytesRead;

            while (true)
            {
                buffer = new byte[10];
                bytesRead = 0;
                try
                {
                    bytesRead = clientStream.Read(buffer, 0, buffer.Length);
                }
                catch
                {
                    break;
                }
                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
                //data received
                ASCIIEncoding encoder = new ASCIIEncoding();
                OnDataReceived(encoder.GetString(buffer, 0, buffer.Length));                
            }  
            tcpClient.Close();            
        }

        private void OnDataReceived(string text)
        {
            string first_number = "";
            string second_number = "";
            int index = 0;
            while (text[index] != '$')            
                first_number += text[index++];
            index++;
            while (text[index] != '$')
                second_number += text[index++];
            this.CoordinateX = Convert.ToInt32(first_number);
            this.CoordinateY = Convert.ToInt32(second_number);
            var myForm = Application.OpenForms["Form2"] as Form2;
            myForm.Text = "Turno N."+Convert.ToString(this.turno++);
        }     
    }
}
Community
  • 1
  • 1
user1909612
  • 273
  • 5
  • 14
  • I suggest you research on WCF... these kind of coding TCP stuff by yourself is not going to take you further... Also, in order to access a Server from the internet you need to open up the port in whatever Router / Firewall and NAT it as well. I suggest you research on that as well. – Federico Berasategui Jan 25 '13 at 19:47
  • You could make it work as is if you set up a VPN, then had the VPN address pool share the same pool with the LAN. Lot more secure. – B L Jan 25 '13 at 19:48
  • if i try to connect to myself or to another computer on my local network it works without problems, if a try to connect to the public ip of a friend running the program in server mode the request times out and the connection fails. – user1909612 Jan 25 '13 at 19:48
  • its possible that the server is inaccessible (like if its behind a nat) – Sam Axe Jan 25 '13 at 19:50

2 Answers2

0

Have you opened the 3000 port on your firewall on the two sides ?

0

Yes of course ^^ If you have routers, don't forget to edit the configurations too.