views:

34

answers:

4

First of all i am new to networking so i may say dumb thing in here. Considering a client-server application using sockets(.net with c# if that matters).

  1. The client sends some data, the server process it and sends back a string.
  2. The client sends some other data, the serve process it, queries the db and sends back several hundreds of items from the database
  3. The client sends some other type of data and the server notifies some other clients

. My question is how to set the buffer size correctly for reading/writing operation. Should i do something like this: byte[] buff = new byte[client.ReceiveBufferSize] ?

I am thinking of something like this:
Client sends data to the server(and the server will follow the same pattern)
byte[] bytesToSend=new byte[2048] //2048 to be standard for any command send by the client
bytes 0..1 ->command type
bytes 1..2047 ->command parameters
byte[] bytesToReceive=new byte[8]/byte[64]/byte[8192] //switch(command type)

But..what is happening when a client is notified by the server without sending data? What is the correct way to accomplish what i am trying to do? Thanks for reading.

A: 

Doesn't matter what is the size of your buffer, it should be just a temporary storage for the data. Read this about reading from binary streams: http://www.yoda.arachsys.com/csharp/readbinary.html

František Žiačik
A: 

I don't really understand what you are trying to do, but i try to give some advice. Your byte array and the Socket.ReceiveBufferSize property are two different things. The buffer size doesn't matter. You should use a byte array large enough to store the data you want to send or receive. For the size of the byte array, 2048 is probably ok, unless you want to send images, very large text, etc. I've done some simple client server programming and i found it easier if the receiver knows how much data to receive in advance. So for example you send the length of the string first as 1 or 2 bytes, and then the string using utf8 encoding. Use the Encoding.GetByteCount method for getting the size of the string in bytes for the chosen encoding. You should convert numbers to binary using the methods from Bitconverter. I found those helpful and now i wish, i would have discovered them earlier. To send other more complex data types you should decompose them and send them as strings or numbers one at a time.

kudor gyozo
A: 

You can simplest think about XML as a protocol of your communication, then you reading socket until null byte was readed. (This is typicaly marker of end of xml document). You can implement this protocol in two ways.

To parse and generate XML document you can use XMLDocument class. You must to it impelemnt some schema. But if you want clean transform data as XML you can use simplest DataSet.

Svisstack
A: 

When designing client/server communication it is good to introduces some a notion of a communication protocol (basically a set of rules, format that will be sent/received) by the client/server.

Here's an example of exchanging messages of variable size over the network

Vadmyst