我正在为一个课堂项目工作,我必须先创建一个套接字服务器,然后创建一堆从队列中请求票证的假客户端。我的所有工作都很好,但是,当我从Socket流中读取Ticket对象时,我只会收到第一个发送的对象。
在Console.WriteLine($"ID {paramData.ClientID} got ticket {tick.TicketID} for show {tick.EventName}");
行中,我获得了唯一的客户端ID,该ID在for循环期间生成,但是,每次都获得票证ID 0。
我有一些想法,但是我不确定为什么会这样。首先,s通过套接字发送第一张票证,然后每次都重复使用该票证,但是我不确定为什么会发生这种情况,因为我在发送票证之前将其出队。
另一个想法是我只是在完全错误地执行线程处理,但是我不明白同一票证如何在不同线程上显示。
这是执行代码的屏幕截图,其中包含10次购买尝试和5张可用票证
完整代码:
class Program
{
static ConcurrentQueue<Ticket> _TicketStorage = new ConcurrentQueue<Ticket>();
static Socket ServerSocket;
static void Main(string[] args)
{
Console.WriteLine("Starting Server ...");
// Gets the event name to apply to all the tickets
Console.Write("Enter Name of Event: ");
var eventName = Console.ReadLine();
// Allows the user to set the ticket count or defaults to 500
Console.Write("\nEnter Max Number of Tickets (Default: 500): ");
var ticketCountTry = int.TryParse(Console.ReadLine(), out int TicketCountResult);
// Setups up the ticket q
Console.WriteLine("Initilizing Ticket Storage");
for (int i = 0; i < (ticketCountTry ? TicketCountResult : 5); i++)
{
_TicketStorage.Enqueue(new Ticket(eventName, i));
}
Console.Clear();
// Finish line for application
Console.WriteLine("Server is ready to process requests");
var ServerThread = Task.Factory.StartNew(()=>
{
StartServerListener();
},TaskCreationOptions.LongRunning);
StartFakeClients();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
static void StartFakeClients()
{
Console.Write("Enter number of tickets to attempt to purchase (Default: 600): ");
var attemptNumberTry = int.TryParse(Console.ReadLine(), out int attemptAmount);
// Setup all the connections to be in the ready state
for (int i = 0; i < (attemptNumberTry ? attemptAmount : 10); i++)
{
ThreadPool.QueueUserWorkItem(FakeClientWorker, new
{
ClientID = i
});
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
static void FakeClientWorker(object state)
{
dynamic paramData = state;
// Create a connection to the server
var Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Connection.Connect(IPAddress.Loopback, 11000);
using (var bReader = new BinaryReader(new NetworkStream(Connection, true), Encoding.UTF8, false))
{
// Create my high level reader to parse incoming data
var dataBlock = bReader.ReadString();
if (!string.IsNullOrEmpty(dataBlock))
{
Ticket tick = JsonConvert.DeserializeObject<Ticket>(dataBlock);
Console.WriteLine($"ID {paramData.ClientID} got ticket {tick.TicketID} for show {tick.EventName}");
}
else
{
// Didn't get a ticket
Console.WriteLine($"ID {paramData.ClientID} didn't get a ticket");
}
}
}
static void StartServerListener()
{
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11000);
ServerSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(localEndPoint);
ServerSocket.Listen(100);
while (true)
{
var clientSocket = ServerSocket.Accept();
// You just got a new client asking for a ticket
// Send this connection to another thread and then continue listening for connections
ThreadPool.QueueUserWorkItem(DoServerWork, clientSocket);
}
}
static void DoServerWork(object clientSocket)
{
// Get the next ticket from the q
var hasNewTickets = _TicketStorage.TryDequeue(out Ticket EventTicket);
// Start a writer
var n = new NetworkStream((Socket)clientSocket, false);
using (var bWriter = new BinaryWriter(new NetworkStream((Socket)clientSocket, true), Encoding.UTF8, false))
{
// I have my writer now I need to send this data
bWriter.Write((hasNewTickets ? JsonConvert.SerializeObject(EventTicket) : String.Empty));
} // Dispose of the advanced network stream, Dispose of the binary writer, close the connection
}
}
/// <summary>
/// A structure for storing information about an event
/// </summary>
public class Ticket
{
/// <summary>
/// Unique identifer for the ticket
/// </summary>
public readonly int TicketID;
/// <summary>
/// Event name this ticket represents
/// </summary>
public readonly String EventName;
public Ticket(String EventName, int ID)
{
this.EventName = EventName;
TicketID = ID;
}
}
答案 0 :(得分:0)
我相信,这不是“ BinaryReader / Writer问题” 。您正在使用JsonConvert
(我认为来自Json.Net)对对象进行序列化和反序列化。我认为,如果您调试项目,则可以发现传递给dataBlock
方法的JsonConvert.DeserializeObject<Ticket>()
很好。
真正的原因是您的Ticket
类没有默认构造函数。在这种情况下,JsonConvert
将尝试将构造函数参数名称与JSON中的值进行匹配。这就是为什么您获得正确的EventName
,但默认为TicketID
(0)的原因。尝试将您的构造方法更改为此:
public Ticket(String EventName, int TicketID)
{
this.EventName = EventName;
this.TicketID = TicketID;
}