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 3499364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:36:09+00:00 2026-05-18T12:36:09+00:00

I’m trying to code a C# UDP server. It receives a specific ID from

  • 0

I’m trying to code a C# UDP server. It receives a specific ID from the client, and return the song associated with it. The client is a PHP webpage, and stocks the bytes received into a file. Right now I’m doing some tests, trying to simply start a fake lecture of the song (just a javascript alert) when the transfer is at 2048 bytes. But I have plenty of bugs… The PHP page seems to finish the transfer into the file BEFORE having received all the data… The server continue to send bytes but the file is complete, with the good weight and all…

I know I don’t have a very good english, so if you don’t undersood something, just ask !

Here is the 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 for a client...");

                data = socket.Receive(ref ipepClient);

                Console.WriteLine("Message received from {0}:", ipepClient.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));



                command.CommandText = "SELECT KeyLocale FROM AssocKeys WHERE NomTable = 'Chanson' AND 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 the PHP code:

<?php
 session_start();
$address="192.168.2.2";
$read = false;
$port = 7575;
$length = 0;
$started = false;

if (isset($port) and
($socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) and
(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 to connect<pre>".socket_strerror(socket_last_error())."</pre>";

echo $text;
?>

Thanks a lot !

  • 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-05-18T12:36:10+00:00Added an answer on May 18, 2026 at 12:36 pm

    UDP is an inherently unreliable transport. You will need to implement acknowledgements, timeouts, retransmissions and sequence numbers on top of UDP in order to guarantee transmission of all of your data in the expected order, unless your client application can live with dropped packets. I would advise you to consider using TCP sockets instead if you need guaranteed transmission of data between server and client and don’t want to have to implement all of this stuff yourself (which might need to include client-side buffering to rearrange out-of-order datagrams). If you want reliability on top of UDP, I would advise you to read a good textbook on the subject (e.g. “Unix Network Programming” by W. Richard Stevens etc.).

    Pointers on TCP:

    You should take a look at System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener for the C# side of things and consult the PHP documentation for info on the PHP side of things.

    Using TCP sockets isn’t really that much different except you’ll be using send and recv (or C#/PHP equivalents) instead of send_to and recv_from. Setting up the server side of things is a little more complicated since you need to bind and listen etc. but there are plenty of resources, e.g.:

    http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.