Diaconescu Cristian
Diaconescu Cristian

Reputation: 23

Error when compiling program

I made a client server but unfortunately I get this error

You can tell me how to fix it please?

The error occurs in class client.cs ... It's written right IP?

I opened the command prompt and wrote "ipconfig" and the number of IPv4 there is "192.168.1.104"

Client.cs

 try
        {
            TcpClient tcp = new TcpClient();
            Console.WriteLine("Conectare...");

            tcp.Connect("192.168.1.104", 8001);
            Console.WriteLine("Conectat");
            Console.WriteLine("Introduce-ti sirul de caractere");
            string str = Console.ReadLine();
            Stream strm = tcp.GetStream();

            ASCIIEncoding asci = new ASCIIEncoding();
            byte[] ba = asci.GetBytes(str);
            Console.WriteLine("Trimitere");
            strm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int b = strm.Read(bb, 0, 100);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Eroare..."+ex.StackTrace);

        }

Server.cs

 try
        {
            IPAddress IPadress = IPAddress.Parse("192.168.1.104");
            TcpListener listner = new TcpListener(IPadress, 8001);
            Console.WriteLine("Serverul ruleaza");
            Console.WriteLine("Punctul final este: " + listner.LocalEndpoint);
            listner.Start();
            Socket o = listner.AcceptSocket();
            Console.WriteLine("Conexiunea acceptata de la " + o.RemoteEndPoint);
            byte[] b = new byte[100];
            int k = o.Receive(b);
            Console.WriteLine("Receptionat");
            for (int i = 0; i < k; i++)
            {
                Console.WriteLine(Convert.ToChar(b[1]));
            }
            ASCIIEncoding asc = new ASCIIEncoding();
            o.Send(asc.GetBytes("Mesaj automat" + "String trimis de server"));
            o.Close();
            listner.Stop();
        }
        catch (Exception ex)
        {

            Console.WriteLine("Measj de eroare" + ex.StackTrace);
        }
        Console.ReadLine();

http://i62.tinypic.com/2zj9uop.jpg

Upvotes: 0

Views: 62

Answers (2)

dimitris93
dimitris93

Reputation: 4273

Go to www.google.com and type "what is my ip", the first result you will see will be your real ip. Now to be able to connect to that IP from an external application you must allow your 'own IP' to the whitelist from your router website page. A simillar page to this http://www.theninjaproxy.org/wp-content/uploads/2013/07/netgearWNDR4500.jpg. If you wanted to open that application from your friends computer who has another IP lets say '11.11.11.11' you need to add that too or else he wont be able to open your c# application.

Edit: its kind of the same situation where you are trying to connect to a mysql database from a c# application and you have to go to your cpanel and allow the IP of the computer that is using that c# application. but for you , you simply go to your router webpage.

Upvotes: 2

NMK
NMK

Reputation: 1020

Server need to listen in loop to accept client connections. Example code

Upvotes: 1

Related Questions