24点游戏注释版.docx
- 文档编号:2956188
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:19
- 大小:21.32KB
24点游戏注释版.docx
《24点游戏注释版.docx》由会员分享,可在线阅读,更多相关《24点游戏注释版.docx(19页珍藏版)》请在冰豆网上搜索。
24点游戏注释版
//user类存用户信息
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.IO;
namespaceServer1
{
classUser//用于保存与客户通信需要的信息
{
publicBinaryReaderbr{get;privateset;}//定义User类的属性:
可以以二进制方式读写流
publicBinaryWriterbw{get;privateset;}
publicTcpClientclient{get;privateset;}//为tcp网络服务提供客户端连接
publicstringuserName{get;set;}//用户名
publicUser(TcpClientclient)//带参数构造方法,初始化类的成员构造函数可理解为一种特殊的方法成员,它在每次创建对象时被自动调用,通常用于对象的初始化。
{
this.client=client;//client字段等于离它定义最近的client
this.userName="";//userName等于空
NetworkStreamnetworkStream=client.GetStream();//取得客户端发过来的网络流
br=newBinaryReader(networkStream);//将网络流作为二进制读写对象
bw=newBinaryWriter(networkStream);
}
}
}
//服务器
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Threading;
namespaceServer1
{
publicpartialclassMainForm:
Form//初始化变量,将类、结构或接口的定义拆分到多个文件中,一个类定义在不同的类文件里
{
privateList
IPAddresslocalAddress;//使用的本机IP地址
privateconstintport=51000;//监听端口
privateTcpListenermyListener;
boolisNormalExit=false;//是否正常退出所有接收线程
publicMainForm()
{
InitializeComponent();//InitializeComponent()初始化了添加到窗体上的所有控件,还初始化了窗体的属性
listBoxStatus.HorizontalScrollbar=true;//显示水平卷轴
IPAddress[]addrIP=Dns.GetHostAddresses(Dns.GetHostName());//获取本机ip
localAddress=addrIP[0];//获取本地ip
buttonStop.Enabled=false;//停止服务不可以点
}
privatevoidbuttonStart_Click(objectsender,EventArgse)//【开始监听】按钮的Click事件
{
myListener=newTcpListener(localAddress,port);//创建对象
myListener.Start();//开始运行
listBoxStatus.Items.Add(string.Format("开始在{0}:
{1}监听客户连接",localAddress,port));//listbox显示
ThreadmyThread=newThread(ListenClientConnect);//创建一个线程监听客户端连接请求
myThread.Start();
buttonStart.Enabled=false;
buttonStop.Enabled=true;
}
privatevoidListenClientConnect()//接收客户端连接
{
TcpClientnewClient=null;//tcpclient为tcp网络服务提供客户端链接
while(true)
{
try//这里有异常才会跳到catch执行
//当单击“停止监听”或者退出此窗体时AcceptTcpClient()会产生异常
{
newClient=myListener.AcceptTcpClient();
}
catch
{
break;
}
//每接受一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息
Useruser=newUser(newClient);//
ThreadthreadReceive=newThread(ReceiveData);//创建一个线程接收客户端连接请求
threadReceive.Start(user);//开始
userList.Add(user);用户列表加一个
listBoxStatus.Items.Add(string.Format("[{0}]进入",newClient.Client.RemoteEndPoint));//显示
listBoxStatus.Items.Add(string.Format("当前连接用户数:
{0}",userList.Count));
}
}
privatevoidReceiveData(objectuserState)//处理接收的客户端数据
{
Useruser=(User)userState;//user状态
TcpClientclient=user.client;//这里user的client是前文的client
while(isNormalExit==false)//不正常退出
{
stringreceiveString=null;
try
{//从网络流中读出字符串,此方法会自动判断字符串长度前缀,并根据长度前缀读出字符串
receiveString=user.br.ReadString();
}
catch
{
if(isNormalExit==false)//非正常退出
{
listBoxStatus.Items.Add(string.Format("与[{0}]失去联系,已终止接收该用户信息",client.Client.RemoteEndPoint));
userList.Remove(user);//用户移除
}
break;
}
string[]splitString=receiveString.Split(',');//把收到的字符串按,分隔然后赋给splitString
switch(splitString[0])
{
case"CONNECT":
listBoxStatus.Items.Add(string.Format("来自[{0}]",user.client.Client.RemoteEndPoint));
SendToClient(user,"CONNECT,"+userList[0].userName);
break;
case"LOGIN":
user.userName=splitString[1];
listBoxStatus.Items.Add(string.Format("该用户用户名为[{0}]",splitString[1]));
SendToClient(user,"lOGIN,"+userList[0].userName);
break;
case"EXIT":
return;
case"COUNT":
break;
}
}
}
privatevoidSendToClient(Useruser,stringmessage)//将所有数据送给指定用户
{
try
{
user.bw.Write(message);//写入流
user.bw.Flush();//清理缓冲区,使缓冲数据写入基础设备
}
catch(Exception)//获取所有异常
{}
}
privatevoidSendToAllClient(Useruser,stringmessage)//发送信息给所有用户
{
stringcommand=message.Split(',')[0].ToLower();//把流分成字符串数组并转换小写
if(command=="lOGIN")//如果命令是login
{
for(inti=0;i { SendToClient(userList[i],message); if(userList[i].userName! =user.userName)//如果用户是个新的 { SendToClient(user,"lOGIN,"+userList[i].userName);//发送login } } } elseif(command=="EXIT")//断了 { for(inti=0;i { if(userList[i].userName! =user.userName) { SendToClient(userList[i],message); } } } } privatevoidRemoveUser(Useruser)//移除用户 { thrownewNotImplementedException();//让程序抛出一个异常 } privatevoidbuttonStop_Click(objectsender,EventArgse)//停止服务按钮 { myListener.Stop();//停止监听 buttonStart.Enabled=true; buttonStop.Enabled=false; } privatevoidMainForm_FormClosing(objectsender,FormClosingEventArgse)//窗口关闭 { if(myListener! =null) { buttonStop.Perfo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 24 游戏 注释