0

Hello everyone I am very new to Visual Studio, C# programming, and Windows Form Applications.

My need is very simple - I want to create my own small program to listen to data being sent by a GPS device over UDP. I do not need to communicate, just listen and see the data on the screen!

Something that works exactly the same as this :

http://sockettest.sourceforge.net/ (see 'UDP' tab)

My GPS device has an IP of 192.168.1.1 and sends a sting of numbers every 1 second, continuously, transmitting on UDP 25.255.255.255:5017.

All the examples on the internet seems to focus on 2-way communicate, client and server chat windows etc. There is a lot of confusing terminology like synchronous and a-synchronous, client, server, UDP, TCP, binding.

I just want an even more simplified program than the above example, where I can type in the port number 5017, click Start Listening, and then straight away works!

All advice and code examples very gratefully received!!

Many thanks, Jon

Jon
  • 21
  • 1
  • 4
  • This might help you: https://stackoverflow.com/questions/746519/udpclient-receive-on-broadcast-address – DogeAmazed Sep 30 '17 at 06:14
  • See msdn samples. The samples uses socket but you can replace with any class that inherites sockets tcpclient, tcplistener, udpclient, udplistener : https://learn.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples – jdweng Sep 30 '17 at 07:01

2 Answers2

1

I now have it working, and can receive data in a textbox in the UI!

I use button_start_Click to open the port and start receiving. However, I cannot get button_stop_Click to work. How can you stop/close/disconnect/endReceive using button click?

    public Form1()
    {
        InitializeComponent();
    }

    private void button_start_Click(object sender, EventArgs e)
    {
        Client = new UdpClient(Convert.ToInt32(textBox_port.Text));
        Client.BeginReceive(DataReceived, null);
    }

    private void DataReceived(IAsyncResult ar)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, Convert.ToInt32(textBox_port.Text));
        byte[] data;
        try
        {
            data = Client.EndReceive(ar, ref ip);

            if (data.Length == 0)
                return; // No more to receive
            Client.BeginReceive(DataReceived, null);
        }
        catch (ObjectDisposedException)
        {
            return; // Connection closed
        }

        // Send the data to the UI thread
        this.BeginInvoke((Action<IPEndPoint, string>)DataReceivedUI, ip, Encoding.UTF8.GetString(data));
    }

    private void DataReceivedUI(IPEndPoint endPoint, string data)
    {
        txtLog.AppendText("[" + endPoint.ToString() + "] " + data + Environment.NewLine);
    }


    private void button_stop_Click(IAsyncResult ar) // NOT WORKING!! AGH!
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, Convert.ToInt32(textBox_port.Text));
        byte[] data;
        data = Client.EndReceive(ar, ref ip);
        Client.Close();

    }
Jon
  • 21
  • 1
  • 4
  • The format for Stack Overflow isn't the same as a forum where you just keep making posts. I think you'd be better off rolling this code into your question as an example, and then removing the answer. – YetAnotherRandomUser Oct 20 '17 at 19:25
0

Run the same code in background worker and then you can cancel the background worker anytime using backgroundworker.cancelasync(). Hope this helps.

Akhil
  • 1