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

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

    • 分享

      C# Socket服務(wù)端和客戶端通話

       昵稱71011036 2020-08-10

      服務(wù)端

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net;
      using System.Net.Sockets;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace tSocket
      {
          class Program
          {
              byte[] bytes = new byte[1024];
              Socket cSocket;
              static void Main(string[] args)
              {
                  Program p = new Program();
                  //打開(kāi)鏈接
                  p.open();
                  //向服務(wù)端發(fā)送消息
                  Console.WriteLine("請(qǐng)輸入你要對(duì)服務(wù)端發(fā)送的消息:");
                  string mes = Console.ReadLine();
                  string con = p.messge(mes);
                  Console.WriteLine("接受到服務(wù)端的消息:" + con);
      
      
              }
              byte[] data = new byte[1024];
              string messge(string mes)
              {
                  //將發(fā)送的消息轉(zhuǎn)成字節(jié)數(shù)組
                  bytes = Encoding.UTF8.GetBytes(mes);
                  //發(fā)送
                  cSocket.Send(bytes);
                  while (true)
                  {
                      //接受服務(wù)端發(fā)送的消息,放入字節(jié)數(shù)組
                      int len = cSocket.Receive(data);
                      //將字節(jié)數(shù)組轉(zhuǎn)成可讀明文
                      string con = Encoding.UTF8.GetString(data, 0, len);
                      ////返回
                      return con;
                  }
                
              }
              /// <summary>
              /// 打開(kāi)鏈接
              /// </summary>
              void open()
              {
                  //創(chuàng)建Socket對(duì)象 指定連接方式
                  cSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  //創(chuàng)建IP,端口
                  IPAddress ip = IPAddress.Parse("10.116.253.10");
                  int port = 7526;
                  //封裝IP和端口
                  IPEndPoint Ipoint = new IPEndPoint(ip, port);
                  //打開(kāi)鏈接
                  cSocket.Connect(Ipoint);
              }
          }
      }

      客戶端

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net;
      using System.Net.Sockets;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace ServerSocket
      {
          class Program
          {
              
              static void Main(string[] args)
              {
                  //創(chuàng)建Socket對(duì)象,指定他的鏈接方式
                  Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  //建立IP
                  string ip = "10.116.253.10";
                  //創(chuàng)建端口
                  int prot = 7526;//1~9999
                  IPAddress IPAdd = IPAddress.Parse(ip);
                  //封裝IP和端口
                  IPEndPoint point = new IPEndPoint(IPAdd, prot);
                  //綁定IP和端口
                  serverSocket.Bind(point);
                  //開(kāi)始監(jiān)聽(tīng)
                  serverSocket.Listen(100);
                  Console.WriteLine("開(kāi)始監(jiān)聽(tīng)!");
      
                  int i = 0;
                  while (true)
                  {
                      i++;
                      //接受客戶鏈接
                      
                     Socket cSocket = serverSocket.Accept();
                     Console.WriteLine("接受第"+i+"個(gè)客戶的連接!");
                     Client c = new Client(cSocket);
                  }
      
              }
          }
      }
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net.Sockets;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      
      namespace ServerSocket
      {
          class Client
          {
              Socket sSocket;
              byte[] data = new byte[1024];
              Thread t;
              public Client(Socket cSocket)
              {
                  //接受客戶的連接
                  sSocket = cSocket;
                  //創(chuàng)建線程
                  t = new Thread(Mess);
                  //開(kāi)始線程
                  t.Start();
              }
      
              void Mess()
              {
                  try
                  {
                      while (true)
                      {
                          //將用戶發(fā)送的數(shù)據(jù)以一個(gè)字節(jié)數(shù)組裝起
                          int length = sSocket.Receive(data);
                          Console.WriteLine("接受客戶端發(fā)的消息!");
                          string mess = Encoding.UTF8.GetString(data, 0, length);
      
                          if (mess == "con")
                          {
                              string con = "DataSource =.";
                              byte[] bytes = Encoding.UTF8.GetBytes(con);
                              sSocket.Send(bytes);
                          }
                          Console.WriteLine("接到用戶的消息:" + mess);
                      }
                  }
                  catch (Exception)
                  {
                      sSocket.Close();
                  }
      
      
      
              }
          }
      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(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)論公約

        類(lèi)似文章 更多