溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用C#實(shí)現(xiàn)一個(gè)客戶端之間的通信

發(fā)布時(shí)間:2020-12-22 15:20:39 來(lái)源:億速云 閱讀:176 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

利用C#實(shí)現(xiàn)一個(gè)客戶端之間的通信?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

首先在服務(wù)端新建一個(gè)serverSocket,對(duì)其進(jìn)行初始化(一般包含AddressFamily:IP地址類型,SocketType:Socket傳輸數(shù)據(jù)方式,ProtoType:傳輸協(xié)議); 

接著我們要設(shè)置server端要綁定的IP:port;然后開(kāi)始監(jiān)聽(tīng),并設(shè)置最多同時(shí)監(jiān)聽(tīng)多少個(gè)Client.

這時(shí),服務(wù)端就在等待狀態(tài),直到某一個(gè)Client連接到這個(gè)ip:port上,這時(shí)serverSocket.Accept()工作,獲得這個(gè)連接。(此時(shí)的連接是有地址信息的哦!記得保存) 

獲得連接之后,server就可以和這個(gè)Client進(jìn)行通信了,當(dāng)加入第二個(gè)Client(我們稱其為ClientB)時(shí),Server接收到ClientB的消息,可以將這個(gè)消息轉(zhuǎn)發(fā)給前一個(gè)Client(我們稱其為ClientA),當(dāng)受到ClientA的消息,也可以轉(zhuǎn)發(fā)給ClientB。這樣就實(shí)現(xiàn)了Clients之間的通信了。(重點(diǎn)在保存連接信息?。。?br/>

那么現(xiàn)在貼上代碼講解:

Server端代碼 

namespace SocketServer
{
  class Program
  {
    private static byte[] result = new byte[1024];
   
    static Socket serverSocket;
    private static string client;
    private static Socket clientSocket;
    //我這里存了兩個(gè)Client,因?yàn)樽约弘娔X開(kāi)了兩個(gè)Client,不會(huì)有多的
    //理論上應(yīng)該開(kāi)一個(gè)Socket[]來(lái)保存信息,最好用一個(gè)二元組將client的信息和連接綁定起來(lái)
    //這樣就可以實(shí)現(xiàn)斷開(kāi)連接后下次登陸還是可以識(shí)別是這個(gè)Client
    private static Socket clientSocketA=null;
    private static Socket clientSocketB=null;
    
    static void Main(string[] args)
    {
      Program.SetPort(8885);
    }
    private static void SetPort(int port)
    {
      IPAddress ip = IPAddress.Parse("127.0.0.1");//set ip
      serverSocket = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);//initialize
      serverSocket.Bind(new IPEndPoint(ip, port));//bind
      serverSocket.Listen(10);
      //進(jìn)入監(jiān)聽(tīng)狀態(tài)
      Console.WriteLine("監(jiān)聽(tīng){0}成功", serverSocket.LocalEndPoint.ToString());
      //開(kāi)啟一個(gè)線程來(lái)監(jiān)聽(tīng)客戶端連接
      Thread myThread = new Thread(ListenClientConnect);
      myThread.Start();
      Console.ReadLine();

    }
    /// <summary>
    /// 監(jiān)聽(tīng)客戶端連接
    /// </summary>
    private static void ListenClientConnect()
    {
      while (true)
      {
        //Client連接上后 得到這個(gè)連接
        clientSocket = serverSocket.Accept();

        //這里我因?yàn)橹挥袃蓚€(gè)Client,所以就簡(jiǎn)單寫(xiě)了
        if (clientSocketA == null)
        {
          clientSocketA = clientSocket;
        }
        else if (clientSocketB == null)
        {
          clientSocketB = clientSocket;
        }
        else
        {
          //當(dāng)其中一個(gè)斷開(kāi)了,又重新連接時(shí),需要再次保存連接
          if (clientSocketB.IsBound)
          {
            clientSocketA = clientSocketB;
            clientSocketB = clientSocket;
          }
          else
          {
            clientSocketB = clientSocketA;
            clientSocketA = clientSocket;
          }
          
        }
        clientSocket.Send(Encoding.ASCII.GetBytes("say hello"));
        //開(kāi)個(gè)線程接收Client信息
        Thread receivedThread = new Thread(ReceiveMessage);
        receivedThread.Start(clientSocket);

      }
    }
   
    private static void ReceiveMessage(object clientSocket)
    {
      Socket myClientSocket = (Socket) clientSocket;
      
      while (true)
      {
        try
        {
          int revceiveNumber = myClientSocket.Receive(result);
          //Console.WriteLine("接受客戶端{(lán)0}消息{1}", myClientSocket.RemoteEndPoint.ToString()
          //  , Encoding.ASCII.GetString(result, 0, revceiveNumber));
          Console.WriteLine(Encoding.ASCII.GetString(result, 0, revceiveNumber));
          if (myClientSocket == clientSocketA)
          {
            Console.WriteLine("receive from A");
            if (clientSocketB!=null&&clientSocketB.IsBound)
            {
              Console.WriteLine("a IS BOUND");
              clientSocketB.Send(result, 0, revceiveNumber, SocketFlags.None);
            }
            else
            {
              myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
              Console.WriteLine("對(duì)方不在線上,發(fā)送失??!");
            }
          }
          else
          {
            Console.WriteLine("receive from B");
            if (clientSocketA != null && clientSocketA.IsBound)
            {
              Console.WriteLine("a IS BOUND");
              clientSocketA.Send(result, 0, revceiveNumber, SocketFlags.None);
            }  
            else
            {
              myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
              Console.WriteLine("對(duì)方不在線上,發(fā)送失??!");
            }

          }
          
        }
        catch(Exception ex)
        {
          Console.WriteLine(ex.Message);
          myClientSocket.Shutdown(SocketShutdown.Both);
          myClientSocket.Close();
          break;

        }
      }

    }
  }
}

Client端代碼(因?yàn)槎疾畈欢?就只貼一個(gè)了) 

namespace SocketClient
{
  class Program
  {
    private static byte[] result = new byte[1024];
    private static Socket clientSocket;
    private static void ListenServer()
    {
      while (true)
      {
        result = new byte[1024];
        int receiveLength = clientSocket.Receive(result);
        
        Console.WriteLine("{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
      }
      
    }
    static void Main(string[] args)
    {

      IPAddress ip = IPAddress.Parse("127.0.0.1");
      clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      try
      {
        clientSocket.Connect(ip, 8885);
        Console.WriteLine("連接成功!");

      }
      catch (Exception e)
      {
        Console.WriteLine("連接失??!");
        return;
      }
      Thread threadRead = new Thread(ListenServer);
      threadRead.Start();
     

      while(true)
      {
        try
        {
          Thread.Sleep(1000);
          string sendMessage = Console.ReadLine();
          clientSocket.Send(Encoding.ASCII.GetBytes("Sylvia:"+sendMessage));
          
        }
        catch (Exception ex)
        {
          clientSocket.Shutdown(SocketShutdown.Both);
          clientSocket.Close();
          break;
        }

      }
      Console.WriteLine("發(fā)送完畢 按回車退出");
      Console.ReadKey();
    }
  }
}

關(guān)于利用C#實(shí)現(xiàn)一個(gè)客戶端之間的通信問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI