473,508 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sockets and Buffer size

Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that a
special char (# for example) will end the stream, but i'm not sure of this,
and don't know how to do this ?!

Thanks for any help !

Mar 8 '06 #1
11 2372
"Steven" <no**@none.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that a
special char (# for example) will end the stream, but i'm not sure of
this, and don't know how to do this ?!

Thanks for any help !


The size of the buffer is always fixed because the full message must always
be assembled from parts. This is because the sender and the network can and
will chop the message into pieces - You might send it as 100 bytes but
receive it as 10,30 and 60 [This is an example only - in real life the
chunks are much bigger]. This is because TCP is a stream protocol not a
datagram protocol such as UDP (which wont work with huge messages for the
same reasons).

Because of the above it is almost always better to send the length first in
a fixed size format rather than use a terminator.
Mar 8 '06 #2
PH
Hi Steven;

You sould to use

Sockets.Bind(YourLocalEndPoint)
BeginRereive()
EndReceive()

EndReceive() returns the length of the packet.
Let's say your buffer is 2048 bytes, but the size of your packet is 230.
Then EndReceive() returns 230, now you might copy only the first 230
bytes from the buffer to a local array and process it for example.

I'm still learning this because I'm implementing it right now.
Good luck.
-----Original Message-----
From: Steven [mailto:no**@none.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Sockets and Buffer size
Subject: Sockets and Buffer size

Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with
the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that
a
special char (# for example) will end the stream, but i'm not sure of
this,
and don't know how to do this ?!

Thanks for any help !

Mar 8 '06 #3
PH


Take a look at
http://msdn2.microsoft.com/en-us/lib...f5(VS.80).aspx

If you are using a connectionless protocol such as UDP, you do not need
to listen for connections at all. Call the ReceiveFrom method to accept
any incoming datagrams. Use the SendTo method to send datagrams to a
remote host.

Good luck

-----Original Message-----
From: Steven [mailto:no**@none.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Sockets and Buffer size
Subject: Sockets and Buffer size

Hi,

I need to write an application using sockets.

I have a server and about 10 clients "speaking" at the same time with
the

server, so i guess i need to use asynchronous sockets.

But the server will receive from the clients an ascii string with a

viariable length.

And in all the example i found, the size of the buffer is always fixed

(private byte[] theBuffer = new byte[1024]).

I probably misunderstood the use of the buffer !

I thought i could choose the largest size possible, and then decide that
a

special char (# for example) will end the stream, but i'm not sure of
this,

and don't know how to do this ?!

Thanks for any help !




Mar 8 '06 #4
Steven wrote:
Hi,

I need to write an application using sockets.
I have a server and about 10 clients "speaking" at the same time with the
server, so i guess i need to use asynchronous sockets.
But the server will receive from the clients an ascii string with a
viariable length.
And in all the example i found, the size of the buffer is always fixed
(private byte[] theBuffer = new byte[1024]).
I probably misunderstood the use of the buffer !
I thought i could choose the largest size possible, and then decide that a
special char (# for example) will end the stream, but i'm not sure of this,
and don't know how to do this ?!

Thanks for any help !



You can read the message firstly and then assemble the message, for
example,

you have

private byte[] theBuffer = new byte[1024], if you don't find "#" in this
message, then you should put it in a temporary buffer, and append the
next read to it.

But you should know the maximum length of your message, if for
example,you made 10 reads, and with a message length of 10240 , and
still don't find the "#". And you know the maximum length of your
message is smaller than this, you should handle this as an "attacker"
situation and do some proper cleanup and termination of this.

Another way is to append the message length at the beginning of the
message, that way, you always know the size of message.

John.

Mar 8 '06 #5
For 10 clients, I would not even use async but a thread per client server
design (sync server). Tons easier and perf would be the ~same as async. As
far as buffer size. If you using tcp, then send len prepended messages.
Then you can read the len and read the data bytes until you got N bytes.
Pretty easy and flexible and your messages can be almost any size so your
not picking some arbitrary number up front.

--
William Stacey [MVP]

"Steven" <no**@none.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
| Hi,
|
| I need to write an application using sockets.
| I have a server and about 10 clients "speaking" at the same time with the
| server, so i guess i need to use asynchronous sockets.
| But the server will receive from the clients an ascii string with a
| viariable length.
| And in all the example i found, the size of the buffer is always fixed
| (private byte[] theBuffer = new byte[1024]).
| I probably misunderstood the use of the buffer !
| I thought i could choose the largest size possible, and then decide that a
| special char (# for example) will end the stream, but i'm not sure of
this,
| and don't know how to do this ?!
|
| Thanks for any help !
|
|
|
|
|
Mar 8 '06 #6
Thanks for your answer.

When you say "For 10 clients, I would not even use async but a thread per
client server design (sync server)", do you mean that async sockets should
be used when having a lot of simultaneous clients, and that for a low number
of clients it's "enough" to use threads which are easier to use ?

Thanks !
Mar 8 '06 #7
That is a good way to put it :-) Some have done server's with something
like 1700+- threads (i.e. clients) with good perf. I personally would not
go that high and may think about async at about 1000 concurrent connections.

--
William Stacey [MVP]

"Steven" <no**@none.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
| Thanks for your answer.
|
| When you say "For 10 clients, I would not even use async but a thread per
| client server design (sync server)", do you mean that async sockets
should
| be used when having a lot of simultaneous clients, and that for a low
number
| of clients it's "enough" to use threads which are easier to use ?
|
| Thanks !
|
|
Mar 9 '06 #8
> "Steven" <no**@none.com> wrote in message
| When you say "For 10 clients, I would not even use async but a thread
per
| client server design (sync server)", do you mean that async sockets
| should be used when having a lot of simultaneous clients, and that
| for a low numberof clients it's "enough" to use threads which are
| easier to use ?


With 10 clients connected you could put together just about any
infrastructure you wanted for socket handling. Just for fun, you could
dynamically emit code into an assembly using the CodDom for each and every
incoming stanza, open a remoting channel to your new assembly, remote over
to it, start a COM+ Distributed Transaction, commit it, encrypt the results,
burnt them to CD, have a robotic arm move the CD between machines (gotta
archive this stuff, right?), commit the transaction, end the remoting call,
tear down the channel, and as long as you remembered to unload your emitted
assembly from your AppDomain everything would still work file. Even if you
forgot to unload your emitted assemblies it would still probably work for a
long time before running out of resources.

Modern hardware is so fast, and 10 connections so few, that you can get
ayway with pretty much anything.

Now, that said, I would still use Async sockets - I think they're the
easiest way to go and I know them quite well. Others may call me crazy.
Others may go further and call me names not printable here.

The real answer though is do it however you're most confortable.... and
whatever you do, don't make the rookie move and turn off Nagel's agorithm
thinking it'll help performance.

--
Chris Mullins
Mar 9 '06 #9
You left out the part about using Egyptian Carrier Pigeons to move the CDs
between machines. :)

--
William Stacey [MVP]
Mar 9 '06 #10
Let's see 1700 threads means 1.7GB of VM allocated for the thread stacks.
That means using .NET that there is not that much left for the application
once the CLR and the framework is loaded.
It's really a bad idea to create that number of threads in windows,
especially for sockets which can easily be bound to IOC ports that do not
use threads at all.

Willy.
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
| That is a good way to put it :-) Some have done server's with something
| like 1700+- threads (i.e. clients) with good perf. I personally would not
| go that high and may think about async at about 1000 concurrent
connections.
|
| --
| William Stacey [MVP]
|
| "Steven" <no**@none.com> wrote in message
| news:u1**************@TK2MSFTNGP09.phx.gbl...
|| Thanks for your answer.
||
|| When you say "For 10 clients, I would not even use async but a thread per
|| client server design (sync server)", do you mean that async sockets
| should
|| be used when having a lot of simultaneous clients, and that for a low
| number
|| of clients it's "enough" to use threads which are easier to use ?
||
|| Thanks !
||
||
|
|
Mar 9 '06 #11
| Let's see 1700 threads means 1.7GB of VM allocated for the thread stacks.
| That means using .NET that there is not that much left for the application
| once the CLR and the framework is loaded.
| It's really a bad idea to create that number of threads in windows,
| especially for sockets which can easily be bound to IOC ports that do not
| use threads at all.

? The IOCP callbacks use IOCP threads from IOCP thread pool (1000 threads).
So on a busy server using async, you can still block the IOCP thread pool
and you can still run out of memory for the read buffers because of "pinned"
buffers and heap fragmentation (better in 2.0). So if you have 500 IOCP
threads in callback, and each does another async operation before returning,
your at 1000 pretty fast. The Indy guy claims he did 1700+ no problems.
But I would not go that high. 10s to 100s is no problem for a threaded
server.

--
William Stacey [MVP]

Mar 9 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2341
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a...
0
1255
by: raza | last post by:
Hi, I have been programmin in C# for quite some time now and recently got into sockets programming in C# though i have done it in C++ on windows/linux . I have an interesting problem at hand . ...
5
2237
by: Dan Ritchie | last post by:
I've got a client/server app that I used to send large amounts of data via UDP to the client. We use it in various scenarios, one of which includes rendering a media file on the client as it is...
4
3549
by: Greg Young | last post by:
Ok so I think everyone can agree that creating buffers on the fly in an async socket server is bad ... there is alot of literature available on the problems this will cause with the heap. I am...
14
11861
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
3
11022
by: shahla.saeed | last post by:
hi, plzz check my code and let me know where the problem is lying...becuase whenever i try to tansfer the file of 573KB(mp3) it just tranfer few Kb of file(Somtimes 5.2Kb,somtimes 32Kb..every time...
1
4437
by: larspeter | last post by:
Hi all. I have a problem with TcpClient ... I am conneting to a server with TcpClient and returning the answer through a webservice. It actully all works fine. BUT if I make a lot of...
2
1716
by: marcf | last post by:
Hello Everyone! I have so nearly finished a mud client for a customer to the point where I was about to send them the source code. Out of luck I took the project home to try it on my personal PC...
1
3777
by: =?Utf-8?B?RGFydGgtQ3ot?= | last post by:
Hi, I'm beginner in Visual C++ so I want to ask you a question. And I'm not good in English:D So I have the program which communicates over sockets. The program has to connect to the server and...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.