部分代码:
命名空间:
代码如下 | 复制代码 |
using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; |
mainform.cs
代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace SyncChatServer /// <summary> /// <summary> /// <summary> public MainForm() /// <summary> /// <summary> /// <summary> } /// <summary> /// <summary> /// <summary> /// <summary> private delegate void AddItemToListBoxDelegate(string str); /// <summary> /// <summary> |
代码片段:
代码如下 | 复制代码 |
/// <summary> /// 根据当前程序目录的文本文件‘ServerIPAndPort.txt’内容来设定IP和端口 /// 格式:127.0.0.1:8885 /// </summary> private void SetServerIPAndPort() { FileStream fs = new FileStream("ServerIPAndPort.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string IPAndPort = sr.ReadLine(); ServerIP = IPAndPort.Split(':')[0]; //设定IP port = int.Parse(IPAndPort.Split(':')[1]); //设定端口 sr.Close(); fs.Close(); } /// <summary> /// 开始监听 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Start_Click(object sender, EventArgs e) { myListener = new TcpListener(IPAddress.Parse(ServerIP), port); myListener.Start(); AddItemToListBox(string.Format("开始在{0}:{1}监听客户连接", ServerIP, port)); //创建一个线程监客户端连接请求 Thread myThread = new Thread(ListenClientConnect); myThread.Start(); btn_Start.Enabled = false; btn_Stop.Enabled = true; } /// <summary> /// 接收客户端连接 /// </summary> private void ListenClientConnect() { TcpClient newClient = null; while (true) { try { newClient = myListener.AcceptTcpClient(); } catch { //当单击‘停止监听’或者退出此窗体时 AcceptTcpClient() 会产生异常 //因此可以利用此异常退出循环 break; } //每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息; 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)); } } /// <summary> /// 处理接收的客户端信息 /// </summary> /// <param name="userState">客户端信息</param> private void ReceiveData(object userState) { User user = (User)userState; TcpClient client = user.client; while (isNormalExit == false) { string receiveString = null; try { //从网络流中读出字符串,此方法会自动判断字符串长度前缀 receiveString = user.br.ReadString(); } catch (Exception) { if (isNormalExit == false) { 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]; SendToAllClient(user,receiveString); break; case "Logout": SendToAllClient(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)); SendToClient(user,"talk," + user.userName + "," + talkString); foreach(User target in userList) { if(target.userName == splitString[1] && user.userName != splitString[1]) { SendToClient(target,"talk," + user.userName + "," + talkString); break; } } break; default: AddItemToListBox("什么意思啊:" + receiveString); break; } } } |
user.cs
代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.IO; namespace SyncChatServer public User(TcpClient client) public void Close() } |
program.cs
代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace SyncChatServer |
main.cs
代码如下 | 复制代码 |
namespace SyncChatServer { partial class MainForm { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> #region Windows 窗体设计器生成的代码 /// <summary> } #endregion private System.Windows.Forms.GroupBox groupBox1; |
以上是不完整的代码片段,由于代码太多这里贴出来肯定眼花缭乱的。。
有需要的朋友还是下载源码看把
点击下载源文件