您好,欢迎光临本网站![请登录][注册会员]  
文件名称: TCP通信实例
  所属分类: C#
  开发工具:
  文件大小: 11kb
  下载次数: 0
  上传时间: 2014-08-22
  提 供 者: bigh****
 详细说明: 包含客户端和服务端监听程序,以下是代码片段,想学习的可以参考以下 /// /// 监听客户端请求 /// private void ListenClientConnect() { TcpClient newClient = null; while (true) { ListenClientDelegate d = new ListenClientDelegate(ListenClient); IAsyncResult result = d.BeginInvoke(out newClient, null, null); //使用轮询方式来判断异步操作是否完成 while (result.IsCompleted == false) { if ( isExit) break; Thread.Sleep(250); } //获取Begin 方法的返回值和所有输入/输出参数 d.EndInvoke(out newClient, result); if (newClient != null) { //每接受一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息 User user = new User(newClient); Thread threadReceive = new Thread(ReceiveData); threadReceive.Start(user); userList.Add(user); AddItemToListBox(string.Format("[{0}]进入", newClient.Client.RemoteEndPoint)); AddItemToListBox(string.Format("当前连接用户数:{0}", userList.Count)); } else { break; } } } private void ReceiveData(object userState) { User user = (User)userState; TcpClient client = user.client; while (!isExit) { string receiveString = null; ReceiveMessageDelegate d = new ReceiveMessageDelegate(ReceiveMessage); IAsyncResult result = d.BeginInvoke(user, out receiveString, null, null); //使用轮询方式来判断异步操作是否完成 while (!result.IsCompleted) { if (isExit) break; Thread.Sleep(250); } //获取Begin方法的返回值和所有输入/输出参数 d.EndInvoke(out receiveString, result); if (receiveString == null) { if (!isExit) { AddItemToListBox(string.Format("与{0}失去联系,已终止接收该用户信息", client.Client.RemoteEndPoint)); RemoveUser(user); } break; } AddItemToListBox(string.Format("来自[{0}]:{1}", user.client.Client.RemoteEndPoint, receiveString)); string[] splitString = receiveString.Split(','); switch (splitString[0]) { case "Login": user.userName = splitString[1]; AsyncSendToAllClient(user, receiveString); break; case "Logout": AsyncSendToAllClient(user, receiveString); RemoveUser(user); return; case "Talk": string talkString = receiveString.Substring(splitString[0].Length + splitString[1].Length + 2); AddItemToListBox(string.Format("{0}对{1}说:{2}", user.userName, splitString[1], talkString)); foreach (User target in userList) { if (target.userName == splitString[1]) { AsyncSendToClient(target, "talk," + user.userName + "," + talkString); break; } } break; default: AddItemToListBox("什么意思啊:" + receiveString); break; } } } /// /// 异步发送信息给所有客户 /// /// /// private void AsyncSendToAllClient(User user, string message) { string command = message.Split(',')[0].ToLower(); if (command == "login") { for (int i = 0; i < userList.Count; i++) { AsyncSendToClient(userList[i], message); if (userList[i].userName != user.userName) AsyncSendToClient(user, "login," + userList[i].userName); } } else if (command == "logout") { for (int i = 0; i < userList.Count; i++) { if (userList[i].userName != user.userName) AsyncSendToClient(userList[i], message); } } } /// /// 异步发送message给user /// /// /// private void AsyncSendToClient(User user, string message) { SendToClientDelegate d = new SendToClientDelegate(SendToClient); IAsyncResult result = d.BeginInvoke(user, message, null, null); while (result.IsCompleted == false) { if (isExit) break; Thread.Sleep(250); } d.EndInvoke(result); } private delegate void SendToClientDelegate(User user, string message); /// /// 发送message给user /// /// /// private void SendToClient(User user, string message) { try { //将字符串写入网络流,此方法会自动附加字符串长度前缀 user.bw.Write(message); user.bw.Flush(); AddItemToListBox(string.Format("向[{0}]发送:{1}", user.userName, message)); } catch { AddItemToListBox(string.Format("向[{0}]发送信息失败", user.userName)); } } /// /// 移除用户 /// /// private void RemoveUser(User user) { userList.Remove(user); user.Close(); AddItemToListBox(string.Format("当前连接用户数:{0}", userList.Count)); } delegate void ReceiveMessageDelegate(User user, out string receiveMessage); /// /// 接收客户端发来的信息 /// /// /// private void ReceiveMessage(User user, out string receiveMessage) { try { receiveMessage = user.br.ReadString(); } catch (Exception ex) { AddItemToListBox(ex.Message); receiveMessage = null; } } private delegate void ListenClientDelegate(out TcpClient client); /// /// 接受挂起的客户端连接请求 /// /// ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: tcp通信
 输入关键字,在本站1000多万海量源码库中尽情搜索: