乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       昵稱15965102 2014-02-25
       

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

      分類: C# 1507人閱讀 評(píng)論(3) 收藏 舉報(bào)

      服務(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)
                  {
                      //測(cè)試是否有數(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);//流讀寫(xiě)器
                  //字組處理
                  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ù)回傳給客戶端,實(shí)際中,應(yīng)該經(jīng)過(guò)解析,將相應(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("與客戶端斷開(kāi)連接了");
                          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);//流讀寫(xiě)器
                  //字組處理
                  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ù)器斷開(kāi)連接了");
                          MessageBox.Show(ex.Message);
                          break;
                      }
                  }
              }
          }
      }

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

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多