|
Hi
I'm trying to measure data from a multicast udp address.
To do this, i use a socket added to multicastgroup and this code:
while (!b)
{
byte[] buffer = new byte[udpReceiver.Available];
udpReceiver.Receive(buffer, 0, buffer.Length, SocketFlags.None, out err);
if ((DateTime.Now - starttime).TotalSeconds < ((double)elem.timetowatch_ms / 1000))
{
newvalue += (ulong)buffer.Length;
}
else
{
newvalue += (ulong)buffer.Length;
receivedByteNumber = ((Convert.ToUInt64((double)newvalue / (DateTime.Now - starttime).TotalSeconds)) / 128);
newvalue = 0;
starttime = DateTime.Now;
b = true;
}
}
my problem that it is using about 50% of cpu time and if i place it in a thread
it shows less data (~4000kbits/sec) than if it is placed on a main thread (~7000) - possibly because of system time management, but i'm unsure.
Any better suggestion on data measuring on multicast udp with less cpu and regardless of this threadproblem?
Thanks in advance:
Collapo
modified on Thursday, September 17, 2009 7:14 AM
|
|
|
|
|
I see you have a dual-core system.
The code you created is basically a polling loop: you are repeating the same thing over and over, without waiting for anything, so the core that executes it will spin around as fast as it can: assume Available is zero, Receive sees zero-length buffer, returns immediately, timespan is very small, loop iteration done, on to the next loop wasting even more cycles.
You need to fix this in one of several ways:
- include a Thread.Sleep(x) in your code, where x>0; say x=33; that way your code will run at most 30 times per second, and your CPU usage will go way down. However you now may miss some packets.
- use a fixed-length buffer, say 2048B; that way Receive will have to wait for data; drawback is it will not report smaller packets until the buffer gets full, so there is a lag.
- use asynchronous I/O, look for BeginReceive. IMO that would be the correct way.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks Luc!
Yeapp, it seem reasonable...
- Thread.Sleep(x) is loosing too much packets, so it won't be good for me i guess
- fixed buffer might be good, I'll try it in a short time
- I'll try BeginReceive as well, though i am not familiar with callbacks, so i would be really thankful if you could enlighten me on that
collapo
|
|
|
|
|
collapo wrote: enlighten me
read the documentation, it probably holds an example.
and learn to use Google, it knows more than 6 million CPians combined.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
|
i'm working C#
I don,t know API very well
please help me how can i learn API in C#?
Also, i,m very thankful if introduce me some good book about API
|
|
|
|
|
There are multiple tutorials and introductions to .NET and C# online. Google the .NET area you want to learn about..
Regarding books, I find "Programming C#" by Jesse Liberty from O'Reilly to be pretty good.
|
|
|
|
|
Try Google and MSDN.
|
|
|
|
|
LIQUID NITROGEN
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Hi.
I've used a WebBrowser to print a HTML file , I want to omit footer[^] and header[^] of the Print result.
Could you guide me how I can do it ?
|
|
|
|
|
Construct a version of the page with these elements and use it for the print
only two letters away from being an asset
|
|
|
|
|
Thanks, I couldn't understand well, could you please describe it more ?
|
|
|
|
|
What don't you understand about having a version of the page you are rendering without the header and footer elements?
Alternatively, use the CSS media type to control what is available for printing
http://www.w3schools.com/css/css_mediatypes.asp[^]
only two letters away from being an asset
|
|
|
|
|
Hey guys
I'm having some troubles communicating to an external device over the network.
Here's my code
TcpClient BadgerConnection = new TcpClient();
BadgerConnection.Connect(IPAddress.Parse("10.0.0.100"), 1111);
if (BadgerConnection.Connected)
{
StreamWriter sw = new StreamWriter(BadgerConnection.GetStream());
sw.AutoFlush = true;
char[] Buffer = new char[]
{
(char)161,
(char)15,
(char)89,
(char)1,
(char)89,
(char)3,
(char)102
};
sw.Write(Buffer);
}
So obviously this isn't working, cause if it did I wouldn't be here
What I did was get a packet sniffer and attached it to my program before connecting to the external device.
Here was the result in hex:
C2 A1 0F 59 01 59 03 66
Here's what I intended to send in hex:
A1 0F 59 01 59 03 66
There's an extra character showing up... C2 or rather 194.
Can someone please explain to me why and how can I stop it from sending the extra character?
Thanks
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Odd, I would have expected an UTF-8 BOM, but C2.. that's something else..
Anyway, why are you using chars at all? Why not just bytes?
If you send them as bytes, that should probably get rid of that C2 (at least I never noticed extraneous C2's while sending plain bytes)
|
|
|
|
|
I tried sending just bytes bytes like so
TcpClient BadgerConnection = new TcpClient();
BadgerConnection.Connect(IPAddress.Parse("10.0.0.100"), 1111);
if (BadgerConnection.Connected)
{
StreamWriter sw = new StreamWriter(BadgerConnection.GetStream());
sw.AutoFlush = true;
byte[] Buffer = new byte[]
{
161,
15,
89,
1,
89,
3,
102
};
sw.Write(Buffer);
}
And the sniffer picked up this:
00000000: (TCP/IP) Sending Packet, 17.09.2009 15:24:37.684
BadgerTest.vshost.exe (2188)
0.0.0.0:1200 ® 10.0.0.100:1111
Size: 13 bytes
53 79 73 74 65 6D 2E 42 79 74 65 5B 5D System.Byte[]
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
It seems to be the StreamWriter 's fault, what if you send them "normally"?
eg
TcpClient BadgerConnection = new TcpClient();
BadgerConnection.Connect(IPAddress.Parse("10.0.0.100"), 1111);
if (BadgerConnection.Connected)
{
byte[] Buffer = new byte[]
{
161,
15,
89,
1,
89,
3,
102
};
NetworkStream ns = BadgerConnection.GetStream();
ns.Write(Buffer, 0, Buffer.Length);
ns.Flush();
}
|
|
|
|
|
That worked like a charm... BUT only the first time
If I try to send that same set of bytes a second time i get an exception
Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
Any ideas?
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Well, if it were making sense it might mean that the other side did a Close on their Socket
But with networks, sense rarely comes into play
What kind of thing is the remote host? Does it close its socket after any command?
|
|
|
|
|
harold aptroot wrote: What kind of thing is the remote host?
Its a proximity card reader
harold aptroot wrote: Does it close its socket after any command?
Nope, it doesn't
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
Weird, what happens if you try to open a new one anyway?
|
|
|
|
|
Hi people
I'm building a desktop application and I want to include a Hyperlink somewhere on my form. This link should not link to a website though, it should instead call a method. Is this possible?
|
|
|
|
|
|
Use the Click event of a label, linkLabel or any control you think is appropriate.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
hi all
i have two project and they are almost the same each other
in one of them i have obj\release file
but in another i dont have
i want to have it in another one also
so how i can make it ??
i will be happy if u help me
|
|
|
|
|