Multithread UDP listener in C#
-
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
-
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
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 {
-
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
Thanx a Ton!!!1