【问题标题】:TCP IP Client in WPF for Ubuntu 14 Server用于 Ubuntu 14 服务器的 WPF 中的 TCP IP 客户端
【发布时间】:2014-08-15 10:22:09
【问题描述】:

我正在尝试在 WPF GUI/C#.NET 中为 Ubunu 服务器创建 TCP IP 客户端。

问题:我能够连接到服务器机器,连接工作正常,正确发送消息并且 ubuntu 控制台也显示,客户端已连接并发送此命令,例如在服务器上启动视频源,但是当涉及到读取响应没有任何反应 - 它不读取应该由 Ubuntu 服务器返回的字节数组。实际上,在消息 - 102 上,它应该在服务器上启动视频馈送并返回应该进一步读取并显示视频的视频馈送字节数组。还没有编写代码来显示视频提要,因为我无法从服务器读取提要,但是,客户端正确地将命令(消息)发送到服务器,如前所述,可以在 Ubuntu 服务器计算机的控制台上查看它。求推荐谢谢!!

下面是代码,请查看并建议我做错了什么:

namespace POC_TCP_Listener
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private int WhichEventFired = 0;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                // string message = "{Site: 1}";
                WhichEventFired = 1; //Start Video Feed
                Thread ClientThread = new Thread(new ThreadStart(ConnectToServerAndRetrieveBytes));
                ClientThread.Start();

            }

            catch (Exception ex)
            {
                string st = ex.Message;

            }
        }

        private void ConnectToServerAndRetrieveBytes()
        {

            TcpClient TCP = new TcpClient();
            TCP.Connect("IPAddress", 5001);

            byte[] packet;

            var size = 9;
            var header = 102;
            var siteId = 1;
            var state = 1;

            if (WhichEventFired == 1)
            {
                header = 102;   // Start Video Feed
            }
            else if (WhichEventFired == 2)
            {
                header = 114; // Stop Video Feed
            }
            else
            {
                header = 115; // query Temperature
            }

            // <8> <115> <1>
            packet = BitConverter.GetBytes(size).Reverse().Concat(BitConverter.GetBytes(header).Reverse()).Concat(BitConverter.GetBytes(siteId).Reverse()).Concat(BitConverter.GetBytes(state).Reverse()).ToArray();


            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = packet;

            // Get a client stream for reading and writing. 

            NetworkStream stream = TCP.GetStream();

            // Send the message to the connected TcpServer. 

            stream.Write(data, 0, data.Length);

            byte[] buffer = new byte[64 * 1024];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
// In the below line - it stops and nothing happens after it - Please Suggest
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                // return ms.ToArray();
            }

            stream.Close();
            TCP.Close();
        }
    }
}

强文本 请提出为什么它停止工作或让我知道我是否做错了什么。

【问题讨论】:

  • WPF是一个基于.net的GUI框架,编程语言为C#。 WPF 与 .net 或语言的网络部分无关。
  • 是的,当然...你能告诉我哪里错了吗?

标签: c# .net network-programming video-streaming tcpclient


【解决方案1】:

我认为问题在于,如果没有更多的包,它会逃脱 while 循环。如果您想无休止地检查它,您应该尝试使用 for。但我不确定。

是的,应该是这样。如果在 while 循环开始的那一刻没有包,它会自行终止,因为没有什么可读取的。所以你可以检查 Ubuntu Server 是否返回了一些东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多