سؤال

Okay, I want to send a UDP packet to an IP, and then receive it. As I can't get it to work, I have to ask for assistance.

I want it to work on 1 app, meaning client and server is on the same application. I currently got it working with Audio sending and receiving on the same app (using sample codes and stuff).

And the thing I can´t understand is just the UDP, and probably Sockets.

It seems simple, but I can´t get it to work.

So if I can get a very simple sample, like:

"UDP Send "hello" to IPofchoice"

"UDP receive "data" from IPofchoice"

Messagebox.show("decoded(data));

Well hope you get what I mean.

EDIT:

Here is my "example" which doesn´t work at all.

void VoiceChat_KeyPress(object sender, KeyPressEventArgs e)

void VoiceChat_KeyPress(object sender, KeyPressEventArgs e)
    {
        string text = "Hello";
        byte[] send_buffer = Encoding.ASCII.GetBytes(text);
        otherPartyIP = new IPEndPoint(IPAddress.Parse(txtCallToIP.Text), 1450);
        udpClientKey.Send(send_buffer, send_buffer.Length, "127.0.0.1", 1450);

        byte[] byteData = udpClientKey.Receive(ref otherPartyIP);
        MessageBox.Show(otherPartyIP.Address.ToString());

        MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteData));
    }

Where txtCallToIP.tex = the ip i write, which is 127.0.0.1 currently.

هل كانت مفيدة؟

المحلول

This i solved.

To have a client and server in UDP it´s very easy, though a bit dangerous, as you don´t know for sure if it´s working or not unless you test it (UDP just send and don´t care about anything else).

So basically to do it.

Client.Send(buffer,0,buffer.size,IPAdress,Port); byte[] rec = Client.Receive(ref IPAdress);

To receive you must have bind it to an IP, so you can just bind it to listen to Any ipadress, The port however needs to be set (i think), and you want to do that, as listening to everything there is, is not a good idea.

So ref IPAdress will be that listening IP and Port.

So it´s very easy to set up. And you can use the same UDPClient for both receiving and sending if you want, though it´s probably better to have 2 separate .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top