Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multithread UDP listener in C#

Multithread UDP listener in C#

Scheduled Pinned Locked Moved C#
csharpdatabasemysqllinqgraphics
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian_V_V
    wrote on last edited by
    #1

    Hello to every body. My name is Christian and i´m not a c# developer (I´m a VFP developer) but some how i manage to built a little UDP server listener app in c#. I needed to do this becouse I will have several vehicles that send a data via GPRS to the server each minute and the server should be listening and decode the data to store in the database (MySql). There will be hundreds of entries per minute. I Hope that somebady can tell me if my code is multithread or not and if it´s not, how can i implement it. Also, if i need multithread in the database part. The code is this: using System; using System.IO; using System.Net.Sockets; using System.Net; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Data.Odbc; using System.Data.OleDb; using System.Threading; namespace httprequest { public partial class udp : Form { private delegate void SetTextCallback(string text); private delegate void SetValCallback(int val); public udp() { InitializeComponent(); } int i; Socket soc; const int bufsize = 1024; byte[] buf = new byte[bufsize]; string szData; private void button1_Click(object sender, EventArgs e) { try { i = 0; IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(this.textBox2.Text),Convert.ToInt32(this.textBox1.Text)); EndPoint epSender = (EndPoint)localIP; soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); soc.Bind(localIP); soc.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); this.button1.Enabled = false; this.richTextBox1.Text = "Waiting for DataPacket..."; } catch (Exception ex) { MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void SetText(string text) { if(i == 0) { this.richTextBox1.Clear(); i = 1; } this.rich

    C P 2 Replies Last reply
    0
    • C Christian_V_V

      Hello to every body. My name is Christian and i´m not a c# developer (I´m a VFP developer) but some how i manage to built a little UDP server listener app in c#. I needed to do this becouse I will have several vehicles that send a data via GPRS to the server each minute and the server should be listening and decode the data to store in the database (MySql). There will be hundreds of entries per minute. I Hope that somebady can tell me if my code is multithread or not and if it´s not, how can i implement it. Also, if i need multithread in the database part. The code is this: using System; using System.IO; using System.Net.Sockets; using System.Net; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Data.Odbc; using System.Data.OleDb; using System.Threading; namespace httprequest { public partial class udp : Form { private delegate void SetTextCallback(string text); private delegate void SetValCallback(int val); public udp() { InitializeComponent(); } int i; Socket soc; const int bufsize = 1024; byte[] buf = new byte[bufsize]; string szData; private void button1_Click(object sender, EventArgs e) { try { i = 0; IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(this.textBox2.Text),Convert.ToInt32(this.textBox1.Text)); EndPoint epSender = (EndPoint)localIP; soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); soc.Bind(localIP); soc.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); this.button1.Enabled = false; this.richTextBox1.Text = "Waiting for DataPacket..."; } catch (Exception ex) { MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void SetText(string text) { if(i == 0) { this.richTextBox1.Clear(); i = 1; } this.rich

      C Offline
      C Offline
      carlecomm
      wrote on last edited by
      #2

      this is codes from web: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace BroadcastExample { public partial class Form1 : Form { delegate void AppendStringCallback(string text); AppendStringCallback appendstringcallback; //Port 51008 /// <summary> /// /// </summary> private int port = 51008; /// <summary> /// /// </summary> private UdpClient udpclient; public Form1() { InitializeComponent(); appendstringcallback = new AppendStringCallback(AppendString); } /// <summary> /// /// </summary> /// <param name="text"></param> private void AppendString(string text) { if (richtextBox2.InvokeRequired == true) { this.Invoke(appendstringcallback, text); } else { richtextBox2.AppendText(text + "\r\n"); } } /// <summary> /// /// </summary> private void RecData() { udpclient = new UdpClient(port); IPEndPoint remote = null; while (true) { try { byte[] bytes = udpclient.Receive(ref remote); string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length); AppendString(string.Format("From{0}:{1}", remote, str)); } catch { break; } } } private void Form1_Load(object sender, EventArgs e) { Thread mythread = new Thread(new ThreadStart(RecData)); mythread.IsBackground = true; mythread.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { udpclient.Close(); } private void button1_Click(object sender, EventArgs e) { UdpClient myUdpclient = new UdpClient(); try {

      1 Reply Last reply
      0
      • C Christian_V_V

        Hello to every body. My name is Christian and i´m not a c# developer (I´m a VFP developer) but some how i manage to built a little UDP server listener app in c#. I needed to do this becouse I will have several vehicles that send a data via GPRS to the server each minute and the server should be listening and decode the data to store in the database (MySql). There will be hundreds of entries per minute. I Hope that somebady can tell me if my code is multithread or not and if it´s not, how can i implement it. Also, if i need multithread in the database part. The code is this: using System; using System.IO; using System.Net.Sockets; using System.Net; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Data.Odbc; using System.Data.OleDb; using System.Threading; namespace httprequest { public partial class udp : Form { private delegate void SetTextCallback(string text); private delegate void SetValCallback(int val); public udp() { InitializeComponent(); } int i; Socket soc; const int bufsize = 1024; byte[] buf = new byte[bufsize]; string szData; private void button1_Click(object sender, EventArgs e) { try { i = 0; IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(this.textBox2.Text),Convert.ToInt32(this.textBox1.Text)); EndPoint epSender = (EndPoint)localIP; soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); soc.Bind(localIP); soc.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); this.button1.Enabled = false; this.richTextBox1.Text = "Waiting for DataPacket..."; } catch (Exception ex) { MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void SetText(string text) { if(i == 0) { this.richTextBox1.Clear(); i = 1; } this.rich

        P Offline
        P Offline
        Prajakta Bhagwat
        wrote on last edited by
        #3

        Thanx a Ton!!!1

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups