|
Hi,
I sometimes get the same behavior with VS2005 C# Express Edition, seems like a bug
in Visual; in my experience there are several ways that typically solve it:
- perform a clean build (Menu Item "Build/Rebuild Solution")
- delete the entire "bin" and "obj" directories in the project folder; first check
they contain output files only, your input files (source files, resources) should
not be there! Then rebuild.
- reboot and build again.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I am trying to mimic the operation of a temperature transmitter that streams data via UDP. So, currently I have a program that sends data every second. For some reason, in my receive program, I receive each packet twice. Once from the correct IP address and the wrong port, and secondly from 0.0.0.0 ip address and the right port (8000). Here is my code. Does anyone have any suggestions?
// defined as a member of the form
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void Form1_Load(object sender, System.EventArgs e)
{
EndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 8000);
s.Connect(ipep);
}
private void timer1_Tick(object sender, System.EventArgs e)
{
// ignore the string part for now
string strLine = (string)lstData[cnt++];
if (cnt == lstData.Count)
cnt = 0;
byte[] data = Encoding.ASCII.GetBytes(strLine);
s.Send(data);
}
Chase Davis
|
|
|
|
|
Chase Davis wrote: Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Chase Davis wrote: EndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 8000);
s.Connect(ipep);
Chase Davis wrote: s.Send(data);
I'm no expert with UDP but I think you are supposed to use Socket.SendTo(...)
Maybe this will help[^]
led mike
|
|
|
|
|
Actually, it works either way. With the Send function, I think you have to call the connect function. With the SendTo function, you don't have to connect, but you do have to set the SocketOption to Broadcast. Anyway, I tried using the SendTo function and I still see the same results. Thanks for the help though.
Chase Davis
|
|
|
|
|
Perhaps the problem is in the Receiving code which you have not posted?
led mike
|
|
|
|
|
Well, I thought about that. However, I used a program from the customer that monitors UDP data and the same thing happens. In any case, here is the receive code:
// local member of the form
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void btnStart_Click(object sender, System.EventArgs e)
{
// user defined class to contain socket information
StateObject state = new StateObject();
state.workSocket = s;
EndPoint ipep = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPortNum.Text, 10));
// only bind if it has not been bound yet
if (s.LocalEndPoint == null)
s.Bind(ipep);
s.BeginReceiveFrom(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, ref ipep, new AsyncCallback(OnReceive), state);
}
void OnReceive(IAsyncResult iar)
{
StateObject state = (StateObject)iar.AsyncState;
Socket client = state.workSocket;
EndPoint epRemote = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPortNum.Text, 10));
int bytesRead = client.EndReceiveFrom(iar, ref epRemote);
string str = Encoding.ASCII.GetString(state.buffer);
// do stuff with the string
s.BeginReceiveFrom(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, ref epRemote, new AsyncCallback(OnReceive), state);
}
Chase Davis
|
|
|
|
|
Chase, like I said I have had no experience with UDP so I just put together two apps, Sender and Client, they work as expected here is the code
Sender Application
public partial class Form1 : Form
{
private System.Net.Sockets.Socket _sock;
private System.Net.IPEndPoint _ipEnd;
public Form1()
{
InitializeComponent();
_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPHostEntry hostent = Dns.Resolve(Dns.GetHostName());
_ipEnd = new IPEndPoint(hostent.AddressList[0], 3030);
}
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = Encoding.ASCII.GetBytes(textBox1.Text);
Cursor oldCursor = Cursor;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
_sock.SendTo(bytes, _ipEnd);
Cursor = oldCursor;
}
}
Client application
public partial class Form1 : Form
{
UdpClient _client;
IPEndPoint _ipEndPoint;
public Form1()
{
InitializeComponent();
_ipEndPoint = new IPEndPoint(IPAddress.Any, 3030);
_client = new UdpClient(_ipEndPoint);
_client.BeginReceive(Recv, new State(_client, _ipEndPoint, this));
}
public void OnRecvMsg(string msg)
{
if( textBox1.Text.Length > 0)
textBox1.Text = String.Format("{0}\r\n{1}", textBox1.Text, msg);
else
textBox1.Text = msg;
}
public static void Recv(IAsyncResult ar)
{
State state = ar.AsyncState as State;
Byte[] bytes = state.Client.EndReceive(ar, ref state.EndPoint);
state.TForm.OnRecvMsg(Encoding.ASCII.GetString(bytes));
state.Client.BeginReceive(Recv, state);
}
}
internal class State
{
public IPEndPoint EndPoint;
public UdpClient Client;
public Form1 TForm;
internal State(UdpClient client, IPEndPoint endpoint, Form1 form)
{
EndPoint = endpoint;
Client = client;
TForm = form;
}
}
led mike
|
|
|
|
|
i want to build an application which can show all files sheared on my windows os
which property is peculiar to shared files within my system and how and from where can i access the properties of my windows files
|
|
|
|
|
Can you specify what means "shared files"?
led mike
|
|
|
|
|
files or folders on my system that i share to enable them to be accesible say over a network
|
|
|
|
|
|
Hi,
I am just testing some functions.
My question is how can I change the color of a LABEL, TAB texts on a form.
What I tried?
I created an xml file with the forecolor, backcolor info.
<color> <settings>
<label1>
<forecolor>> #FFFFFF
<backcolor> #FFFFFF
then I tried to link it.
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 13);
this.label1.TabIndex = 0;
// this.label1.Text = "Test123";
this.label1.Text = xDoc.SelectSingleNode("Language/Settings/Label1/Text").InnerText.Trim();
this.label1.ForeColor = xDoc1.SelectSingleNode("Color/Settings/Label1/ForeColor").InnerText.Trim();
this.label1.BackColor = xDoc1.SelectSingleNode("Color/Settings/Label1/BackColor").InnerText.Trim();
and under my main class i have:
public Form1()
{
InitializeComponent();
//Load XML
xDoc1 = new XmlDocument();
xDoc1.Load(Application.StartupPath + @"\Application Data\DummyText.xml");
}
but its doesnt work like this, Summary : I want to change the color of the TEXTs on my form from an xml document. i know i need to use system.drawing.color somewhere.
modified on Monday, February 11, 2008 1:18 PM
|
|
|
|
|
s3rro wrote: I want to change the color of the TEXTs on my form from an xml document. i know i need to use system.drawing.color somewhere.
So you want to create a Color object from a hexstring representation of the color, yes?
Please NOTE this is without error checking and it is NOT a design.
string scolor = "#6633aa";
int red = Convert.ToInt16(scolor.Substring(1, 2),16);
int green = Convert.ToInt16(scolor.Substring(3, 2),16);
int blue = Convert.ToInt16(scolor.Substring(5, 2),16);
label1.ForeColor = Color.FromArgb(red, green, blue);
led mike
|
|
|
|
|
See ColorTranslator.FromHtml.
e.g
this.label1.ForeColor = ColorTranslator.FromHtml(xDoc1.SelectSingleNode("Color/Settings/Label1/ForeColor").InnerText.Trim());
|
|
|
|
|
Sweet, had not seen that before.
led mike
|
|
|
|
|
thanks man thats what i was looking for.
|
|
|
|
|
Very nice. I'd forgotten about that - thanks for the reminder.
Definitely worth the 5 that's winging its way to you.
|
|
|
|
|
Hi guys
I have a function called SendEmail which runs within a thread.
This function simply send an email to the user.
However, I made a "Please Wait..." page while the email gets send in the background the user see the "Please Wait..." page.
<br />
<br />
public void SendEmail()<br />
{<br />
StringBuilder messageText = new StringBuilder();<br />
<br />
messageText.AppendLine(string.Format("Name: {0} {1}", txtName.Text.Trim().ToString(), txtSurname.Text.Trim().ToString()));<br />
messageText.AppendLine(string.Format("Contact Number: {0}", txtContactNumber.Text.Trim().ToString()));<br />
messageText.AppendLine(string.Format("Email Address: {0}", txtEmail.Text.Trim().ToString()));<br />
messageText.AppendLine(string.Format("Comments: {0}\n\n", txtComments.Text.Trim().ToString()));<br />
messageText.AppendLine(string.Format("Quote Reference Nr: {0}", Request.QueryString["quoteNr"].ToString()));
<br />
}<br />
I call the SendEmail() function using the following lines of code:
<br />
Guid id = Guid.NewGuid();<br />
<br />
ThreadStart ts = new ThreadStart(SendEmail);<br />
<br />
Thread th = new Thread(ts);<br />
<br />
th.Start();<br />
<br />
Response.Redirect(string.Format("PleaseWait.aspx?ID={0}&type={1}", id, "email"));<br />
Is it true that you can't access Request data within a thread, if not, how do I then get access to Request.QueryString data?
|
|
|
|
|
Ryno Burger wrote: if not, how do I then get access to Request.QueryString data?
Why are you creating threads if you don't know how to use them?
led mike
|
|
|
|
|
What do you mean?
I left some code out for simplicity and readability reasons...
May I ask, why do you reply if you don't want to offer help in the first place?
Isn't that like... well... wasting your own valuable time?
|
|
|
|
|
Ryno Burger wrote: What do you mean?
I mean you don't create threads in production software if you don't have strong experience developing multi-threaded software. You clearly don't have strong experience or you wouldn't have the problem you have.
By the way the solution to your problem is to eliminate the creation of the thread, it is serving no purpose.
led mike
|
|
|
|
|
This is obviously an ASP.NET question. Why not use the appropriate forum, you might get an answer faster.
only two letters away from being an asset
|
|
|
|
|
Call th.Start with the Start(object Parameter) overload; pass in your Request value.
|
|
|
|
|
Or just the particular querystring value would do
|
|
|
|
|
I thought we were here to help people, yes? So in this case we have helped this person create an overly complicated resource intensive implementation of sending an email, is that about it? Whatever happened to the KISS Principle[^] or Occam's razor?
led mike
|
|
|
|
|