none
Send video and voice RRS feed

  • Question

  • Hello,

    I want to send a video file and a voice file(wav,mp4) over my TCP based connection.

    I searched a lot and haven't found a solution.

    Can someone give me a code or information about how should it be done?

    (C#)

    Thursday, January 25, 2018 1:35 PM

Answers

All replies

  • Your wav and mp4 files are just a stream of bytes.  You send it like you would send any other file.  Read a buffer full, send it, read the next buffer full, send it, etc.

    However, I suspect there is a lot more to your question that you didn't explicitly ask.  What are you planning to do with the files at the other end?  Are you just going to save them to disk, like a file server?  If so, that's easy.  Are you hoping to have the files play automatically at the other end?  That's way more complicated.

    Tell us more about your use case.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, January 26, 2018 1:22 AM
  • Hello Roy,

    >>I want to send a video file and a voice file(wav,mp4) over my TCP based connection

    The goal you want to achieve should be split as below steps.

    1.where the files go.  You need a server side whether it runs at own computer or others. it will listen request or communicate package.

    2. which protocol the server and client are communicated based on. There are some options like

    Application layer protocol: FTP protocol,  HTTP protocol and so on. Transport layer protocol: TCP/IP,UDP and so on. They both could be used to transfer files. According to your description, you want to transfer based TCP connection. Socket is good interface for programming TCP.

    3.Code in client side using C#.  You could use TcpClient directly or use Socket object that specify protocol is TCP.

    You could get started Socket programming here. and If you have any issues about above steps please feel free to contact me.

    Hope those would be helpful.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 26, 2018 9:41 AM
    Moderator
  • Yes I understand. I have already a server side and client side (both c#). Their connection is based on TCP protocol and so far it's working for me with text, files and images. I want to expend it and add the option to send the video and voice files. 
    So what I need is an example for how to get the video/voice file in the client section, convert it to bytes and stream it to the server that will save it.
    My method for sending files now is splitting the file to byte arrays in specific size and send them one after the other, and then the server receives them and combine them back to one file(and then save it).
    I believe that it should be similar with video and voice...
    Friday, January 26, 2018 10:52 AM
  • If you don't tell what you use to sent them to your server. It is again as my first message which is deleted. 

    With endless possibilities nobody can give a correct answer. 

    Be aware what you want to do is clear. However, as I try it always in analogies, try yourself to give a good answer on the question "I want to have a fine vacancy what can I do?"

    Show at least the method which you use to upload the file to the server. 


    Success Cor


    Friday, January 26, 2018 11:26 AM
  • TCP doesn't care about file types.  Any file can be treated as an array of bytes.  So what's the issue?
    Friday, January 26, 2018 11:58 AM
  • So how do i convert a mp4/wav file to bytes and backwards?

    Any exampl for code?

    Sunday, January 28, 2018 1:12 PM
  • So how do i convert a mp4/wav file to bytes and backwards?

    Any exampl for code?


    Well, how would you read the contents of any file containing binary data into a buffer?
    Sunday, January 28, 2018 1:31 PM
  • So how do i convert a mp4/wav file to bytes and backwards?

    Any exampl for code?


    Well, how would you read the contents of any file containing binary data into a buffer?

    So that is my code for converting images\text files (send + receive) :

    //sending
    byte[] SendingBuffer = null;
                NetworkStream ns = null;
                TcpClient client1 = null;
                int BufferSize = 1024;
                try
                {
                    client1 = new TcpClient();
                    client1.Connect(IP, port);
                    ns = client1.GetStream();
                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
    
                    int numberOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fs.Length) / Convert.ToDouble(BufferSize)));
                    int totalLength = (int)fs.Length, CurrentPacketLength;
                    for (int i = 0; i < numberOfPackets; i++)
                    {
                        if (totalLength > BufferSize)
                        {
                            CurrentPacketLength = BufferSize;
                            totalLength = totalLength - CurrentPacketLength;
                        }
                        else
                            CurrentPacketLength = totalLength;
                        SendingBuffer = new byte[CurrentPacketLength];
                        fs.Read(SendingBuffer, 0, CurrentPacketLength);
                        ns.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
                    }
                    fs.Close();
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    ns.Close();
                    client1.Close();
                }
    
    //receiving
    int BufferSize = 1024;
                byte[] RecData = new byte[BufferSize];
                int RecBytes;
                bool done = false;
                while (!done)
                {
                    try
                    {
                        NetworkStream ns = _client.GetStream();
                        int totalRecBytes = 0;
                        FileStream fs = new FileStream(saving_path, FileMode.OpenOrCreate, FileAccess.Write);
                        while ((RecBytes = ns.Read(RecData, 0, RecData.Length)) > 0)
                        {
                            fs.Write(RecData, 0, RecBytes);
                            totalRecBytes += RecBytes;
                        }
                        fs.Close();
                        done = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
    I found the code in the web, I believe video and voice should be similar but Im not sure what to do...

    Sunday, January 28, 2018 1:47 PM
  • Examine the code you posted.  Is there anything about it that is specific to text?  If not, then it should work equally well for any type of data.
    Sunday, January 28, 2018 1:54 PM
  • But how to I save it?
    I mean for example I used Image img = .... for image, what do I use for video?

    Or if I'll just change the saving format it will work? Because I tried and I had issues with that

    Sunday, January 28, 2018 6:56 PM
  • I haven't reviewed the code you posted and don't intend to.  As a test, you should use your client/server implementation on your local system to copy a file from one location to another on your hard drive.  Then compare the two files, the original and the copy, making sure it is a binary comparison.  If they are not identical that would indicate that the code is broken and you need to debug it to find the problem.
    Sunday, January 28, 2018 7:41 PM
  • Hi Roy Jiny,

    The following article provide a sample, which you could save the extension name as you want.

    public void ReceiveTCP(int portN)
    {
        TcpListener Listener = null;
        try
        {
            Listener = new TcpListener(IPAddress.Any, portN);
            Listener.Start();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    
        byte[] RecData = new byte[BufferSize];
        int RecBytes;
    
        for (; ; )
        {
            TcpClient client = null;
            NetworkStream netstream = null;
            Status = string.Empty;
            try
            {                          
                 string message = "Accept the Incoming File ";
                 string caption = "Incoming Connection";
                 MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                 DialogResult result;
    
                 if (Listener.Pending())
                 {
                      client = Listener.AcceptTcpClient();
                      netstream = client.GetStream();
                      Status = "Connected to a client\n";
                      result = MessageBox.Show(message, caption, buttons);
    
                      if (result == System.Windows.Forms.DialogResult.Yes)
                      {
                           string SaveFileName=string.Empty;
                           SaveFileDialog DialogSave = new SaveFileDialog();
                           DialogSave.Filter = "All files (*.*)|*.*";
                           DialogSave.RestoreDirectory = true;
                           DialogSave.Title = "Where do you want to save the file?";
                           DialogSave.InitialDirectory = @"C:/";
                           if (DialogSave.ShowDialog() == DialogResult.OK)
                                SaveFileName = DialogSave.FileName;
                           if (SaveFileName != string.Empty)
                           {
                               int totalrecbytes = 0;
                               FileStream Fs = new FileStream
    			(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
                               while ((RecBytes = netstream.Read
    				(RecData, 0, RecData.Length)) > 0)
                               {
                                    Fs.Write(RecData, 0, RecBytes);
                                    totalrecbytes += RecBytes;
                               }
                               Fs.Close();
                           }
                           netstream.Close();
                           client.Close();
                      }
                 }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //netstream.Close();
            }
        }
    }

    https://www.codeproject.com/Articles/32633/Sending-Files-using-TCP

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 30, 2018 9:13 AM
    Moderator
  • If you want to send the video and audio data from one computer so that it can be directly played back on another computer, this is called streaming. One way to implement it is to use the LEADTOOLS Multimedia SDK (https://www.leadtools.com/sdk/multimedia/media-streaming-server) which you can use to create a streaming server that takes the video data from a disk folder and make it available to be played on demand. (Disclaimer: I am the employee of the vendor of this toolkit).

    The code to create a server would look like this:

    _server = new Leadtools.MediaStreaming.Server(); 
    NetworkProperties props = _server.GetNetworkProperties(); 
    props.IPAddress = "0.0.0.0";
    props.Port = 6969;
    props.RTPPort = 6970;
    props.OpenWindowsFirewall = true; 
    props.Authentication = Authentication.None;
    props.UserName = "admin";
    props.Password = "password";
    props.Realm = "LTMS"; 
    props.IdleTimeOut = 70000; 
    props.RTCPTimeOut = 30000;
    props.ServerName = "LTMS/1.0";
    props.RTSPEnable = true; 
    props.HDSEnable = true;
    props.SSFEnable = true;
    props.DASHEnable = true; 
    props.MediaFolder = "Path to the folder containing your videos";
    _server.SetNetworkProperties(props);
    _server.Start()
    
    Once you have the video file in an appropriate stream, you can use the appropriate URL to play the video stream in a valid player.

    Thursday, March 15, 2018 4:21 PM
  • > I mean for example I used Image img = ... for image,
    > what do I use for video?

    The code you posted doesn't ever create an Image object.  And why should it?

    Why do you care what kind of data it is?  Why wouldn't you just use a FileStream and write the data out?  Your data stream should be literally just a stream of uninterpreted bytes.

    Now, maybe the question you asked isn't the one you meant to ask.  If you meant "I want to receive arbitrary files via TCP and then interpret them", that's a HUGE task.  That's essentially what web browsers do.  They have the MIME types to help identify the data, but even that isn't always enough.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, March 15, 2018 5:42 PM
  • OK, I think I got it (combining all answers)

    thanks everyone


    • Marked as answer by Roy Jiny Tuesday, March 27, 2018 5:35 PM
    • Edited by Roy Jiny Tuesday, March 27, 2018 5:36 PM
    Tuesday, March 27, 2018 5:35 PM