您好,登录后才能下订单哦!
利用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。