|
i need some guidance regarding c# winsock programming, im a beginner in programming and network applications. i understand the client/server idea.
server -> socket() bind() listen() accept() {send() recv()} close()
client -> socket() connect() {send() recv()} close()
--
i also know Socket.Connected Property to test if connected.
--
where i am having difficulty is in both server and client how to implement send() and recv() [events]. i know how to send and receive but to do it while keeping the connection seems to very vague in google links.
most examples on google just creat the connection and may send some data or receive some data and the connection is closed.
the while(true) on server runs in a loop and accepts new connections.
i need to know how to implement send and recv (events) so the connected clients and server can keep on exchanging data using the same connection.
lots of the web examples have the server running in a loop and the client makes a new connection whenever data needs to be sent, it doesn't seem efficient to me.
i have also seen server loop running on its own thread to avoid gui lockups but there also the actual communication seems vague.
so. i would like info on how to handle send and recv in a structured manner.
thanks for reading and a very happy new year to you.
---------------------
my current approach is wrong, it concerns blocking.. i should be using .NET asynchronous socket programming.
thanks.
modified on Saturday, January 2, 2010 1:36 PM
|
|
|
|
|
You need another thread which accepts messages from clients and process it. Take a look at this[^] MSDN page.
New year wishes to you too.
Best wishes,
Navaneeth
|
|
|
|
|
.NET makes this stuff pretty straight forward.
Server side, use a TcpListener to accept incoming connections from the client. Once accepted, which gives you a TcpClient, the ususal practice is to create a new thread and pass it the TcpClient and let it do its server side stuff. If the connection is likely to be brief, use the ThreadPool.
On the client, just use a TcpClient to connect to the server.
Generally, the connection stays open until either end closes it, or it *may* get closed due to inactivity timeouts - not sure.
You can attach a NetworkStream to the TcpClient at both ends, so you just end up with a duplex stream you can read and write to.
Cassini is a little web server from the early days of .NET which allowed people to develop ASP.NET applications. Have a look at the source of that for some patterns. http://blogs.msdn.com/dmitryr/archive/2005/09/27/474534.aspx[^]
Regards,
Rob Philpott.
|
|
|
|
|
Hi all...
At runing the following function i get the exeption "Arithmetic operation resulted in an overflow", an exeption arises from the underlining line of code :
public static int OnesSum(byte[] bytes, int start, int len)
{
MemoryStream memStream = new MemoryStream(bytes, start, len);
BinaryReader br = new BinaryReader(memStream);
Int32 sum = 0;
UInt16 val;
while (memStream.Position < memStream.Length -1)
{
val = (UInt16)IPAddress.NetworkToHostOrde(br.ReadInt16());
sum += val;
}
// if we have a remaining byte we should add it
if (memStream.Position < len)
{
sum += br.ReadByte();
}
// fold the sum into 16 bits
while((sum >> 16) != 0)
{
sum = (sum & 0xffff) + (sum >> 16);
}
return sum;
}
|
|
|
|
|
Have you tried making val an Int32 (and performing the appropriate cast when that method returns it?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
The problem here is the cast to UInt16 which fails when the IPAddress byte order reversal method returns a negative value. Overflow checking must be disabled to allow the 16 bit result which is typed as Int16 to be successfully assigned to an unsigned type. Use the unchecked keyword to do this.
val = unchecked((UInt16)IPAddress.NetworkToHostOrder(br.ReadInt16()));
Alan.
|
|
|
|
|
Alan N wrote: val = unchecked((UInt16)IPAddress.NetworkToHostOrder(br.ReadInt16()));
That will always return 0.
The unchecked (UInt16) cast throws away the higher-order bits - but NetworkToHostOrder just swapped the byte order, so ALL the input bits will be thrown away.
I'm not sure what he wanted to do in the first place.
|
|
|
|
|
Not really, the overload with a short as input also has a short as result, so he's really just casting a short to an ushort
|
|
|
|
|
Oh, I didn't know there were multiple overloads. I thought it was for 32-bits only (as that's the size of an IPAddress), casting from int to ushort.
|
|
|
|
|
In that case you would have been right, of course
I suppose this overload can be useful for port numbers, I'm not sure why there is an overload for longs as well though..
|
|
|
|
|
I have written a User Control and made it dll from a classLibrary Project.
This User Control get the Name of the Form (in Project that we use this dll) in textbox1 and then do something with the controls on this form. Now my question is how i can Cast textbox1.Text (of this user control) to Form ?
|
|
|
|
|
Well, for one thing, you could pass in the form object into the user control itself.
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
This is rarely a good idea. If the user control needs acces to a Form object, it should have a Form property OR extend the Form class.
Also when the user control is dropped onto a form (or other container) in the designer, a parameterless constructor is used/needed.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
You can use the Parent property to access the container the user control is in - this may or may not be a form... it could be a groupbox, a panel etc, you can check by using is or as . If it's not a form, check the parent's parent etc etc...
This is normally the wrong way round of doing things though as you will effectively be coupling your user control to a form and if you want to manipulate other controls on the form then you will be coupling to those too. If your control is then used in a different container and/or the expected controls aren't present you will end up with problems. A better solution is to expose events, properties and methods in your user control that it's host form can access, then the host form can control all it's controls including your user control.
If you are wanting a 'controller' form with your user control acting as the controller - you would be better to create a custom form for this purpose rather than a user control IMO.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
see i use this user control in FormA and when the project runs it gets any form name execpt itself(the form that has this user control) in textbox1 (this textbox1 is also in the user control)and then the user control brings out all control on that form and show all properties of that control.
I have used this code in User Control:
Type type = Type.GetType(txtFormName.Text, false, true);
Form frm = Activator.CreateInstance(type) as Form;
I get this error :
Value cannot be null.
Parameter name: type.
Alert :
This user control has been made in another project and i have build DLL and now I use this DLL in my another projects.
|
|
|
|
|
Very good day all,
Is there a built in control & classes in C# allow me to draw a circle "dynamically"?
like the image in this link?
thank you!
|
|
|
|
|
Yes there is, in a windows form app you can:
private void Form1_Load(object sender, EventArgs e)
{
Pen pen = new Pen(Color.Black, 3);
Graphics gfx = this.CreateGraphics();
gfx.DrawEllipse(pen, 20, 20, 50, 50);
}
That should get you on the right track
|
|
|
|
|
I made a slight mistake in my last reply, you should actually add the code to the form "paint" event:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 3);
Graphics gfx = this.CreateGraphics();
gfx.DrawEllipse(pen, 20, 20, 50, 50);
}
as the form "load" event doesnt apply graphical effects it just initialises its base values.
|
|
|
|
|
Thanks , I have modfied that , it helps
now , the key thing how to create those node on to of them?
|
|
|
|
|
A couple of points...
1. Your pen should be disposed (or better still enclosed in a using block.)
2. Any graphics object you create yourself should also be disposed (gfx)
2b. Creating grapics is 'expensive', there is already a Graphics object in e.
3. Better to override the OnPaint method.
3b. You should call the base's method at a suitable location.
so the code would become...
protected override void OnPaint(PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Black, 3))
{
e.Graphics.DrawEllipse(pen, 20, 20, 50, 50);
}
base.OnPaint(e);
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
DaveyM69 wrote: You should call the base's method at a suitable location
Really? I never do that for Paint, and neither does this MSDN example[^].
So my questions are: why? what does it do for me? and if so, what is "a suitable location"?
|
|
|
|
|
Because otherwise (if overriding OnPaint rather than handling the Paint event) the Paint event will not get raised. I normally do it at the end of the method i.e. after all painting, however if there is some custom action that needs doing after all paint handlers have finished then base.OnPaint(e) should be called before then.
Edit: I've just followed the link a reread your post - In my point before I had suggested overriding OnPaint rather than handling the Paint event which is where the confusion comes from!
I prefer to override OnPaint if painting as I can be sure that no subscribers to the Paint event will be called until I've done with painting. If I paint in the Paint event handler then other subscribers may already have been notified yet my painting hasn't even begun!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
modified on Saturday, January 2, 2010 9:32 AM
|
|
|
|
|
hi......
i want to protect my application with licence key....in Xaml
can anyone help mee....
thanquuu...
|
|
|
|
|
Its pretty simple, make some random characters using a random number generator and specify how many you want?
Random ran = new Random();
string mySerial = string.Empty;
int serialLength = 12;
for (int i = 0; i < serialLength;i++)
{
int ascii = ran.Next(70,90);
char myChar = (char)ascii;
mySerial += myChar.ToString();
}
A bit like that, next time lookup the problem.
|
|
|
|
|
Why do you say you want it in XAML? XAML is a markup language - pure and simple.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|