0

我对客户端和服务器应用程序感兴趣。顺便说一句,我正在做实时项目。我遇到了一系列问题,我无法弄清楚。你能帮我看看这个软件吗?让我在下面解释我的项目;

我的项目由两部分组成。其中一个是客户端,另一个是服务器。路由器上有设备。我正在尝试通过以太网连接此设备。我可以通过其 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();
        }

    }
}

}

4

1 回答 1

0

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

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

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

于 2014-08-21T12:09:02.327 回答