Ejemplo n.º 1
0
        static void AutoSender(Socket socket)
        {
            while (true)
            {
                if (messages.Count != 0)
                {
                    UDPMessage udpm       = messages.Dequeue();
                    int        bufferSize = udpm.message.Length;
                    int        lastIndex  = 0;

                    byte[] byteLength = new byte[4];
                    string str        = string.Format($"{udpm.message.Length}");
                    for (int i = byteLength.Length - str.Length, j = 0; i < byteLength.Length; i++, j++)
                    {
                        byteLength[i] = (byte)int.Parse($"{str[j]}");
                    }
                    socket.SendTo(Buffer.AddStartCode(byteLength, 0), udpm.ipend);

                    while (bufferSize > 0)
                    {
                        socket.SendTo(Buffer.AddStartCode(Buffer.ChangeBufferSize(udpm.message, lastIndex, bufferSize < maxBufferSize ? bufferSize : maxBufferSize - 1), 1), udpm.ipend);
                        bufferSize -= maxBufferSize - 1;
                        lastIndex  += maxBufferSize - 1;
                    }
                    Console.WriteLine("Сообщение отправлено");
                }
            }
        }
Ejemplo n.º 2
0
        static void Send(object obj, Socket socket, IPEndPoint ipend)
        {
            byte[] message    = ToBinary(obj);
            int    bufferSize = message.Length;
            int    lastIndex  = 0;

            byte[] byteLength = new byte[4];
            string str        = string.Format($"{message.Length}");

            for (int i = byteLength.Length - str.Length, j = 0; i < byteLength.Length; i++, j++)
            {
                byteLength[i] = (byte)int.Parse($"{str[j]}");
            }
            socket.SendTo(Buffer.AddStartCode(byteLength, 0), ipend);

            while (bufferSize > 0)
            {
                byte[] messagePartBuffer = Buffer.ChangeBufferSize(message, lastIndex, bufferSize < maxBufferSize ? bufferSize : maxBufferSize - 1);
                socket.SendTo(Buffer.AddStartCode(messagePartBuffer, 1), ipend);
                bufferSize -= maxBufferSize - 1;
                lastIndex  += maxBufferSize - 1;
            }
        }