简体   繁体   中英

C# Send string and video stream at the same time over Socket

I am using Sockets in C# to send string over network. I am sending strings without any problems but I am trying to stream video in byte Array with string at the same time. In other words, I would like to send two byte Arrays as one message.

Why I need it? I need it to stream to particular user and the way I do it is by specifying in string username who should receive the video and second byte Array would be accepted by that user and by nobody else.

I send string by using this code:

public static void Send(string text) //Send String to the Server
    {
        try
        {
            byte[] buffer = Encoding.UTF32.GetBytes(text);
            Main.AppControls.ClientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

I was wondering if it is possible to do something like this to make one sending function not separate but as one.

public static void Send(string text) //Send String to the Server
    {
        try
        {
            byte[] buffer = Encoding.UTF32.GetBytes(text, byteVideoStream); <---- THIS IS WHAT I NEED
            Main.AppControls.ClientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

Obviously if I stream only one byte Array with video stream and receive it in server side, I won't be able to specify person who would receive it. But if with string it has second byte array I can navigate to user who would receive it.

UPDATED I have established protocol between Sender and Receiver When server receives a string byte array it will read first word and if it contains for example "Connect" then server pass this to all clients:

if (_function== "Connect") // Client Sending Connection
   {
   foreach (Socket _connectedUsers in Server.clientSockets) //Send message back to everyone but accepts only this user.
           {
             byte[] data = Encoding.UTF32.GetBytes("Connected" + "-" + "UserName"); //Passing Message to Other Clients from where specific client will pick it up
             Socket socket = (Socket)_connectedUsers;
             socket.Send(data);
           }

    }

You can send whatever raw bytes you want to across your socket.

It's up to the receiving end to understand what data you are sending and interpret it appropriately.

The problem you'll need to solve is establishing a protocol between sender and receiver to understand where the string ends and the video stream begins. You might do this, for example, by terminating every string with a UTF32 character that won't be part of an ordinary message or by sending a fixed-length string.

If I stream only one byte Array with video stream and receive it in server side, I won't be able to specify person who would receive it

I'm not sure what you mean by this. The open socket is already connected to a specific device on the other end. If your receiving code somehow routes/proxies the video stream to another device, you could for example use the string portion to specify a final recipient of the video stream and have your receiver open a new socket to the final recipient and copy the video portion of your stream to that new socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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