当前位置:编程学习 > C#/ASP.NET >>

C# Socket 异步 聊天室

Socket 异步通信,线程池是由系统来维护线程的

注意:异步调用时,不能使用同步调用的方法,会线程阻塞

Server:

[csharp]
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
 
namespace SocketDemo 

    class ClientInfo 
    { 
        public Socket Socket { get; set; } 
        public bool IsOpen { get; set; } 
        public string Address { get; set; } 
    } 
 
    // 管理Client  
    class ClientManager 
    { 
        static List<ClientInfo> clientList = new List<ClientInfo>(); 
        public static void Add(ClientInfo info) 
        { 
            if (!IsExist(info.Address)) 
            { 
                clientList.Add(info); 
            } 
        } 
        public static bool IsExist(string address) 
        { 
            return clientList.Exists(item => string.Compare(address, item.Address, true) == 0); 
        } 
        public static void Close(string address) 
        { 
            clientList.ForEach(item => 
            { 
                if (string.Compare(address, item.Address, true) == 0) 
                { 
                    item.IsOpen = false; 
                } 
            }); 
        } 
        // 发送消息到ClientList  
        public static void SendMsgToClientList(string msg, string address = null) 
        { 
            byte[] bt = Encoding.UTF8.GetBytes(msg); 
            clientList.ForEach(item => 
            { 
                if (item.IsOpen && (address == null || item.Address != address)) 
                { 
                    item.Socket.BeginSend(bt, 0, bt.Length, SocketFlags.None, new AsyncCallback(SendTarget), item.Socket); 
                } 
            }); 
        } 
        private static void SendTarget(IAsyncResult res) 
        { 
            //Socket client = (Socket)res.AsyncState;  
            //int size = client.EndSend(res);  
        } 
    } 
 
    // 接收消息  
    class ReceiveHelper 
    { 
        public byte[] Bytes { get; set; } 
        public void ReceiveTarget(IAsyncResult res) 
        { 
            Socket client = (Socket)res.AsyncState; 
            int size = client.EndReceive(res); 
            if (size > 0) 
            { 
                string stringdata = Encoding.UTF8.GetString(Bytes, 0, size); 
 
                string address = client.RemoteEndPoint.ToString(); // 获取Client的IP和端口  
                if (stringdata.IndexOf("exit") > -1) 
                { 
                    ClientManager.SendMsgToClientList(address + "已从服务器断开", address); 
                    ClientManager.Close(address); 
                    Console.WriteLine(address + "已从服务器断开"); 
                    Console.WriteLine(address + " " + DateTimeOffset.Now.ToString("G")); 
                    return; 
                } 
                else 
                { 
                    Console.WriteLine(stringdata); 
                    Console.WriteLine(address + " " + DateTimeOffset.Now.ToString("G")); 
                    ClientManager.SendMsg

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,