473,586 Members | 2,863 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 2392
"Steven" <no**@none.co m> wrote in message
news:uk******** ******@TK2MSFTN GP10.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(Yo urLocalEndPoint )
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**@non e.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.publi c.dotnet.langua ges.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**@non e.com]
Posted At: Wednesday, March 08, 2006 8:02 AM
Posted To: microsoft.publi c.dotnet.langua ges.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.co m> wrote in message
news:uk******** ******@TK2MSFTN GP10.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.co m> wrote in message
news:u1******** ******@TK2MSFTN GP09.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.co m> 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

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

Similar topics

0
2346
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 SocketOptionName.SendTimeout for sends on TCPIP sockets. In all the tests that I have done with both Async and Sync sends the send returns immediately with the...
0
1259
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 . I am porting a C++ tunnel library into C# . It has 2 functions WriteOut: I make a header and send it then send the actual data to the server and...
5
2240
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 transferred via the underlying UDP transport. In this scenario it is very important to keep CPU usage as low as possible. Both the client and...
4
3554
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 looking at a few options to get around this. 1) Have a BufferPool class that hands out ArraySegment<byteportions of a larger array (large enough that...
14
11880
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 doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call ...
3
11028
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 i run the program).....but when i watch the transfering of bytes by debuging it.(RED dot on while(nfs.CanRead) )..it shows that complete bytes are...
1
4451
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 (re)connection (hitting the submit button) then I start to recieve a an error: IOException:System.IO.IOException: Der kunne ikke læses data fra...
2
1728
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 just to make sure. At work on my corporate network everything works perfectly but it seems once anyone uses the client on a direct high speed...
1
3786
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 send and receive data. But when I start the program, there is one Socketexception - something like the program is unable to connect to the server...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
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 we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.