0

i am interested in Client & Server application. By the way, i am working on real time project. I have come accross a series problem for me and i cannot figure out. Would you help me about this sowftware. Let me explain my project below;

my project consists of two parts. One of them is Client other, part is Server. There is device on router. I am trying to connect this device via ethernet. I can connect to the device over its Ipnumber and port number which are 172.16.204.100, 50007 and i can send my data ( command ).

  CLIENT PART


  namespace client
   {
     class Program
   {
    static Socket sck;
    //static byte[] Buffer { get; set; }

    static void Main(string[] args)
    {
        Console.Title = "rrrrrr";
        sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint localendpoint = new IPEndPoint(IPAddress.Parse("172.16.204.100"), 50007); //50007  bidirectional interface The event 
        //channel is unidirectional and connects across port 50008.The event channel reports asynchronous events such as errors, tag arrivals, and digital output triggers. 

        try
        {
            sck.Connect(localendpoint);

            if (sck.Poll(-1, SelectMode.SelectWrite))
            {
                Console.WriteLine("this socket is writable");
            }
            else if (sck.Poll(-1, SelectMode.SelectRead))
            {
                Console.WriteLine("this socket is readable");
            }
            else if (sck.Poll(-1, SelectMode.SelectError))
            {
                Console.WriteLine("this socket has an error");
            }
        }
        catch
        {
            Console.Write(" unable ");
            Main(args);
        }

        Console.Write(" \r\n ");
        // Using the RemoteEndPoint property.
        Console.WriteLine("I am connected to " + IPAddress.Parse(((IPEndPoint)sck.RemoteEndPoint).Address.ToString()) + " on port number " + ((IPEndPoint)sck.RemoteEndPoint).Port.ToString());
        Console.Write(" \r\n ");

        // Using the LocalEndPoint property.
        Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)sck.LocalEndPoint).Address.ToString()) + " I am connected on port number " + ((IPEndPoint)sck.LocalEndPoint).Port.ToString());
        Console.Write(" \r\n ");

        Console.Write("enter text:  ");
        string text = Console.ReadLine();
        byte[] data = Encoding.ASCII.GetBytes(text);

        sck.Send(data);
        Console.Write(" data sent! \r\n   ");
        //Console.Write(" press ant key to continue  ");

        Console.Read();
        sck.Close();

    }
}

}

then, I wrote my computer's Ipnumber for setting up connection between Device and my computer in Bind method. i am start to wait getting response. At this point, I have never got an answer. I think that My port number should be wrong and this can cause a problem. I don't know what is the problem actually. I just know that I cannot get any responce.

By the way, I have just been studying this platform for almost 15 days. I am not professional. I am really in trouble about this work.

Would you help me? If someone can help me, i will be really pleasure to him.

   SERVER PART

   namespace server
   {


    class Program
    {

    static byte[] Buffer { get; set; }
    static Socket sck;

    static void Main(string[] args)
    {
        Console.Title = " Server ";
        sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        sck.Bind(new IPEndPoint(IPAddress.Parse("172.16.204.104"), 4561));

        //sck.Bind(new IPEndPoint(IPAddress.Parse("172.16.204.104"), 0));

        while (true)
        {
            sck.Listen(100);
            Socket accepted = sck.Accept();
            Buffer = new byte[accepted.SendBufferSize];
            int bytesRead = accepted.Receive(Buffer);
            byte[] formatted = new byte[bytesRead];

            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = Buffer[i];
            }
            string strData = Encoding.ASCII.GetString(formatted);
            Console.Write(strData + "\r\n");
            Console.Read();
            sck.Close();
            accepted.Close();
        }

    }
}

}

  • Why is your server listening on port `4561`, and client trying to connect to port `50007`? – vgru Aug 21 '14 at 12:02
  • Hi, 50007 is My device's sencron port. and 4561 is just choosed any port for listenning in my computer. – Gökhan Aug 21 '14 at 12:05
  • actually i am confused at this point. if you look at next line you will see i chosed 0 for port number. i am not sure what should I do and think for port – Gökhan Aug 21 '14 at 12:07
  • So, you have a "device", a C# client app and a C# server app? Three endpoints? The line where port number is `0` is commented out, as I see it. – vgru Aug 21 '14 at 12:09
  • by the way, i see your answer little bit late, so i can response late – Gökhan Aug 21 '14 at 12:15
  • Shortly, I am just set up communication over ethernet to connect a device and i want to send it data and i want to get response – Gökhan Aug 21 '14 at 12:17
  • Hi Mr Shubhada Fuge. I understood you, – Gökhan Aug 21 '14 at 12:22
  • However, when i chance the port number of Localendpoint from 50007 to any number, I cannot connect my device. When i do reverse, i cannot get response again. i meant that when i chance port number of server – Gökhan Aug 21 '14 at 12:24
  • I am confused why you have tagged the question with XSockets.NET :) When you do not ask anything about XSockets.NET. However the thing you try to do is exactly what XSockets.NET does for you without any need for you to write sockets programming. – Uffe Aug 21 '14 at 14:18

1 Answers1

0

You should change the port number your client is trying to connect to to 4561..As your server is listening on 4561. So change the port number of Localendpoint from 5007 to 4561.

I hope you have understood localendpoint denotes your server and not client.

Also, if you are trying to connect to the middle device through 5007, then your server is obviously not going to receive anything unless the device redirects the data to server at 4561.

Shubhada Fuge
  • 242
  • 2
  • 12