Click here to Skip to main content
15,891,423 members
Home / Discussions / C#
   

C#

 
AnswerRe: Get a list of System Font Sizes Pin
OriginalGriff9-Sep-09 21:40
mveOriginalGriff9-Sep-09 21:40 
GeneralRe: Get a list of System Font Sizes Pin
hardsoft10-Sep-09 0:51
hardsoft10-Sep-09 0:51 
GeneralRe: Get a list of System Font Sizes Pin
xirisjohn30-May-11 6:17
xirisjohn30-May-11 6:17 
AnswerRe: Get a list of System Font Sizes Pin
carlecomm22-Sep-09 16:06
carlecomm22-Sep-09 16:06 
QuestionPrint with two printers ? Pin
Mohammad Dayyan9-Sep-09 11:36
Mohammad Dayyan9-Sep-09 11:36 
AnswerRe: Print with two printers ? Pin
Christian Graus9-Sep-09 11:40
protectorChristian Graus9-Sep-09 11:40 
AnswerRe: Print with two printers ? Pin
Zoki Manas9-Sep-09 21:00
Zoki Manas9-Sep-09 21:00 
QuestionMultithread UDP listener in C# Pin
Christian_V_V9-Sep-09 11:35
Christian_V_V9-Sep-09 11:35 
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.richTextBox1.Text += text;
this.richTextBox1.Text += Environment.NewLine;
this.richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
//this.richTextBox1.ScrollToCaret();

if (text != null)
{
string[] geoValues = text.Split(new char[] { ',' });
if (geoValues.Length > 0 && geoValues.Length == 36)
{
string GeoResponse = geoValues[0].Substring(1, 4);
if (GeoResponse == "unit")
{
string unit = geoValues[0].Substring(6);
string reason = geoValues[7].Substring(7);
string eventid = geoValues[8].Substring(8);
string longitude = geoValues[11].Substring(10);
string latitude = geoValues[12].Substring(9);
string gps_valid = geoValues[14].Substring(10);
string gps_connected = geoValues[15].Substring(14);
string velocity = geoValues[17].Substring(9);
string emergency = geoValues[19].Substring(10);
string door = geoValues[21].Substring(5);
string datetime_actual = geoValues[34].Substring(16);
DateTime UTC = Convert.ToDateTime(datetime_actual);
DateTime LOCAL = UTC.ToLocalTime();
string datetime_actual1 = LOCAL.ToString("yyyy/MM/dd HH:mm:ss");
if (eventid != "0" && reason == "4")
{
conexion con = new conexion();
con.InsertRow(unit, reason, eventid, longitude, latitude, gps_valid, gps_connected, velocity, emergency, door, datetime_actual1);
con.closingcon();
}
}
}
}
}


private void WaitForData()
{
IPEndPoint localIP = new IPEndPoint(IPAddress.Parse(this.textBox2.Text), Convert.ToInt32(this.textBox1.Text));
EndPoint epSender = (EndPoint)localIP;
soc.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}

private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
int iRx = 0 ;
iRx = soc.EndReceiveFrom(ar,ref epSender);
char[] chars = new char[iRx+1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buf, 0, iRx, chars, 0);
szData = Encoding.Default.GetString(buf, 0, buf.Length);
string text = szData;
if (this.richTextBox1.InvokeRequired)
{
SetTextCallback f = new SetTextCallback(SetText);
this.Invoke(f, new object[] { text });
}

WaitForData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void udp_Load(object sender, EventArgs e)
{
String strHostName = "";
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostByName(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
this.textBox2.Text = addr[i].ToString();
}

}

private void button3_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
}

}
}
AnswerRe: Multithread UDP listener in C# Pin
carlecomm22-Sep-09 15:51
carlecomm22-Sep-09 15:51 
AnswerRe: Multithread UDP listener in C# Pin
Prajakta Bhagwat7-Dec-11 19:23
Prajakta Bhagwat7-Dec-11 19:23 
QuestionWebBrowser ? Pin
Mohammad Dayyan9-Sep-09 9:34
Mohammad Dayyan9-Sep-09 9:34 
AnswerRe: WebBrowser ? Pin
Christian Graus9-Sep-09 9:38
protectorChristian Graus9-Sep-09 9:38 
GeneralRe: WebBrowser ? Pin
Mohammad Dayyan9-Sep-09 9:40
Mohammad Dayyan9-Sep-09 9:40 
GeneralRe: WebBrowser ? Pin
OriginalGriff9-Sep-09 9:48
mveOriginalGriff9-Sep-09 9:48 
GeneralRe: WebBrowser ? Pin
Mohammad Dayyan9-Sep-09 10:11
Mohammad Dayyan9-Sep-09 10:11 
AnswerRe: WebBrowser ? Pin
carlecomm22-Sep-09 16:14
carlecomm22-Sep-09 16:14 
QuestionTry-Catch problem in C# console app Pin
Member 46458019-Sep-09 9:05
Member 46458019-Sep-09 9:05 
AnswerRe: Try-Catch problem in C# console app Pin
Christian Graus9-Sep-09 9:15
protectorChristian Graus9-Sep-09 9:15 
AnswerRe: Try-Catch problem in C# console app Pin
harold aptroot9-Sep-09 9:19
harold aptroot9-Sep-09 9:19 
GeneralRe: Try-Catch problem in C# console app Pin
Member 46458019-Sep-09 9:27
Member 46458019-Sep-09 9:27 
GeneralRe: Try-Catch problem in C# console app Pin
harold aptroot9-Sep-09 9:28
harold aptroot9-Sep-09 9:28 
GeneralRe: Try-Catch problem in C# console app Pin
Christian Graus9-Sep-09 9:39
protectorChristian Graus9-Sep-09 9:39 
AnswerRe: Try-Catch problem in C# console app Pin
Zoki Manas9-Sep-09 20:46
Zoki Manas9-Sep-09 20:46 
AnswerRe: Try-Catch problem in C# console app Pin
carlecomm22-Sep-09 16:00
carlecomm22-Sep-09 16:00 
QuestionPowerpoint using c# Pin
Vijay Mudunuri9-Sep-09 7:59
Vijay Mudunuri9-Sep-09 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.