• C#客户端和服务端数据的同步传输 (转载)


     

    客户端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;

    namespace TCP同步客户端2014_12_15
    {
        public partial class Form1 : Form
        {
            private TcpClient myTcpClient;// 我的客户端
            private NetworkStream networdStream;// 网络数据流
            private delegate void ShowRecivieMsgCallBack(string text);
            private ShowRecivieMsgCallBack showRecivieMsgCallBack;
            private delegate void SetProgressBarCallBack();
            private SetProgressBarCallBack setProgressBarCallBack;
            private Thread ReceiveMsgThread;//接收消息线程
            public Form1()
            {
                InitializeComponent();
            }
            // 在窗体加载的时候启动
            private void Form1_Load(object sender, EventArgs e)
            {
                //初始化回调
                showRecivieMsgCallBack = new ShowRecivieMsgCallBack(ReceiveMsgtoList);
                setProgressBarCallBack = new SetProgressBarCallBack(setProgressBar);
            }
            //建立连接
            private void btnConnect_Click(object sender, EventArgs e)
            {
                //创建实例化IP终结点
                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(txtServerIp.Text),Convert.ToInt32(txtServerPort.Text));
                //创建实例化TCP客户端
                myTcpClient = new TcpClient();
                try
                { 
                    //建立TCP连接
                    myTcpClient.Connect(ipEndPoint);
                    //实例化网络数据流
                    networdStream = myTcpClient.GetStream();
                    //接收消息
                    ReceiveMsgThread = new Thread(ReceiveMsg);
                    ReceiveMsgThread.Start();

                }
                    catch(ThreadAbortException)
                {
                    
                    }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            //发送消息
            private void btnSendMsg_Click(object sender, EventArgs e)
            {
                byte[] sendMsg;
                sendMsg = Encoding.Default.GetBytes(tbSendMsg.Text);
                    networdStream.Write(sendMsg, 0,sendMsg.Length);

            }
            //断开连接
            private void btnDisConnect_Click(object sender, EventArgs e)
            {
                //关闭相关的实例化对象
                myTcpClient.Close();
                networdStream.Dispose();
                // 终端线程
                ReceiveMsgThread.Abort();
            }

            //接收消息线程
            private void ReceiveMsg()
            {
                while (true)
                {
                    try
                    { 
                      //获得数据
                        byte[] getDate = new byte[1024];
                        networdStream.Read(getDate,0,getDate.Length);
                        // 转化格式
                        string msg = Encoding.Default.GetString(getDate);
                        //将受到的消息放在列表中
                        lstRecevieMsg.Invoke(showRecivieMsgCallBack,msg);
                        pgProgressBar.Invoke(setProgressBarCallBack);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        if (networdStream != null)
                        {
                            networdStream.Dispose();
                        }
                        break;
                    }
                }
            
            }
            //接收消息的方法
            private void ReceiveMsgtoList(string text)
            {
                lstRecevieMsg.Items.Add(text);
            }
            private void setProgressBar()
            {
                if (pgProgressBar.Value < pgProgressBar.Maximum)
                {
                    pgProgressBar.Value++;
                }
                else
                {
                    pgProgressBar.Value = 0;
                }
            }
        }
    }

    服务端:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.IO;
    namespace TCP异步编程2014_12_15
    {
        public partial class Form1 : Form
        {
            private TcpListener myTcpListener;
            private Thread AccessMsgThread;
            private TcpClient myTcpClient;
            private NetworkStream ns;
            private string path;
            private delegate void ShowMgsCallBack(string text);
            private ShowMgsCallBack showMgsCallBack;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                showMgsCallBack = new ShowMgsCallBack(showMsg);
            }
            private void showMsg(string text)
            {
                lstRecevieMsg.Items.Add(text);
            }
            //开始监听的单击事件
            private void btnEndListen_Click(object sender, EventArgs e)
            {
                try
                {
                    IPEndPoint ipendPoint = new IPEndPoint(IPAddress.Parse(txtServerIp.Text), Convert.ToInt32(txtServerPort.Text));
                    myTcpListener = new TcpListener(ipendPoint);
                    myTcpListener.Start();
                    AccessMsgThread = new Thread(AccessMsg);
                    AccessMsgThread.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            //结束监听的单击事件
            private void btnBeginListen_Click(object sender, EventArgs e)
            {
                myTcpListener.Stop();
                AccessMsgThread.Abort();
            }

            private void AccessMsg()
            {
                try
                {
                    myTcpClient = myTcpListener.AcceptTcpClient();
                    ns = myTcpClient.GetStream();
                    while (true)
                    {
                        int readLen = myTcpClient.Available;
                        if (readLen > 0)
                        { 
                            //string path;
                           // System.IO.Directory.CreateDirectory(path);
                            //DirectoryInfo dir = new DirectoryInfo(path);
                          //  dir.Create();//自行判断一下是否存在。
                           
                           byte[] getDate=new byte[1024];
                            ns.Read(getDate,0,getDate.Length);
                            string getMsg=Encoding.Default.GetString(getDate);
                            lstRecevieMsg.Invoke(showMgsCallBack,getMsg);
                            Thread.Sleep(50);
                            ns.Write(getDate, 0, getDate.Length);
                        }
                    }

                }
                    catch(ThreadAbortException)
                {
                    
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                    if (ns != null)
                    {
                        ns.Dispose();
                    }
                    if (myTcpClient != null)
                    {
                        myTcpClient.Close();
                    }
                }
            }
        }
    }

  • 相关阅读:
    Java线程池
    代理模式
    Bean的加载
    Spring的启动流程
    MFC编程入门之二十七(常用控件:图片控件PictureControl)
    MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)
    MFC编程入门之二十五(常用控件:组合框控件ComboBox)
    MFC编程入门之二十四(常用控件:列表框控件ListBox)
    MFC编程入门之二十三(常用控件:按钮控件的编程实例)
    MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
  • 原文地址:https://www.cnblogs.com/shuaiheng/p/10501758.html
Copyright © 2020-2023  润新知