|
By "socket.BeginAccept" did you mean socket.Receive()?
public void listensend()
{
while(keepalive)
{
byte[] buffer = new byte[1024];
int rcount = sourceSocket.Receive(buffer,buffer.Length,0) ;
...
If so, I really did placed it inside a loop. this method listensend is the thread that runs for the client (the "StartNewThreadAndDoSomething")
but wasn't this the right thing to do? I mean how does the server keep on listening for client messages when it is not in a loop?
thanks i appreciate the reply
|
|
|
|
|
It sounds as if your socket has got switched into non-blocking mode. This might have happened if the framework has called an asynchronous checking function (WSAEventSelect) on your behalf. I think this is pretty unlikely, though.
For high-performance servers, it's generally better to use asynchronous I/Os. This normally allows a small pool of threads to serve a large number of requests.
In particular reference to this code, I'd recommend creating the buffer before the while loop, not creating a new buffer on every iteration. You're likely to end up spending far longer in the garbage collector than necessary.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
thank you. I'll move the buffer creation.
I still don't have much of an understanding to blocking/non-blocking mode. If it is set to non-blocking mode... then will it surely consume 100% cpu usage?
|
|
|
|
|
Mike Dimmick wrote:
In particular reference to this code, I'd recommend creating the buffer before the while loop, not creating a new buffer on every iteration.
I have tried doing as you have suggested and here is the problems that I have encountered...
If I receive a 1st message of "ABCDEFGHI," everything works fine.
If I receive a 2nd message of "ABCDEFGHIJKLM," the buffer is still correct.
BUT if next i receive a shorter message, say "1234," then the current buffer will not be correct, having "1234EFGHIJKLM."
I just wanted to share this tidbit. ^^
|
|
|
|
|
The return value of Receive tells you how much data you received. Use it. Don't rely on the rest of the buffer being zeros.
When converting a byte array to a string, there is an overload of Encoding.GetString which takes an offset into the array and a length.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
socket.Receive works with a blocking socket, that means, it blocks the thread until readable data is available. Your loop is correct.
Do you have any calls to Application.DoEvents or Thread.Sleep ? These methods reduce CPU usage, so that other processes can run, even though your program is receivingg bytes.
public void listensend()
{
while(keepalive)
{
byte[] buffer = new byte[1024];
int rcount = sourceSocket.Receive(buffer,buffer.Length,0) ;
Thread.Sleep(100); //hey, you're not the only process on this machine!
...
_________________________________
Vote '1' if you're too lazy for a discussion
|
|
|
|
|
Application.DoEvents???
Hmm... what I'm doing for the server is accept messages from clients. So the buffer in the code will be changed into a string, and then I will parse the string to know what kind of message did the client sent.
Based on the message I will do necessary functions like looking for another client, or getting necessary information, and then the final action would be this server will send string messages.
so in the code I have a series of if statements
if(command == ....)
{
...
...
clientSocket.Send()
}
if(command == ....)
{
...
...
clientSocket.Send()
}
I will try the Thread.Sleep now..
|
|
|
|
|
I have tried the thread.sleep and it does tone down the cpu usage.
I was just wondering whether using sleep can lead to a lesser performance, like when the client is expecting faster continuous replies from the server.
I imagined the thread will sleep on its every run, so it cannot give continuous messages.
Is there any other way around this issue?
|
|
|
|
|
i need to read a .txt and .doc file, then get few random sentences from it, anyone know how can i do that ?? i'm really headache about it ~ ~
|
|
|
|
|
Sentences start typically with a space and end with a period, or a couple other punctuations...So tear it apart using a StringBuilder, then use a Random number seed to go through the StringBuilder array of sentences..and wallah!! Should not be to hard...
"Your KungFu is not strong enough"
|
|
|
|
|
This may sound simple yet I keep running into this problem and re-implementing solutions, heres the deal, this example is over simplified for discussion reasons...
Lets say you design a program with a Listview (or some other non databound control), each item in the control can have up to 1-10 different functions, lets say 400 items in the control. So each item can only have one function but have the possability of up to 10 different ones.
Ok so you need a way to manage this data in the background while the user works with the list in interface, Arrays? Hashtables? Collections? DataSets? Well my current pick of the litter is a DataTable, I use two columns one for "Items" and one for "Function" then whenever the user changes the list I clear it completely and reiterate through the DataTable using a for each and repopulate it. This seems very very clunky and sloppy, not only do I have to do something like Class.Add(listview,item,function) or I set the listview item earlier on during loadup to a ListView declaration inside my class and I manipulate it from my class, I have never been a big fan of handing off controls to a class and letting the class manipulate the control through reference...Just doesnt seem very clean...
Does anyone have any thoughts on this?
Thanks, Eric
|
|
|
|
|
Hello,
I'm trying to find all of the concrete classes in an assembly that derive from an abstract base class that I've defined. I'm using the following code to create a StringCollection of the class names. The collection being returned has no items inside of it when I pass it the current assembly and the my custom type. However, I can pass it another assembly along with another abstract type (such as CollectionBase) and it returns the correct number of items. Does anyone see anything obvious that I screwed up? I believe I've been looking at this code too long to be able to spot an error.
Thanks,
Will
public static StringCollection GetConcreteDerivedClassList(System.Reflection.Assembly asm,
Type typ)
{
StringCollection ret = new StringCollection();
foreach(Type t in asm.GetTypes())
{
if(QAUtility.ClassIsConcreteDerivative(typ, t))
{
ret.Add(t.Name);
}
}
return ret;
}
public static bool ClassIsConcreteDerivative(Type tBase, Type tDerived)
{
bool ret = false;
if(tDerived.IsSubclassOf(tBase) & !tDerived.IsAbstract)
{
ret = true;
}
return ret;
}
|
|
|
|
|
Are your classes in the passed assembly public?
Regards
Senthil
|
|
|
|
|
They are all public. Even so, shouldn't Assembly.GetTypes() return all types in the assembly?
Will
|
|
|
|
|
Correction, Assembly.GetTypes() returns all the types in the assembly, PROVIDED that you pass the assembly you actually meant to pass. I, however, did not. I think I should probably get some sleep before I try any more coding. The four hours a night for the last three nights was apparently insufficient.
Thanks for the help.
Will
|
|
|
|
|
How could I fill a Ellipse with one color, making the center most intensive, and the intensity fades as the distance from the center increase.
I tried to use LinearGradientBrush, but could not find a mode for ellipse.
|
|
|
|
|
Hello fellow programming, what you are seeking comes with good fortune and happy tidings..LOL!!!
You want whats caleld a PathGradientBrush which is derived from GraphicsPen...Lemme type up some code for you, I will not have tested this but you will get the idea...
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics
GraphicPath gp = new GraphicPath();
gp.AddLine(10,10,110,15);
gp.AddLine(110,15,100,96);
gp.AddLine(100,96,15,110);
gp.CloseFigure(); //its a box duh!!
g.FillRectangle(Brushes.White, this.ClientRectangle); //paint the form white...
g.SmoothingMode = SmoothingMode.AntiAlias; //otherwise it looks like crapola
PathGradientBrush pgb = new PathGradientBrush(gp);
pgb.CenterColor = Color.White; //the middle "cone"
pgb.SurroundColors = new Color[]{Color.Blue,Color.Red}; //Surround Colors, pass in an array of colors...
g.FillPath(pgb, gp);
g.DrawPath(Pens.Black, gp); //draw the stupid square...its not perfectly square prolly
pgb.Dispose();
gp.Dispose();
}
|
|
|
|
|
|
The error is: 'Class1.frmAccount.txtOwnerName' is inaccessible due to its protection level.
<br />
public void ShowAddAccount(string Name)<br />
{<br />
frmAccount frmA = new Class1.frmAccount();<br />
frmA.txtOwnerName.text = Name;
frmA.ShowDialog();<br />
}<br />
When I change "private System.Windows.Forms.TextBox txtOwnerName;" to public in frmAccount then it says "'System.Windows.Forms.Control.text' is inaccessible due to its protection level"
I know this is a logical mistake but I still need help. Thanks in advance.
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
text with lower case is an internal property. Try Text. Then make it a property.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Ooops.. Trouble for VB programmers huh?
Thanks Christian
Radgar
"Imagination is more important than knowledge." - Albert Einstein
|
|
|
|
|
hi there,
I have a Solution with two projects. Both projects have a main method to open its own.
- projectA - formA(with a main() method) - buttonA
Solution
- projectB - formB(with a main() method) - buttonB
I want to open formB in projectB by clicking buttonA.
private void buttonA_click(){
projectB.formB fb = new projectB.formB();
fb.Show(); // open projectB
this.Visible = false; // close projectA
this.Dispose();
}
But," this.Visible = false; and this.Dispose();" statements cause both projects closed.
1) how can Open a Project, then close another one by not using " this.Visible = false; and this.Dispose();" ??
Thansks
|
|
|
|
|
luckyShek wrote:
this.Visible = false; // close projectA
This hides child windows as well, such as the one you created.
luckyShek wrote:
this.Dispose();
Oh, someone shoot me. projectB exists as an instance inside projectA. What did you expect this would do ?
You probably want to use ShellExecute to just run the other program, then close this one.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
hi there,
I had created a UserControl.exe. I want to add it to a project by using "Add References". But it only take .dll file.
1) Can I just rename UserControl.exe to UserControl.dll ? 2) Will it cause any error ??
3) What's the best to create a userControl file with .dll in vs.net ?
Thank you very much.
|
|
|
|
|
luckyShek wrote:
What's the best to create a userControl file with .dll in vs.net ?
Set the compiler to build a dll.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|