21.7 C
New York
Monday, April 28, 2025

c# – How can I get a response from my server operating a udp socket between my server and my residence?


I’ve an ac# udp socket from which I’m making an attempt to ship information to my residence. The server socket is hosted on aws and the shopper is at my home. I modified the code from the next GitHub to ship a response to the shopper after the server receives the primary message despatched by the shopper: https://gist.github.com/darkguy2008/413a6fea3a5b4e67e5e0d96f750088a9.

Right here is the shopper code:

    public void Consumer(string deal with, int port)
    {
        _socket.Join(IPAddress.Parse(deal with), port);
        Obtain();            
    }

    public void Ship(string textual content)
    {
        byte() information = Encoding.ASCII.GetBytes(textual content);
        _socket.BeginSend(information, 0, information.Size, SocketFlags.None, (ar) =>
        {
            State so = (State)ar.AsyncState;
            int bytes = _socket.EndSend(ar);
            Console.WriteLine("SEND: {0}, {1}", bytes, textual content);
        }, state);
    }

    public void Obtain()
    {
        _socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
        {
            State so = (State)ar.AsyncState;
            int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
            _socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
            Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));
        }, state);
    }

int port = 27000;

UDPSocket c = new UDPSocket();

int new_port = 40156;
IPEndPoint bep = new IPEndPoint(IPAddress.Parse((MY_HOME_INTERNAL_IP)), new_port );
if( bep != null ){
    Console.WriteLine( "binding_to_ep" );
    c._socket.Bind( bep );
    c.Obtain();
}

c.Consumer((REMOTE_SERVER_EXTERNAL_IP), port );
c.Ship("TEST!");
Console.ReadKey();

right here is the server code:

public void Server(string deal with, int port)
        {
            _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            _socket.Bind(new IPEndPoint(IPAddress.Parse(deal with), port));
            Obtain();
        }


personal void Obtain()
        {
            _socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
            {
                State so = (State)ar.AsyncState;
                int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
                _socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
                Console.WriteLine("RECV: {0}: {1}, {2}", epFrom.ToString(), bytes, Encoding.ASCII.GetString(so.buffer, 0, bytes));

//that is the place I try and ship again to the shopper, however nothing get's acquired by the shopper it appears.
            byte() information = Encoding.ASCII.GetBytes("12");
            //EndPoint epFrom1 = new IPEndPoint(IPAddress.Parse( (MY_HOME_EXTERNAL_IP) ), 40156);
            EndPoint epFrom1 = new IPEndPoint(IPAddress.Parse((MY_HOME_EXTERNAL_IP)), 27000 );
            _socket.SendTo(information, epFrom1 );

        }, state);
    }

UDPSocket s = new UDPSocket();
            s.Server( (THE_SERVER_LOCAL_IP) , port );

Thus far I’ve acquired the message to the server, however within the server’s obtain code the place I attempt to name SendTo to ship it again to the shopper, nothing seems within the shopper’s obtain methodology. Beforehand I used to be getting a “connection refused” error message when making an attempt to ship to the shopper, however now I am not getting any message, which makes me suppose that possibly the info arrived, however now it does not. Does anybody have any recommendations? I’d respect it very a lot. I assumed this might be fairly easy and I would not must fiddle with firewall guidelines and such as a result of the connection is already clearly established, due to this fact my server ought to be capable to simply ship information to the shopper.

Additionally, in case anybody is questioning, the AWS server permits all outbound site visitors and prohibits incoming site visitors to port 27000, which is what’s required as specified within the code.

Additionally, a typical message I get from the shopper when the server receives the primary message is: RECV: (MY_HOME_EXTERNAL_IP_ADDRESS):(NUMBER_OF_BYTES): 5, TRY!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles