5.5 C
New York
Saturday, March 15, 2025

c# – How can I get a response again from my server working a udp socket between my server and residential?


I’ve a c# udp socket I am attempting to ship information from to my dwelling. The server socket is hosted at aws, and the consumer is at my home. I modified the code from the next GitHub to ship a response again to the consumer after the server receives the primary message despatched by the consumer: https://gist.github.com/darkguy2008/413a6fea3a5b4e67e5e0d96f750088a9.

Right here is the consumer code:

    public void Shopper(string handle, int port)
    {
        _socket.Join(IPAddress.Parse(handle), 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.Shopper([REMOTE_SERVER_EXTERNAL_IP], port );
c.Ship("TEST!");
Console.ReadKey();

right here is the server code:

public void Server(string handle, int port)
        {
            _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            _socket.Bind(new IPEndPoint(IPAddress.Parse(handle), 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 consumer, however nothing get's acquired by the consumer 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 );

up to now I’ve gotten the message by means of to the server, however within the server obtain code the place I try to name SendTo to ship again to the consumer, nothing seems within the consumer obtain technique. beforehand I used to be receiving a “connection refused” error message when attempting to ship to the consumer, however now I do not obtain any message, which makes me suppose that maybe the information bought by means of, however I do not now. anybody have any ideas. it would be tremendously appreciated. I figured this could be fairly straight ahead and I would not need to fiddle with firewall guidelines and stuff as a result of the connection is already/clearly established, subsequently my server ought to simply be capable of ship information again to the consumer.

additionally, in case anybody is questioning, the aws server permits all output visitors and prohibits inbound visitors to port 27000, which is what is critical as specified by the code.

additionally, a typical message I get from the consumer when that first message is acquired by the server is: RECV: [MY_HOME_EXTERNAL_IP_ADDRESS]:[NUMBER_OF_BYTES]: 5, TEST!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles