Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:23:31+00:00 2026-06-09T16:23:31+00:00

I’m working on creating an Library in C# to use the Airplay protocol to

  • 0

I’m working on creating an Library in C# to use the Airplay protocol to send Photos and Video to my Apple TV (Specifically working with Generation 3 but hopefully that should not matter for this).

https://airlib.codeplex.com/

All of the commands for Airplay are HTTP on port 70 as per this spec:
http://nto.github.com/AirPlay.html

I have been successful at getting both photos and video to play on the Apple TV, but no matter what I do the AppleTV will only play 30 seconds worth of video. It appears as though my C# client that issues the play command is disconnecting right at 30 seconds, which causes the AppleTV to end the play session.

Reasons why I think this:

  • Terminating the client app completely produces the same behavior as waiting 30 seconds (essentially forcing the connection to close).
  • Manually closing the HttpWebRequest or TcpClient connection produces the same behavior (Midway through a play session).
  • Regardless of how long I hold the breakpoint to prevent the GetResponse() call the video always times out 30 seconds after the WebRequest begins sending the message.
  • Using a different source (IIS, external webserver) for the video does not change the behavior.
  • Even after the video has cached on the AppleTV and does not re-stream the timeout still occurs.

I’m pretty sure that the client request needs to stay connected throughout the “play” of the video, and to the best of my knowledge I have coded it to do that. I really am at my wits end. I have tried everything that I can think of including doing the request both as a HttpWebRequest and as a raw TcpClient (which both work but both time out), setting the Recieve/Send timeouts to crazy numbers, and looping the read of the Tcp stream to ensure that there is “activity”.

Its as though the AppleTV is expecting me to send a “hey, keep playing” message, but I have yet to see anything like that from any source on the web. I’m hoping that this is simply something stupid that I’m not doing based on my lack of Http/Tcp knowledge.

Here is my code:

    Uri url = "http://somevideo.com/video.mov";
    float startPosition = 0;        
    TcpClient tcpClient = new TcpClient("192.168.1.20",7000);
    tcpClient.ReceiveTimeout = 100000;
    tcpClient.SendTimeout = 100000;

    //get the client stream to read data from.
    NetworkStream clientStream = tcpClient.GetStream();

     string body = 
    "Content-Location: " + url + "\n" +
    "Start-Position: " + startPosition + "\n";

    string request = "POST /play HTTP/1.1\n" + 
    "User-Agent: MediaControl/1.0\n" +
    "Content-Type: text/parameters\n" +
    "Content-Length: " + Encoding.ASCII.GetBytes(body).Length + "\n" +           
    "X-Apple-Session-ID:" + _sessionGuid.ToString() + "\n\n";

    sendMessage(clientStream, request);
    sendMessage(clientStream, body);

    byte[] myReadBuffer = new byte[1024];
    StringBuilder myCompleteMessage = new StringBuilder();
    int numberOfBytesRead = 0;

    //incoming message might be bigger than the buffer
    do
    {
        try
        {
            numberOfBytesRead = clientStream.Read(myReadBuffer, 0, myReadBuffer.Length);
            myCompleteMessage.Append(Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
            Thread.Sleep(10);//let the iOS device catch up sending data
        }
        catch (System.IO.IOException) { }
    } while (tcpClient.Connected); //check if it's connected before checking for data available, as maybe the program might get quit and the sockets closed halfway through a read

Note: using telnet I am able to connect to the AppleTV on port 7000 and paste in this command which plays the entire video:

POST /play HTTP/1.1
User-Agent: MediaControl/1.0
Content-Type: text/parameters
Content-Length: 89
X-Apple-Session-ID:fb6d816a-a5ad-4e8f-8830-9642b6e6eb35

Content-Location: http://192.168.1.11:82/2012/2012_03_11/IMG_1328.MOV
Start-Position: 0

I’m running the Cassini Webserver on port 82, but this also works with IIS. This offers further evidence that the .Net stack is doing something under the hood at 30 seconds that causes a disconnect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:23:33+00:00Added an answer on June 9, 2026 at 4:23 pm

    I figured it out finally. It wasn’t the .Net code killing the connection, it was the Apple TV itself. With wireshark I was able to see the proper Ack and Fin messages which were from the AppleTV after 30 seconds of not recieving any new messages on that connection. To solve the problem I figured out by playing around with Telnet that the AppleTV doesn’t seem to care what you send it as long as you send it SOMETHING on a periodic basis, which seems to keep the connection alive.

    With HttpWebRequest the send/recieve portion is pretty canned. Its designed for a standard Http request and response, and if you need to do anything else you simply start a new HttpWebRequest rather than use the existing one. Trying to send a 2nd message on the same HttpWebRequest errors out.

    So I had to use a TcpClient, and had to rework the end.

        /// <summary>
        /// Starts a video.
        /// </summary>
        /// <param name="url">The URL of the video to play.</param>
        /// <param name="startPosition">The start position of the video. This value must be between 0 and 1</param>
        public void StartVideo(Uri url, float startPosition = 0)
        {
            if (startPosition > 1)
            {
                throw new ArgumentException("Start Position must be between 0 and 1");
            }
    
            TcpClient tcpClient = new TcpClient("192.168.1.20", 7000);
            tcpClient.ReceiveTimeout = 100000;
            tcpClient.SendTimeout = 100000;
    
            //get the client stream to read data from.
            NetworkStream clientStream = tcpClient.GetStream();
    
            string body =
           "Content-Location: " + url + "\n" +
           "Start-Position: " + startPosition + "\n";
    
            string request = "POST /play HTTP/1.1\n" +
            "User-Agent: MediaControl/1.0\n" +
            "Content-Type: text/parameters\n" +
            "Content-Length: " + Encoding.ASCII.GetBytes(body).Length + "\n" +
            "X-Apple-Session-ID:" + _sessionGuid.ToString() + "\n\n";
    
            //Send the headers
            sendMessage(clientStream, request);
            //Send the body
            sendMessage(clientStream, body);
    
            //Get the response
            byte[] myReadBuffer = new byte[1024];
            StringBuilder myCompleteMessage = new StringBuilder();
            int numberOfBytesRead = 0;
            numberOfBytesRead = clientStream.Read(myReadBuffer, 0, myReadBuffer.Length);
            myCompleteMessage.Append(Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    
            //Now start doing a "keepalive"
            while (true)
            {
                //Simply send the characters "ok" every two seconds
                sendMessage(clientStream, "ok");
                Thread.Sleep(2000);
            }                      
        }
    
        /// <summary>
        /// Sends a message across the NetworkStream
        /// </summary>
        /// <param name="clientStream">The stream to send the message down</param>
        /// <param name="message">The message to send</param>
        public void sendMessage(NetworkStream clientStream, string message)
        {
            byte[] buffer = new ASCIIEncoding().GetBytes(message);
            try
            {
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }
            catch (System.IO.IOException e)
            {
                Debug.WriteLine("IOException: " + e.Message);
            }
        }
    

    Obviously this is not the final answer, but this was the bare minimum to get it to work. If anybody figures out what the actual Apple hardware is sending in place of the “ok” please add a note.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.