已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/user类 存用户信息using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.IO;namespace Server1 class User/用于保存与客户通信需要的信息 public BinaryReader br get; private set; /定义User类的属性:可以以二进制方式读写流 public BinaryWriter bw get; private set; public TcpClient client get; private set; /为tcp网络服务提供客户端连接 public string userName get; set; /用户名 public User(TcpClient client)/带参数构造方法,初始化类的成员构造函数可理解为一种特殊的方法成员,它在每次创建对象时被自动调用,通常用于对象的初始化。 this.client = client;/client字段等于离它定义最近的client this.userName = ;/userName等于空 NetworkStream networkStream = client.GetStream(); /取得客户端发过来的网络流 br = new BinaryReader(networkStream);/将网络流作为二进制读写对象 bw = new BinaryWriter(networkStream); /服务器using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace Server1 public partial class MainForm : Form/初始化变量,将类、结构或接口的定义拆分到多个文件中,一个类定义在不同的类文件里 private List userList = new List();/保存连接的所有用户 IPAddress localAddress;/使用的本机IP地址 private const int port = 51000;/监听端口 private TcpListener myListener; bool isNormalExit = false;/是否正常退出所有接收线程 public MainForm() InitializeComponent();/InitializeComponent()初始化了添加到窗体上的所有控件,还初始化了窗体的属性 listBoxStatus.HorizontalScrollbar = true;/显示水平卷轴 IPAddress addrIP = Dns.GetHostAddresses(Dns.GetHostName();/获取本机ip localAddress = addrIP0;/获取本地ip buttonStop.Enabled = false;/停止服务不可以点 private void buttonStart_Click(object sender, EventArgs e)/【开始监听】按钮的Click事件 myListener = new TcpListener(localAddress, port);/创建对象 myListener.Start();/开始运行 listBoxStatus.Items.Add(string.Format(开始在0:1监听客户连接, localAddress, port);/listbox显示 Thread myThread = new Thread(ListenClientConnect); /创建一个线程监听客户端连接请求 myThread.Start(); buttonStart.Enabled = false; buttonStop.Enabled = true; private void ListenClientConnect()/接收客户端连接 TcpClient newClient = null;/tcpclient为tcp网络服务提供客户端链接 while (true) try/这里有异常才会跳到catch执行 /当单击“停止监听”或者退出此窗体时AcceptTcpClient()会产生异常 newClient = myListener.AcceptTcpClient(); catch break;/每接受一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息 User user = new User(newClient);/ Thread threadReceive = new Thread(ReceiveData);/创建一个线程接收客户端连接请求 threadReceive.Start(user);/开始 userList.Add(user);用户列表加一个 listBoxStatus.Items.Add(string.Format(0进入, newClient.Client.RemoteEndPoint);/显示 listBoxStatus.Items.Add(string.Format(当前连接用户数:0, userList.Count); private void ReceiveData(object userState)/ 处理接收的客户端数据 User user = (User)userState;/user状态 TcpClient client = user.client;/这里user的client是前文的client while (isNormalExit = false)/不正常退出 string receiveString = 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 (splitString0) case CONNECT: listBoxStatus.Items.Add(string.Format(来自0, user.client.Client.RemoteEndPoint); SendToClient(user, CONNECT, + userList0.userName); break; case LOGIN: user.userName = splitString1; listBoxStatus.Items.Add(string.Format(该用户用户名为0,splitString1); SendToClient(user, lOGIN, + userList0.userName); break; case EXIT: return; case COUNT: break; private void SendToClient(User user, string message)/将所有数据送给指定用户 try user.bw.Write(message);/写入流 user.bw.Flush();/清理缓冲区,使缓冲数据写入基础设备 catch(Exception)/获取所有异常 private void SendToAllClient(User user, string message)/发送信息给所有用户 string command = message.Split(,)0.ToLower();/把流分成字符串数组并转换小写 if (command = lOGIN)/如果命令是login for (int i = 0; i userList.Count; i+)/每个user都经历一次 SendToClient(userListi, message); if (userListi.userName != user.userName)/如果用户是个新的 SendToClient(user, lOGIN, + userListi.userName);/发送login else if (command = EXIT)/断了 for (int i = 0; i (sum / num) return -1000; else return sum / num; else return -1000; private string operators(int i) if (i = 0) return +; else if (i = 1) return -; else if (i = 2) return ; else return ; private void runter()/runter定义 bool isResult = false; int numb = 0, 0, 0, 0 ;/数字 numb0 = Int16.Parse(label8.Text); numb1 = Int16.Parse(label9.Text); numb2 = Int16.Parse(label10.Text); numb3 = Int16.Parse(label11.Text); int count = 0;/循环所有可能,4个输入数的每一种排列、对于每一类算式、对于每类算式中三个运算符的全部可能情况进行枚举,然后对每一个具体的表达式计算其值,看看结果是否24:a1b2c3d后面判断括号加在哪,再算得出是否24 for (int a = 0; a 4; a+) for (int b = 0; b 4; b+) for (int c = 0; c 4; c+) for (int d = 0; d 4; d+) if (this.isSame(a, b, c, d) /+,-,*,/,循-环* for (int a1 = 0; a1 4; a1+) for (int b1 = 0; b1 4; b1+) for (int c1 = 0; c1 4; c1+) /1,2,3算法括号加在哪的顺序 count = this.counter(c1, this.counter(b1, this.counter(a1, numba, numbb), numbc), numbd); if (count = 24) listBoxInfo.Items.Add( + numba + operators(a1) + numbb + ) + operators(b1) + numbc + ) + operators(c1) + numbd + = + count); isResult = true; count = this.counter(c1, this.counter(a1, numba, this.counter(b1, numbb, numbc), numbd); if (count = 24) listBoxInfo.Items.Add( + numba + operators(a1) + ( + numbb + operators(b1) + numbc + ) + operators(c1) + numbd + = + count); isResult = true; count = this.counter(b1, this.counter(a1, numba, numbb), this.counter(c1, numbc, numbd); if (count = 24) listBoxInfo.Items.Add( + numba + operators(a1) + numbb + ) + operators(b1) + ( + numbc + operators(c1) + numbd + ) + = + count); isResult = true; count = this.counter(a1, numba, this.counter(b1, numbb, this.counter(c1, numbc, numbd); if (count = 24) listBoxInfo.Items.Add( + numba + operators(a1) + ( + numbb + operators(b1) + ( + numbc + operators(c1) + numbd + ) + = + count); isResult = true; count = this.counter(a1, numba, this.counter(c1, this.counter(b1, numbb, numbc), numbd); if (count = 24) listBoxInfo.Items.Add( + numba + operators(a1) + ( + numbb + operators(b1) + numbc + ) + operators(c1) + numbd + ) + = + count); isResult = true; if (!isResult) MessageBox.Show(此副牌无解); break; private void buttonEnter_Click(object sender, EventArgs e)/确定 timer1.Stop(); runter(); string str = new stringlistBoxInfo.Items.Count; for (int i = 0; i listBoxInfo.Items.Count; i+)/循环结束条件 stri = listBoxInfo.Itemsi.ToString();/ if (textBox1.Text = stri)/输入的表达式和xxx一样 MessageBox.Show(恭喜您,您赢了,您的积分+1!);/弹框 jifen += 1; label24.Text = jifen.ToString(); break; else MessageBox.Show(对不起,您输
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026年考研外语阿尔及利亚语阅读理解专项训练习题
- 2025-2026年自然奥秘科普试卷
- ESG评价体系下六桶柜项目环境社会治理风险敞口与估值调整机制
- 2026年火电电力职业技能鉴定考试-交联操作工历年参考题库含答案解析
- 2026年湘西民族职业技术学院高职单招笔试数学试题库含答案解析3套试卷
- 2026年湖南电气职业技术学院高职单招笔试物理试题库含答案解析2套试卷
- 2026年湖南城建职业技术学院高职单招笔试职业适应性测验试题库含答案解析2套试卷
- 2026年湖北生物科技职业学院高职单招笔试职业适应性测验试题库含答案解析2套试卷
- 2026年浙江汽车职业技术学院高职单招笔试化学试题库含答案解析2套试卷
- 2026年浙江东方职业技术学院高职单招笔试语文试题库含答案解析2套试卷
- 自愿跟随协议书
- 2026年电容式触摸屏行业分析报告及未来发展趋势报告
- 2026年全国农产品质量安全检技能竞赛理论知识参能力检测试卷1套附答案详解
- 2026年小学食品安全培训
- 《禁止生物武器公约》信任措施机制空转-基于2024年缔约国提交年度宣布完整率
- 2025年高新投资集团笔试题目及答案
- 维修技师薪酬激励方案设计
- GB/T 17587.2-2025滚珠丝杠副第2部分:公称直径、公称导程、螺母尺寸和安装螺栓公制系列
- 《中华中医药学会标准肿瘤中医诊疗指南》
- 院感职业暴露知识培训课件
- 社会稳定风险评估报告汇报
评论
0/150
提交评论