资源简介
设计在线网络售票程序。多人在多台售票机(客户端)上通过网络买票,要求如下:
总票数(>5000)存储在服务器端,各个客户端通过TCP协议将买票数(<100)发送到服务器。
服务器可以同时监听多个客户端的买票请求并执行。
如果售票数量超过现有票数,则这次售票被拒绝,服务器将拒绝信息发给客户端。
服务器显示各个连接的客户端的IP地址和端口号,买票数量及每次买票后的余票。
为了各个客户端的同步,要求用lock语句对服务器端的卖票代码加锁。
客户端显示买票前后的服务器剩余总票数。
代码片段和文件信息
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace shiyan3_SocketClient_91403090101
{
class Program
{
private static byte[] result = new Byte[1024];
static void Main(string[] args)
{
int tmp;//用于判断输入的数字
String input;//用于判断是否退出程序
//服务器IP地址
IPAddress ip = IPAddress.Parse(“127.0.0.1“);
Socket clientSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip 8012));
Console.WriteLine(“连接服务器成功“);
}
catch
{
Console.WriteLine(“连接服务器失败请按回车键退出“);
return;
}
//通过clientSocket接收数据
int receiveLength = clientSocket.Receive(result);
Console.WriteLine(“\n“ + “服务器剩余票数:“ + Encoding.Unicode.GetString(result 0 receiveLength));
L1: Console.Write(“请输入需要购买的票数:“);
String piao = Console.ReadLine();//piao:购买的票数
//判断是否输入正确
if (!int.TryParse(piao out tmp))
{
Console.WriteLine(“\n/***请输入数字***/\n“);
goto L1;
}
else if (int.Parse(piao) < 1 || int.Parse(piao) > 100)
{
Console.WriteLine(“\n/***至少购买1张票,最多购买100张票***/\n“);
goto L1;
}
// 通过clientSocket发送数据
try
{
clientSocket.Send(Encoding.Unicode.GetBytes(piao));
Console.WriteLine(“正在提交......“);
receiveLength = clientSocket.Receive(result);
Console.WriteLine(“\n“ + Encoding.Unicode.GetString(result 0 receiveLength) + “\n“);
Console.WriteLine(“/***是否还要继续购票***/\n“ + “\n 输入任意键继续购票 \n“退出”:请输入N“);
Console.Write(“输入:“);
input = Console.ReadLine().ToString();
if (input == “N“ || input == “n“)
{
clientSocket.Send(Encoding.Unicode.GetBytes(input));
return;
}
else
{
clientSocket.Send(Encoding.Unicode.GetBytes(input));
goto L1;
}
}
catch
{
Console.WriteLine(“\n/***服务器失去连接***/\n“);
}
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6144 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\bin\Debug\shiyan3_SocketClient_91403090101.exe
文件 13824 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\bin\Debug\shiyan3_SocketClient_91403090101.pdb
文件 11600 2016-11-27 21:08 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\bin\Debug\shiyan3_SocketClient_91403090101.vshost.exe
文件 490 2015-10-22 09:54 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\bin\Debug\shiyan3_SocketClient_91403090101.vshost.exe.manifest
文件 6053 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1755 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug\shiyan3_SocketClient_91403090101.csproj.FileListAbsolute.txt
文件 6144 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug\shiyan3_SocketClient_91403090101.exe
文件 13824 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug\shiyan3_SocketClient_91403090101.pdb
文件 2829 2016-11-27 21:07 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\Program.cs
文件 1396 2016-11-22 14:57 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\Properties\AssemblyInfo.cs
文件 2485 2016-11-22 14:57 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101.csproj
文件 938 2016-11-22 14:57 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101.sln
..A..H. 19456 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101.suo
文件 7168 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\bin\Debug\shiyan3_SocketSever_91403090101.exe
文件 13824 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\bin\Debug\shiyan3_SocketSever_91403090101.pdb
文件 11600 2016-11-27 20:58 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\bin\Debug\shiyan3_SocketSever_91403090101.vshost.exe
文件 490 2015-10-22 09:54 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\bin\Debug\shiyan3_SocketSever_91403090101.vshost.exe.manifest
文件 6049 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1729 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug\shiyan3_SocketSever_91403090101.csproj.FileListAbsolute.txt
文件 7168 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug\shiyan3_SocketSever_91403090101.exe
文件 13824 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug\shiyan3_SocketSever_91403090101.pdb
文件 5062 2016-11-27 21:10 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\Program.cs
文件 1394 2016-11-22 14:59 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\Properties\AssemblyInfo.cs
文件 2483 2016-11-22 14:59 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101.csproj
文件 935 2016-11-22 14:59 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101.sln
..A..H. 19456 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101.suo
目录 0 2016-11-22 14:57 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug\TempPE
目录 0 2016-11-22 14:59 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug\TempPE
目录 0 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketClient_91403090101\shiyan3_SocketClient_91403090101\obj\x86\Debug
目录 0 2016-11-27 21:11 shiyan3_91403090101\shiyan3_SocketSever_91403090101\shiyan3_SocketSever_91403090101\obj\x86\Debug
............此处省略18个文件信息
- 上一篇:C#坦克大战源代码全部用C#编写
- 下一篇:根据出生日期获取星座和生肖,农历等信息
评论
共有 条评论