• 大小: 470KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: C#
  • 标签: UDP  抢答器  套接字  C#  

资源简介

基于客户-服务模式的局域网简易抢答器,用C#+VS2005编写

资源截图

代码片段和文件信息

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.Sockets;
using System.Threading;
using System.Net;


namespace QuickAnswerClient
{
public partial class Answer : Form
{
private string userName;
private IPAddress serverIP;
private IPAddress clientIP;
private int serverPort;
private IPEndPoint myServer;
private Socket connectSocket;
private Thread receiveThread = null;
private const int _maxPacket = 64 * 1024;
private bool flagFirstHit;

#region 发送的消息
byte[] msgUsername;
byte[] msgQuit = System.Text.Encoding.Unicode.GetBytes(“quit“);
#endregion

public Answer(string user IPAddress ip int portSocket skt)
{
InitializeComponent();
userName = user;
serverIP = ip;
connectSocket = skt;
serverPort = port;
myServer = new IPEndPoint(serverIP serverPort);
clientIP = Dns.GetHostAddresses(Dns.GetHostName())[0];
lbLocalIP.Text = “本地IP:“ + clientIP;
lbUsername.Text = “用户名:“ + userName;
lbServerIP.Text = “服务器IP:“ + serverIP;
msgUsername = System.Text.Encoding.Unicode.GetBytes(userName);
flagFirstHit = true;
receiveThread = new Thread(new ThreadStart(begReceive));
receiveThread.Start();
}

#region 其他线程修改窗体控件
private void ChangebtnStartAnswer() //更新开始抢答按钮
{
btnStartAnswer.Enabled = true;
}
#endregion

private void begReceive()
        {
while (true)
{
try
{
byte[] packetBuff = new byte[_maxPacket];
connectSocket.Receive(packetBuff);
string sContent = System.Text.Encoding.Unicode.GetString(packetBuff).Trim(‘\0‘);
if (sContent == “cmdStop“)
{
MessageBox.Show(“服务器中断请退出客户端“ “提示“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
slbServerStatus.Text = “与服务器连接中断...“;
btnStartAnswer.Enabled = false;
return;
}
else if (sContent == “cmdReset“)
{
flagFirstHit = true;
MethodInvoker mi = new MethodInvoker(ChangebtnStartAnswer);
BeginInvoke(mi);
}
else if (sContent == “cmdSuccess“)
{
MessageBox.Show(“抢答成功“ “恭喜“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
}
else if (sContent == “cmdFail“)
{
MessageBox.Show(“抢答落后“ “遗憾“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
}
else if (sContent == “cmdFoul“)
{
MessageBox.Show(“抢答犯规 请等开始后再抢答“ “犯规“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
}
else
{
}
}
catch
{
if (receiveThread != null)
{
receiveThread.Abort();
}
connectSocket.Close();
break;
}
}
        }

private void StartAnswer()
{
try
{
connectSocket.Send(msgUsername);
}
catch
{
MessageBox.Show(“请确认已连接服务器“ “提示“ MessageBoxButtons.OK MessageBoxIcon.Exclamation);
s

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4264  2009-12-08 23:10  111\QuickAnswerClient\Answer.cs

     文件       7013  2007-12-20 22:14  111\QuickAnswerClient\Answer.Designer.cs

     文件       9674  2007-12-20 22:14  111\QuickAnswerClient\Answer.resx

     文件       4262  2007-12-20 22:14  111\QuickAnswerClient\Backup\Answer.cs

     文件       7013  2007-12-20 22:14  111\QuickAnswerClient\Backup\Answer.Designer.cs

     文件       9674  2007-12-20 22:14  111\QuickAnswerClient\Backup\Answer.resx

     文件       2216  2007-12-20 21:04  111\QuickAnswerClient\Backup\Login.cs

     文件       5637  2007-12-20 21:04  111\QuickAnswerClient\Backup\Login.Designer.cs

     文件       5814  2007-12-20 21:04  111\QuickAnswerClient\Backup\Login.resx

     文件       2238  2007-12-17 23:09  111\QuickAnswerClient\Backup\mycom.ico

     文件        398  2007-12-14 13:32  111\QuickAnswerClient\Backup\Program.cs

     文件       1217  2007-12-17 20:06  111\QuickAnswerClient\Backup\Properties\AssemblyInfo.cs

     文件       2541  2007-12-14 13:08  111\QuickAnswerClient\Backup\Properties\Resources.Designer.cs

     文件       5612  2007-12-14 13:08  111\QuickAnswerClient\Backup\Properties\Resources.resx

     文件       1023  2007-12-14 13:08  111\QuickAnswerClient\Backup\Properties\Settings.Designer.cs

     文件        249  2007-12-14 13:08  111\QuickAnswerClient\Backup\Properties\Settings.settings

     文件       3703  2007-12-18 15:04  111\QuickAnswerClient\Backup\QuickAnswerClient.csproj

     文件        922  2007-12-14 16:20  111\QuickAnswerClient\Backup\QuickAnswerClient.sln

    ..A..H.     25088  2007-12-20 22:15  111\QuickAnswerClient\Backup\QuickAnswerClient.suo

     文件      22016  2009-12-08 23:10  111\QuickAnswerClient\bin\Debug\QuickAnswerClient.exe

     文件      46592  2009-12-08 23:10  111\QuickAnswerClient\bin\Debug\QuickAnswerClient.pdb

     文件       5632  2005-12-08 14:51  111\QuickAnswerClient\bin\Debug\QuickAnswerClient.vshost.exe

     文件      28672  2007-12-20 22:14  111\QuickAnswerClient\bin\Release\QuickAnswerClient.exe

     文件      36352  2007-12-20 22:14  111\QuickAnswerClient\bin\Release\QuickAnswerClient.pdb

     文件      14328  2009-12-08 21:18  111\QuickAnswerClient\bin\Release\QuickAnswerClient.vshost.exe

     文件        490  2007-07-21 01:33  111\QuickAnswerClient\bin\Release\QuickAnswerClient.vshost.exe.manifest

     文件       2216  2007-12-20 21:04  111\QuickAnswerClient\Login.cs

     文件       5637  2007-12-20 21:04  111\QuickAnswerClient\Login.Designer.cs

     文件       5814  2007-12-20 21:04  111\QuickAnswerClient\Login.resx

     文件       2238  2007-12-17 23:09  111\QuickAnswerClient\mycom.ico

............此处省略143个文件信息

评论

共有 条评论