设为首页 加入收藏

TOP

C#的Socket简单实现消息发送(四)
2015-02-03 22:27:47 来源: 作者: 【 】 浏览:56
Tags:Socket 简单 实现 消息 发送
nfo.msgBuffer = info.buffer;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? string msgTip = Encoding.ASCII.GetString(info.msgBuffer);
? ? ? ? ? ? ? ? if (msgTip == "\0\0\0faild")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? info.isConnected = false;
? ? ? ? ? ? ? ? ? ? if (this.OnDisConnected != null) OnDisConnected(info.socket.RemoteEndPoint.ToString());
? ? ? ? ? ? ? ? ? ? _listSocketInfo.Remove(info.socket.RemoteEndPoint.ToString());
? ? ? ? ? ? ? ? ? ? info.socket.Close();
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (OnReceiveMsg != null) OnReceiveMsg(info.socket.RemoteEndPoint.ToString());
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? public void SendMsg(string text, string endPoint)
? ? ? ? {
? ? ? ? ? ? if (_listSocketInfo.Keys.Contains(endPoint) && _listSocketInfo[endPoint] != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? _listSocketInfo[endPoint].socket.Send(Encoding.ASCII.GetBytes(text));
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? public void Stop()
? ? ? ? {
? ? ? ? ? ? _isListening = false;
? ? ? ? ? ? foreach (SocketInfo s in _listSocketInfo.Values)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? s.socket.Close();
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? public class SocketInfo
? ? ? ? {
? ? ? ? ? ? public Socket socket = null;
? ? ? ? ? ? public byte[] buffer = null;
? ? ? ? ? ? public byte[] msgBuffer = null;
? ? ? ? ? ? public bool isConnected = false;


? ? ? ? ? ? public SocketInfo()
? ? ? ? ? ? {
? ? ? ? ? ? ? ? buffer = new byte[1024 * 4];
? ? ? ? ? ? }
? ? ? ? }
? ? }


public class SocketServerManager


?


public class SocketClientManager
? ? {
? ? ? ? public Socket _socket = null;
? ? ? ? public EndPoint endPoint = null;
? ? ? ? public SocketInfo socketInfo = null;
? ? ? ? public bool _isConnected = false;


? ? ? ? public delegate void OnConnectedHandler();
? ? ? ? public event OnConnectedHandler OnConnected;
? ? ? ? public event OnConnectedHandler OnFaildConnect;
? ? ? ? public delegate void OnReceiveMsgHandler();
? ? ? ? public event OnReceiveMsgHandler OnReceiveMsg;


? ? ? ? public SocketClientManager(string ip, int port)
? ? ? ? {
? ? ? ? ? ? IPAddress _ip = IPAddress.Parse(ip);
? ? ? ? ? ? endPoint = new IPEndPoint(_ip, port);
? ? ? ? ? ? _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? }


? ? ? ? public void Start()
? ? ? ? {
? ? ? ? ? ? _socket.BeginConnect(endPoint, ConnectedCallback, _socket);
? ? ? ? ? ? _isConnected = true;
? ? ? ? ? ? Thread socketClient = new Thread(SocketClientReceive);
? ? ? ? ? ? socketClient.IsBackground = true;
? ? ? ? ? ? socketClient.Start();
? ? ? ? }


? ? ? ? public void SocketClientReceive()
? ? ? ? {
? ? ? ? ? ? while (_isConnected)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SocketInfo info = new SocketInfo();
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? _socket.BeginReceive(info.buffer, 0, info.buffer.Length, SocketFlags.None, ReceiveCallback, info);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (SocketException ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? _isConnected = false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? Thread.Sleep(100);
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? public void ReceiveCallback(IAsyncResult ar)
? ? ? ? {
? ? ? ? ? ? socketInfo = ar.AsyncState as SocketInfo;
? ? ? ? ? ? if (this.OnReceiveMsg != null) OnReceiveMsg();
? ? ? ? }


? ? ? ? public void ConnectedCallback(IAsyncResult ar)
? ? ? ? {
? ? ? ? ? ? Socket socket = ar.AsyncState as Socket;
? ? ? ? ? ? if (socket.Connected)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (this.OnConnected != null) OnConnected();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (this.OnFaildConnect != null) OnFaildConnect();
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? public void SendMsg(string msg)
? ? ? ? {
? ? ? ? ? ? byte[] buffer = Encoding.ASCII.GetBytes(msg);
? ? ? ? ? ? _socket.Send(buffer);
? ? ? ? }


? ? ? ? public class SocketInfo
? ? ? ? {
? ? ? ? ? ? public Socket socket = null;
? ? ? ? ? ? public byte[] buffer = null;


? ? ? ? ? ? public SocketInfo()
? ? ? ? ? ? {
? ? ? ? ? ? ? ? buffer =

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇AngularJS开发指南9:AngularJS作.. 下一篇C语言实例:同时实现两个文件的内..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: