繁体   English   中英

使用BinaryReader / Writer的C#套接字从读取器获取错误的数据

[英]C# Socket using BinaryReader/Writer is getting the wrong data from reader

我正在上课的一个项目,我必须创建一个套接字服务器,然后创建一堆从队列中请求票证的假客户端。 我的所有工作都很好,但是,当我从Socket流中读取Ticket对象时,我只会收到第一个发送的对象。

Console.WriteLine($"ID {paramData.ClientID} got ticket {tick.TicketID} for show {tick.EventName}"); 我获得了在for循环期间生成的唯一客户端ID,但是每次获得的票证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;
    }
}

我相信,这不是“ 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM