小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C# TCP 服務(wù)端(PC)與客戶端(PPC) 簡單代碼

 昵稱15965102 2014-02-25
 

C# TCP 服務(wù)端(PC)與客戶端(PPC) 簡單代碼

分類: C# 1507人閱讀 評論(3) 收藏 舉報

服務(wù)器端代碼

控件:btnStart_Click,btnSend_Click,label4,textBox1

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

namespace MobileServer
{
    public partial class FrmEasy : Form
    {
        //變量
        private ThreadStart start;
        private Thread listenThread,client_th;
        static private bool m_bListening = false;
        //static private System.Net.IPAddress MyIP = System.Net.IPAddress.Parse("192.168.1.3");

        //獲得當(dāng)前服務(wù)端的IP    地址
        static private System.Net.IPAddress MyIP = Dns.Resolve(Dns.GetHostName()).AddressList[0];

        static private TcpListener listener = new TcpListener(MyIP, 5567);
        private String msg;
        ArrayList clientArray = new ArrayList();

        public FrmEasy()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            start = new ThreadStart(startListen);
            listenThread = new Thread(start); 
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (m_bListening)
            {
                m_bListening = false;
                label4.Text = "Server Idle...";
            }
            else
            {
                m_bListening = true;
                listenThread.Start();
                label4.Text = "Server Listening...";
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            object a = clientArray[0];
            TcpClient client = (TcpClient)clientArray[0];
            if (client == null)
                return;
            NetworkStream sendStream = client.GetStream();
            String msg = textBox1.Text;
            Byte[] sendBytes = Encoding.Default.GetBytes(msg);
            sendStream.Write(sendBytes, 0, sendBytes.Length);
            sendStream.Flush();
            //sendStream.Close();
        }

        private void showmore()
        {
            textBox1.Text = msg;
        }
        private void startListen()
        {
            //textBox1.Text = "listening...";
            listener.Start();
            //接收數(shù)據(jù)
            while (m_bListening)
            {
                //測試是否有數(shù)據(jù)
                try
                {
                    TcpClient client = listener.AcceptTcpClient();
                    clientArray.Add(client);
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(AcceptMsg);                   
                    client_th = new Thread(threadStart);
                    client_th.Start(client); 
                }
                catch (Exception re)
                {
                    MessageBox.Show(re.Message);
                }
            }
            listener.Stop();
        }
        private void AcceptMsg(object arg)
        {
            TcpClient client = (TcpClient)arg;
            NetworkStream ns = client.GetStream();
            //StreamReader sr = new StreamReader(ns);//流讀寫器
            //字組處理
            while (true)
            {
                try
                {
                    byte[] bytes = new byte[1024];
                    int bytesread = ns.Read(bytes, 0, bytes.Length);
                    msg = Encoding.Default.GetString(bytes, 0, bytesread);
                    //顯示
                    //MessageBox.Show(msg);
                    showmore();

                    //將收到的數(shù)據(jù)回傳給客戶端,實際中,應(yīng)該經(jīng)過解析,將相應(yīng)的數(shù)據(jù)回傳給客戶端。
                    NetworkStream sendStream = client.GetStream();
                    String msga = textBox1.Text;
                    Byte[] sendBytes = Encoding.Default.GetBytes(msga);
                    sendStream.Write(sendBytes, 0, sendBytes.Length);
                    sendStream.Flush();
                    ns.Flush();
                    //ns.Close();
                }
                catch(Exception ex)
                {
                    //MessageBox.Show("與客戶端斷開連接了");
                    MessageBox.Show(ex.Message);
                    break;
                }
            }
        }
    }
}

 

客戶端代碼:

控件:button1_Click,btnSend_Click,textBox2

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

namespace TCP_PPCClient
{
    public partial class Form3 : Form
    {
        private TcpClient client;
        private Thread client_th;
        public Form3()
        {
            InitializeComponent();

            //此句在WinForm中可以,但在智能設(shè)備上不可用
            //Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (client == null)
                return;
            NetworkStream sendStream = client.GetStream();
            String msg = textBox1.Text;
            Byte[] sendBytes = Encoding.Default.GetBytes(msg);
            sendStream.Write(sendBytes, 0, sendBytes.Length);
            sendStream.Flush();
            //sendStream.Close();
            textBox1.Text = "Msg Sent!";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String mystr = textBox1.Text;
            string ip = "192.168.1.3";  //連接服務(wù)器端的IP地址
            this.connect_s(ip);
        }

        public void connect_s(string ip)
        {
            try
            {
                client = new TcpClient(ip, 5567);
                ThreadStart threadStart = new ThreadStart(AcceptMsg);
                client_th = new Thread(threadStart);
                client_th.Start();
                textBox2.Text="連接成功";
            }
            catch(System.Exception e)
            {
                textBox2.Text = e.ToString(); ;
            }
        }
        private void AcceptMsg()
        {
            NetworkStream ns = client.GetStream();
            //StreamReader sr = new StreamReader(ns);//流讀寫器
            //字組處理
            while (true)
            {
                try
                {
                    byte[] bytes = new byte[1024];
                    int bytesread = ns.Read(bytes, 0, bytes.Length);
                    string msg = Encoding.Default.GetString(bytes, 0, bytesread);                    
                    //顯示
                    MessageBox.Show(msg);
                    ns.Flush();//刷新流中的數(shù)據(jù)
                    //ns.Close();
                }
                catch(Exception ex)
                {
                    //MessageBox.Show ("與服務(wù)器斷開連接了");
                    MessageBox.Show(ex.Message);
                    break;
                }
            }
        }
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多