使用Beetlex实现TCP客户端

首次使用Beetlex ,做个记录 

using BeetleX;
using BeetleX.Clients;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InfusionBagSmartLabeler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Hello, world!");
            var newWindowThread = new Thread(connect);
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();
            //connect();
            //MessageBox.Show("connect OK");
        }

        private void connect()
        {
            string IP = ConfigurationManager.AppSettings["ServerIP"];
            int Port = Convert.ToInt32(ConfigurationManager.AppSettings["ServerPort"]);

            TcpClient client = SocketFactory.CreateClient<TcpClient>(IP, Port);

            client.Connect();
            var stream = client.Stream.ToPipeStream();
            while (true)
            {
                Debug.WriteLine("Enter Name:");
                // Console.Write("Enter Name:");
                var line = "easyboot" + DateTime.Now.ToString();// $"{DateTime.Now}";// Console.ReadLine();
                client.Stream.ToPipeStream().WriteLine(line);
                client.Stream.Flush();

                Debug.WriteLine("ReceivBytes " + client.ReceivBytes);

                try
                {
                    var read = client.Receive();
                    // read.
                    var line3 = read.ReadToEnd();
                    Debug.WriteLine(line3 + "----" + DateTime.Now.ToString());
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
   


            }

        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值