none
Creating a TCP socket connection & verify CDPF

    Question

  • I have created a SL4 project that when a button "Connect" is pressed it created a TCP connection to my embedded device so i can pass information back and forth. THis works just fine in debug mode. When i go to release and try it, it of course requests the policy file on port 943. I have created a second listening socket for this port and reply back with the following file when the "<policy-file-request/>" comes in.I have used a packet sniffer, and i can see that i am trully sending the policy file back, but it still does not allow me to then create the TCP socket connection as before. I have suceesfully done this same thing using Flex, so i have knowledge about doing this. Just that i am new to SL and cannot get this to work as i have in adobe Flex.

    <?xml version="1.0"?>  
    <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">  
    <cross-domain-policy>  
        <site-control permitted-cross-domain-policies="all"/>  
        <allow-access-from domain="*"/>  
        <allow-http-request-headers-from domain="*" headers="*"/>  
    </cross-domain-policy>
    
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                DnsEndPoint endPoint = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, 4502);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
    
                Debugwindow.Text += Application.Current.Host.Source.AbsoluteUri + "\n";
                Debugwindow.Text += Application.Current.Host.Source.AbsolutePath + "\n";
                Debugwindow.Text += Application.Current.Host.Source.DnsSafeHost + "\n";
    
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken = socket;
                args.RemoteEndPoint = endPoint;
                args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
    
                socket.ConnectAsync(args);
            }
    
    Sunday, November 06, 2011 11:34 AM

Answers

  • Hi. The problem is that the flash policy file (crossdomain.xml) only works for the high-level classes (like WebClient). It does not work for the classes in the sockets namespace (more info here). 

    To make this work, you need to provide Silverlight's own policy file format, as "clientaccesspolicy.xml" in the root of your server. On the same page I linked to above you can find a "sample policy file for sockets" (scroll down to the bottom) as a good starting point. 

    Monday, November 07, 2011 5:54 AM