UDP Streaming transfer from C# application to PHP webpage -
i'm trying code c# udp server. receives specific id client, , return song associated it. client php webpage, , stocks bytes received file. right i'm doing tests, trying start fake lecture of song (just javascript alert) when transfer @ 2048 bytes. have plenty of bugs... php page seems finish transfer file before having received data... server continue send bytes file complete, weight , all...
i know don't have english, if don't undersood something, ask !
here c# code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.threading; using system.io; using system.net.sockets; using system.net; using system.data.sqlite; namespace cdcollector { public partial class streaming : form { private static list<ipaddress> clients_ = new list<ipaddress>(); public streaming() { initializecomponent(); listen(); } public class threadclient { private static udpclient socket_; private static ipendpoint ipepclient_; private static int nochanson_; private static sqliteconnection connexion_; public void setsocket(ref udpclient socket) { socket_ = socket; } public void setipepclient(ref ipendpoint ipepclient) { ipepclient_ = ipepclient; } public void setnochanson(int nochanson) { nochanson_ = nochanson; } public void setconnexion(ref sqliteconnection connexion) { connexion_ = connexion; } public static void send() { try { while (thread.currentthread.isalive) { chanson unechanson; filestream stream; byte[] buffer = new byte[1024]; int read; unechanson = new chanson(nochanson_); unechanson.load(ref connexion_); stream = new filestream("c:\\users\\julie\\documents\\toune.flac", filemode.open, fileaccess.read); socket_.send(encoding.ascii.getbytes(stream.length.tostring()), stream.length.tostring().length, ipepclient_); while ((read = stream.read(buffer, 0, buffer.length)) > 0) socket_.send(buffer, buffer.length, ipepclient_); console.writeline("finished"); } } catch (threadabortexception tae) { } catch (exception) { thread.currentthread.abort(); } } } public static void listen() { byte[] data = new byte[1024]; ipendpoint ipepserver = new ipendpoint(ipaddress.any, 7575); // ip du serveur ipendpoint ipepclient = new ipendpoint(ipaddress.any, 0); // ip du client; udpclient socket = new udpclient(ipepserver); // socket serveur int nochanson; sqliteconnection connexion = new sqliteconnection("data source=" + application.startuppath + "\\cdcollector.db"); sqlitecommand command = new sqlitecommand(connexion); sqlitedatareader dr; thread thread; connexion.open(); while (true) { try { console.writeline("waiting client..."); data = socket.receive(ref ipepclient); console.writeline("message received {0}:", ipepclient.tostring()); console.writeline(encoding.ascii.getstring(data, 0, data.length)); command.commandtext = "select keylocale assockeys nomtable = 'chanson' , keyweb = " + int.parse(encoding.ascii.getstring(data, 0, data.length)); dr = command.executereader(); if (dr.hasrows) { dr.read(); nochanson = dr.getint32(0); dr.close(); threadclient client = new threadclient(); client.setconnexion(ref connexion); client.setipepclient(ref ipepclient); client.setnochanson(nochanson); client.setsocket(ref socket); thread = new thread(new threadstart(threadclient.send)); thread.start(); } else socket.send(encoding.ascii.getbytes("erreur: chanson introuvable"), ("erreur: chanson introuvable").length, ipepclient); } catch (socketexception se) { console.writeline("erreur socket:" + se.message); } catch (exception ex) { console.writeline("erreur: " + ex.message); } } connexion.close(); } } }
and php code:
<?php session_start(); $address="192.168.2.2"; $read = false; $port = 7575; $length = 0; $started = false; if (isset($port) , ($socket=socket_create(af_inet, sock_dgram, sol_udp)) , (socket_connect($socket, $address, $port))) { $text = "connection successful on ip $address, port $port <br />"; $from = ''; $port = 0; $length = 0; socket_send( $socket, $_get['no'], 1024, msg_eor ); socket_recvfrom( $socket, $buf, 1024, 12, $from, $port); $lengthtotal = $buf; echo "taille prévue du fichier: " . $lengthtotal . "<br />"; if( file_exists( "temp" . $_session['id_membre'] . ".flac" ) ) unlink("temp" . $_session['id_membre'] . ".flac"); $file = fopen("temp" . $_session['id_membre'] . ".flac", 'a'); $buf = null; while( $length < $lengthtotal ) { $length += socket_recvfrom( $socket, $buf, 1024, 12, $from, $port ); if( $length > 2048 && !$started ) { ?> <script type="text/javascript"> <!-- alert("lecture..."); //-> </script> <?php $started = true; } fputs($file, $buf); flush(); } echo "<br />" . $length . "<br />"; fclose($file); } else $text="unable connect<pre>".socket_strerror(socket_last_error())."</pre>"; echo $text; ?>
thanks lot !
udp inherently unreliable transport. need implement acknowledgements, timeouts, retransmissions , sequence numbers on top of udp in order guarantee transmission of of data in expected order, unless client application can live dropped packets. advise consider using tcp sockets instead if need guaranteed transmission of data between server , client , don't want have implement of stuff (which might need include client-side buffering rearrange out-of-order datagrams). if want reliability on top of udp, advise read textbook on subject (e.g. "unix network programming" w. richard stevens etc.).
pointers on tcp:
you should take @ system.net.sockets.tcpclient
, system.net.sockets.tcplistener
c# side of things , consult php documentation info on php side of things.
using tcp sockets isn't different except you'll using send
, recv
(or c#/php equivalents) instead of send_to
, recv_from
. setting server side of things little more complicated since need bind
, listen
etc. there plenty of resources, e.g.:
http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
Comments
Post a Comment