欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于Socket實(shí)現(xiàn)多人聊天功能

 更新時(shí)間:2022年02月11日 07:49:27   作者:以前是少年  
這篇文章主要為大家詳細(xì)介紹了C#基于Socket實(shí)現(xiàn)多人聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#基于Socket實(shí)現(xiàn)多人聊天功能的具體代碼,供大家參考,具體內(nèi)容如下

服務(wù)器

服務(wù)器負(fù)責(zé)接受所有客戶(hù)端發(fā)來(lái)的消息,和將接受到的問(wèn)題群發(fā)到其他用戶(hù)。

代碼:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace ChatRoomService
{
? ? class Service
? ? {
? ? ? ? Socket socketSevice ;
? ? ? ? List<Socket> userList;//用戶(hù)組
? ? ? ? public Service()
? ? ? ? {
? ? ? ? ? ?socketSevice = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? ? ?userList = new List<Socket>();
? ? ? ? }

? ? ? ? public void ?Start()
? ? ? ? {
? ? ? ? ? ? socketSevice.Bind(new IPEndPoint(IPAddress.Any,5566));
? ? ? ? ? ? socketSevice.Listen(10);
? ? ? ? ? ? Console.WriteLine("服務(wù)器啟動(dòng)成功");

? ? ? ? ? ? //開(kāi)啟接受連接,用多線(xiàn)程
? ? ? ? ? ? Thread accThread = new Thread(Accept);
? ? ? ? ? ? accThread.IsBackground = true;
? ? ? ? ? ? accThread.Start();
? ? ? ? }

? ? ? ? private void Accept()
? ? ? ? {
? ? ? ? ? ? //接受連接
? ? ? ? ? ? Socket clientSocket = socketSevice.Accept();
? ? ? ? ? ? userList.Add(clientSocket);
? ? ? ? ? ? //打印已經(jīng)連接IP地址
? ? ? ? ? ? Console.WriteLine(IPToAddress(clientSocket)+"連接進(jìn)來(lái)了");

? ? ? ? ? ? //
? ? ? ? ? ? Thread RecvThread = new Thread(ReceMessage);
? ? ? ? ? ? RecvThread.IsBackground = true;
? ? ? ? ? ? RecvThread.Start(clientSocket);

? ? ? ? ? ? Accept();//遞歸
? ? ? ? }
? ? ? ? //接收客戶(hù)端信息
? ? ? ? private void ReceMessage(Object obj)
? ? ? ? {
? ? ? ? ? ? Socket client = obj as Socket;
? ? ? ? ? ? byte[] strByte = new byte[1024 * 1024];//設(shè)定接受字符的長(zhǎng)度
? ? ? ? ? ? string str = "";
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? int len = client.Receive(strByte);//接受用戶(hù)發(fā)送的內(nèi)容
? ? ? ? ? ? ? str = Encoding.Default.GetString(strByte, 0, len);
? ? ? ? ? ? ? Broadcast(str,client);//廣播給用戶(hù)
? ? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? ? ? ?}
? ? ? ? ? ? ?catch (Exception e)
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? Console.WriteLine(IPToAddress(client)+"退出");
? ? ? ? ? ? ? ? userList.Remove(client);
? ? ? ? ? ? ? ? Thread.CurrentThread.Abort();//退出時(shí)掐死線(xiàn)程,不然遞歸反彈
? ? ? ? ? ? }
? ? ? ? ? ?ReceMessage(client); //使用遞歸
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 廣播信息
? ? ? ? /// </summary>
? ? ? ? /// <param name="useStr">傳入收到的傳輸?shù)膬?nèi)容</param>
? ? ? ? /// <param name="obj">傳送信息的客戶(hù)</param>
? ? ? ? private void Broadcast(string userStr,object obj)
? ? ? ? {
? ? ? ? ? ? Socket clientSend = obj as Socket; //當(dāng)前發(fā)送信息的客戶(hù)
? ? ? ? ? ? foreach (Socket client in userList)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (client != clientSend)//將信息廣播給其他用戶(hù)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? client.Send(Encoding.Default.GetBytes(IPToAddress(clientSend)+":"+userStr));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }? ? ? ? //轉(zhuǎn)換出連來(lái)客戶(hù)的IP地址
? ? ? ? private string IPToAddress(Socket soket)
? ? ? ? {
? ? ? ? ? ? return (soket.RemoteEndPoint as IPEndPoint).Address.ToString();
? ? ? ? }
? ? }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatRoomService
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Service ss = new Service();
? ? ? ? ? ? ss.Start();
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}

客戶(hù)端

客戶(hù)端的功能開(kāi)始十分簡(jiǎn)單,可以發(fā)送信息給服務(wù)器。也可以接收服務(wù)器轉(zhuǎn)發(fā)過(guò)來(lái)其他客戶(hù)端的信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ChatRoom
{
? ? class ClientRoom
? ? {
? ? ? ? Socket clientSocket;

? ? ? ? public ClientRoom()
? ? ? ? {
? ? ? ? ? ? clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化服務(wù)器
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 連接服務(wù)器
? ? ? ? /// </summary>
? ? ? ? /// <param name="Ip"></param>
? ? ? ? /// <param name="port"></param>
? ? ? ? public void Connected(string Ip,int port)
? ? ? ? {
? ? ? ? ? ? clientSocket.Connect(Ip,port);
? ? ? ? ? ? Console.WriteLine("連接成功");
? ? ? ? ? ? // ClientSocket.Bind(new IPEndPoint());

? ? ? ? ? ? Thread RecvThread = new Thread(RecvMessage);
? ? ? ? ? ? RecvThread.IsBackground = true;
? ? ? ? ? ? RecvThread.Start();
? ? ? ? }

? ? ? ?public void Send(String str)
? ? ? ? {
? ? ? ? ? ? clientSocket.Send(Encoding.Default.GetBytes(str));
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 接受信息
? ? ? ? /// </summary>
? ? ? ? private void RecvMessage()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? byte[] strByte = new byte[500 * 1024];
? ? ? ? ? ? ? ? int len = clientSocket.Receive(strByte);
? ? ? ? ? ? ? ? Console.WriteLine(Encoding.Default.GetString(strByte, 0, len));
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e) //服務(wù)器關(guān)閉
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("服務(wù)器關(guān)閉");
? ? ? ? ? ? ? ? Thread.CurrentThread.Abort();//關(guān)閉時(shí)切斷進(jìn)程
? ? ? ? ? ? }
? ? ? ? ? ? RecvMessage();
? ? ? ? } ? ? ? ?
? ? }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatRoom
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? ClientRoom client = new ClientRoom();
? ? ? ? ? ? client.Connected("127.0.0.1", 5566);
? ? ? ? ? ? string str = Console.ReadLine();
? ? ? ? ? ? while (!str.Equals("q"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? client.Send(str);
? ? ? ? ? ? ? ? str = Console.ReadLine();
? ? ? ? ? ? }
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}

可以正常對(duì)話(huà),測(cè)試一下。假裝和自己對(duì)話(huà)

目前還沒(méi)有解決沾包問(wèn)題

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論