Re: Client-Server application (based on sockets) - too many messages at the same time

Tech-Archive recommends: Speed Up your PC by fixing your registry



On Sat, 02 Jun 2007 03:22:52 -0700, Cichy <cichy83@xxxxxxxxx> wrote:

Yes, You we're right that I'm using TCP Protocol. I thought that when
I'm sending some datas over a socket it's checking how many bytes
we're sent and received.

Define "checking". In one sense, I suppose it is...after all, assuming nothing's wrong with the connection, the bytes all do eventually get sent and received. The "how many" is "checked" in that sense.

However, TCP doesn't do anything to ensure that between the first byte sent and the last byte sent, that everything received is grouped exactly the same way that it was sent.

I really was sure that there is some sort of byte (like EOM - End Of
Message, like EOF in files) which idnicates if everything was sent.

There's definitely nothing like that. You put a byte in at one end, that byte comes out at the other. Nothing more, nothing less.

But know I'm not too sure where should I check this things?

You need to look at the return value for Socket.EndReceive() and Socket.EndSend(). These are integers that tell you how many bytes were actually successfully received or sent.

When sending it looks more simple, I have a method:

public void OnSend(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndSend(ar);
}
catch (Exception ex)
{
if (eerrorOccured != null)
eerrorOccured(this, ex.Message);
}
}

and probably here i can check how many bytes where sent (probably I
have to add second parameter with message to be able to resend some
bytes).

You need to hang on to the original send buffer, presumably in some per-client data structure. Along with that, you need to keep track of how many bytes in the buffer have already been sent, so that when you call Socket.EndSend(), you can compare the byte count with how many you had left to send, and if the sent bytes are fewer than the amount you actually have to send, make another attempt to send the remaining bytes.

But where should I check if i received all bytes (and the more
important questions, how could I know how many of them should
be ???).

Yes, that is the more important question. Your protocol needs to define a way for the recipient of the data to know how large that data is, so that it can tell whether it's received it all or not. How this is done depends entirely on the protocol.

Some precede the data with a byte count (which itself has a predefined way to know its length, whether that's because it's a fixed-size variable, or the byte count is sent as a string, or something like that). Some rely on something specific about the data itself; for example, strings that are always null-terminated. Some use fixed-sized structures so that every "message" is always the same size. And some even only allow for a single "message" to be sent on a given connection; once a complete "message" has been sent, the connection is closed (I'm not suggesting this is appropriate for your design...just that it's an example of how some other protocols work).

What you will do is entirely up to you, assuming you have defined your own application-level protocol (which it appears you have). But you do need to include something in your protocol so that the recipient knows when it has received a complete "message".

And also, is this construction doesn't fix the problem of receiving
only a part of a message:

clientSocket.EndReceive(ar);

I'm sorry, I don't understand the above sentence.

Pete
.



Relevant Pages

  • Re: NetworkFacade 0.4
    ... This means that the protocol is similar to DRb, but different, ... with regards to connection caching and what happens if connections are ... can be made in both directions down the same socket. ... hostname and a port on the firewall which forwards inbound connections. ...
    (comp.lang.ruby)
  • Re: About Information Hiding Design Rules
    ... EALREADY Connection already in progress ... ENETRESET Connection aborted by network ... ENOTCONN The socket is not connected ... EPFNOSUPPORT Protocol family not supported ...
    (comp.lang.c)
  • Re: Socket mit Network-Byte-Order (Motorola-Format)- Client und Server
    ... dass Du mit dem eigenen Server den eigenen Code testest. ... Connection z.b. zwischen Read und WriteTelegram. ... Socket serverSocket = new Socket( ... string request = ReadTelegram; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: SOCKET_ERROR [10054]
    ... 10041 Protocol wrong type for socket. ... 10052 Network dropped connection on reset. ... the Windows Sockets DLL cannot support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: NetworkFacade 0.4
    ... This means that the protocol is similar to DRb, but different, i.e. ... Doesn't allow bi-directional method calls through a single connection, ... made in both directions down the same socket. ... hostname and a port on the firewall which forwards inbound connections. ...
    (comp.lang.ruby)