C#实时系统的套接字程序

时间:2014-08-21 11:46:53

标签: c# xsockets.net

我对客户&服务器应用。顺便说一下,我正在研究实时项目。我遇到了一系列问题,我无法弄明白。你能帮帮我这个sowftware吗?让我解释下面的项目;

我的项目由两部分组成。其中一个是Client other,部分是Server。路由器上有设备。我试图通过以太网连接此设备。我可以通过它的Ipnumber和端口号连接到设备,它们是172.16.204.100,50007,我可以发送我的数据(命令)。

  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();

    }
}

}

然后,我写了我的计算机的Ipnumber,用于在Bind方法中设置Device和我的计算机之间的连接。我开始等待得到回应。在这一点上,我从来没有得到答案。我认为我的端口号应该是错误的,这可能会导致问题。我不知道究竟是什么问题。我只知道我无法得到任何回应。

顺便说一句,我刚刚研究这个平台将近15天。我不专业。我对这项工作真的很麻烦。

你能帮助我吗?如果有人可以帮助我,我将非常高兴他。

   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();
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

您应该将客户端尝试连接的端口号更改为4561 ..当您的服务器正在侦听4561时。因此,将Localendpoint的端口号从5007更改为4561.

我希望你理解localendpoint表示你的服务器而不是客户端。

此外,如果您尝试通过5007连接到中间设备,那么除非设备在4561将数据重定向到服务器,否则您的服务器显然不会收到任何内容。