0

I'm using c# MVC 4 as server, meaning I have no Views in it and it currently accept Http/s requests to the controllers, process them, query the db and returns a json object to the user.

I would like to enhance my server and add a udp (or tcp, i haven't completely decided yet) listener.

Problem is I haven't seen someone does it, everyone use either Console Application or WCF-like because the listener can be triggered by some sort of action.

I have a standard code from CodeProject

// Create UDP client
UdpClient client = new UdpClient(localPort);
UdpState state = new UdpState(client, remoteSender);
// Start async receiving
client.BeginReceive(new AsyncCallback(DataReceived), state);


private static void DataReceived(IAsyncResult ar)
{
    UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
    IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
    IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);

    // Check sender
    bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address) 
                       || wantedIpEndPoint.Address.Equals(IPAddress.Any);
    bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port)
                       || wantedIpEndPoint.Port == 0;
    if (isRightHost && isRightPort)
    {
        // Convert data to ASCII and print in console
        string receivedText = ASCIIEncoding.ASCII.GetString(receiveBytes);
        Console.Write(receivedText);
    }

    // Restart listening for udp data packages
    c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
}

My issue is, How can I set the listener to start working when I run the server?

Framework : .Net framework 4.5

Output type : Class Library

Ori Refael
  • 2,888
  • 3
  • 37
  • 68
  • I came up with a similiar idea before, but did not proceed because I could not justify a listener being dependent on my web application running...unless it was in response to some action taken through a controller. In which case I expect it would be done in controller actions. So perhaps you can explain why you want this listener to only run when you web application is running. This may clarify how / if you can accomplish your goal – hubson bropa Oct 13 '15 at 21:47
  • basically, i want to allow the user to upload a file to the server and I want the server to concentrate all user's requests and options in one place. basically i want to do it to experience with udp/tcp hosting and not just as a client. – Ori Refael Oct 13 '15 at 21:50
  • That's kind of like building an engine inside you car, that already has an engine. But if you really want to do it that way instead of a console app or windows service then put your code as outlined here http://stackoverflow.com/questions/21189854/execute-code-when-starting-an-asp-net-mvc-4-application – hubson bropa Oct 13 '15 at 22:03
  • i would really like to get a little more explanation than this about how you described what i do, i will accept this as an answer. why is it wrong? why my thinking is wrong here? – Ori Refael Oct 13 '15 at 22:08

0 Answers0