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

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

    • 分享

      利用Tcp和socket實(shí)現(xiàn)的客戶端與服務(wù)端的簡(jiǎn)單通信

       和諧世界 2020-12-11

      /*服務(wù)端*/

      using System;
      using System.Collections.Generic;
      using System.Collections;
      using System.Collections.Specialized;
      using System.Linq;
      using System.Text;
      using System.Net.Sockets;
      using System.Net;
      using System.Threading;
      namespace ChatSever
      {       class Sever
          {
              static void Main(string[] args)
              {
                  Hashtable clientTable = new Hashtable();//存放連接的轉(zhuǎn)發(fā)表            
                  IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];//返回主機(jī)的ip地址
                  int port=9999;
                  int maxsize=1024;
                  TcpListener listener = new TcpListener(ip, port);
                  listener.Start();
                  Console.WriteLine("服務(wù)器已啟動(dòng),正在監(jiān)聽....../n");
                  Console.WriteLine(string.Format("服務(wù)器IP:{0}/t端口號(hào):{1}/n",ip,port));
                  while (true)
                  {
                    byte[] packetBuff=new byte[maxsize];
                    Socket newClient = listener.AcceptSocket();
                    newClient.Receive(packetBuff);
                    string userName = Encoding.Unicode.GetString(packetBuff).TrimEnd('/0');
                    if (clientTable.Count != 0 && clientTable.ContainsKey(userName))//驗(yàn)證是否為唯一用戶
                    {
                        newClient.Send(Encoding.Unicode.GetBytes("Failed"));
                        continue;
                    }
                    else
                    {
                        newClient.Send(Encoding.Unicode.GetBytes("Successful"));

                    }
                  //將新的連接加入轉(zhuǎn)發(fā)表并創(chuàng)建線程為其服務(wù)
                    clientTable.Add(userName,newClient);
                    string strlog = string.Format("[系統(tǒng)消息]用戶{0}在{1}連接.... 當(dāng)前在線人數(shù):{2}/r/n/r/n",userName ,DateTime.Now ,clientTable.Count);
                     Console.WriteLine(strlog);
                     Thread thread = new Thread(new ParameterizedThreadStart(Sever.ThreadFunc));
                     thread.Start(userName);
                     //向所有客戶端發(fā)送系統(tǒng)消息
                     foreach (DictionaryEntry de in clientTable)
                     {
                         string clientName = de.Key as string;
                         Socket clientSkt = de.Value as Socket;
                         if (!clientName.Equals(userName))
                         {
                             clientSkt.Send(Encoding.Unicode.GetBytes(strlog));
                         }
                     }
                  }
              }

              static  void ThreadFunc(object obj)
              {
                 //代碼----啟動(dòng)新的線程監(jiān)聽來自客戶端的信息     

              }
          }
      }

      /*客戶端:*/
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Net;
      using System.Net.Sockets;
      namespace CustomProgram
      {
          class Custom
          {
              static void Main(string[] args)
              {
                byte [] data=new byte[1024];
                Socket newClient = new Socket(AddressFamily.InterNetwork ,SocketType.Stream,ProtocolType.Tcp);
                Console.WriteLine("請(qǐng)輸入服務(wù)器的地址:");
                string ipadd = Console.ReadLine();
                Console.WriteLine("請(qǐng)輸入服務(wù)的端口號(hào):");
                 int  port = Convert.ToInt32(Console.ReadLine());
                IPEndPoint ipend = new IPEndPoint(IPAddress .Parse(ipadd) ,port);
                try
                {
                    newClient.Connect(ipend);//連接服務(wù)器;
                }
                catch(SocketException e)
                {
                    Console.WriteLine("連接服務(wù)器失敗!");
                    Console.WriteLine(e.Message);
                    return;
                }
                int rec = newClient.Receive(data);
                string stringdata = Encoding.ASCII.GetString(data,0,rec);
                Console.WriteLine(stringdata);
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.ToUpper() == "EXIT")
                        break;
                    newClient.Send(Encoding.ASCII .GetBytes(input));
                    data =new byte[1024];
                    rec = newClient.Receive(data);
                    stringdata = Encoding.ASCII.GetString(data,0,rec);
                    Console.WriteLine("{0}來自服務(wù)器消息:{1}",DateTime.Now,stringdata);
                }
                Console.WriteLine("斷開連接!");
                newClient.Shutdown(SocketShutdown.Both);
                newClient.Close();
               }
          }
      }

      一上完成之后,右擊項(xiàng)目解決方案的屬性把項(xiàng)目設(shè)為多啟動(dòng),同時(shí)運(yùn)行客戶端與服務(wù)端即可;

      希望這個(gè)實(shí)例能對(duì)剛學(xué)C#網(wǎng)絡(luò)編程的人有所幫助吧,小弟水平很菜,有不對(duì)的地方還請(qǐng)論壇里的大俠給小弟指正!

        本站是提供個(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)論公約

        類似文章 更多