您好,登錄后才能下訂單哦!
利用C#實現一個客戶端之間的通信?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
首先在服務端新建一個serverSocket,對其進行初始化(一般包含AddressFamily:IP地址類型,SocketType:Socket傳輸數據方式,ProtoType:傳輸協議);
接著我們要設置server端要綁定的IP:port;然后開始監聽,并設置最多同時監聽多少個Client.
這時,服務端就在等待狀態,直到某一個Client連接到這個ip:port上,這時serverSocket.Accept()工作,獲得這個連接。(此時的連接是有地址信息的哦!記得保存)
獲得連接之后,server就可以和這個Client進行通信了,當加入第二個Client(我們稱其為ClientB)時,Server接收到ClientB的消息,可以將這個消息轉發給前一個Client(我們稱其為ClientA),當受到ClientA的消息,也可以轉發給ClientB。這樣就實現了Clients之間的通信了。(重點在保存連接信息!!)
那么現在貼上代碼講解:
Server端代碼
namespace SocketServer { class Program { private static byte[] result = new byte[1024]; static Socket serverSocket; private static string client; private static Socket clientSocket; //我這里存了兩個Client,因為自己電腦開了兩個Client,不會有多的 //理論上應該開一個Socket[]來保存信息,最好用一個二元組將client的信息和連接綁定起來 //這樣就可以實現斷開連接后下次登陸還是可以識別是這個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); //進入監聽狀態 Console.WriteLine("監聽{0}成功", serverSocket.LocalEndPoint.ToString()); //開啟一個線程來監聽客戶端連接 Thread myThread = new Thread(ListenClientConnect); myThread.Start(); Console.ReadLine(); } /// <summary> /// 監聽客戶端連接 /// </summary> private static void ListenClientConnect() { while (true) { //Client連接上后 得到這個連接 clientSocket = serverSocket.Accept(); //這里我因為只有兩個Client,所以就簡單寫了 if (clientSocketA == null) { clientSocketA = clientSocket; } else if (clientSocketB == null) { clientSocketB = clientSocket; } else { //當其中一個斷開了,又重新連接時,需要再次保存連接 if (clientSocketB.IsBound) { clientSocketA = clientSocketB; clientSocketB = clientSocket; } else { clientSocketB = clientSocketA; clientSocketA = clientSocket; } } clientSocket.Send(Encoding.ASCII.GetBytes("say hello")); //開個線程接收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("接受客戶端{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("對方不在線上,發送失敗!"); } } 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("對方不在線上,發送失敗!"); } } } catch(Exception ex) { Console.WriteLine(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } } } }
Client端代碼(因為都差不多 就只貼一個了)
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("發送完畢 按回車退出"); Console.ReadKey(); } } }
關于利用C#實現一個客戶端之間的通信問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。