In The Name of God
hello
as u know tcp doesn't protect message boundries it means messages might
be mixed into each other. in addition to that in TCP it's possible that
a message would be delivered in diffrent times.
so you must use a while loop to get the complete message. like this:
int size_Total = 0;
int size_Dataleft = 4;
int size_Received = 0;
byte[] size_Container = new byte[4];
while( size_Total < 4 )
{
size_Received = sck_Socket.Receive(size_Container, size_Total,
size_Dataleft, 0);
if( size_Received == 0 )
return "Exit";
size_Total += size_Received;
size_Dataleft -= size_Received;
}
Does TCP Asyncronous sockets protect message boundary I mean is it
necessary to use such a loop like this in Asyncronous sockets or it waits to get the complete message and then it returns

Asyncronous Sockets support TCP Message Boundary?
bkaye
NitinDhall
If you detect that you haven't received the whole message, then you start another asynchronous read. ie socket.BeginReceive(...).
In other words, your state object contains information about how much data you have received and how much you are expecting. Once you have received all of the data, then you kick off the processing code that handles the message. Asynchronous sockets do not protect message boundaries.
kishan_99
in addition to that in TCP it's possible that a message would be delivered in diffrent times. This is not correct
Does TCP Asyncronous sockets protect message boundary No
Please look at http://blogs.msdn.com/joncole for an example on how you ca do this using custom framing
hzwang
hello
Now my cycle is complete. first i got the problem that i have now, then i changed my code serveral times to overcome that problem, now i am on that problem again.
this is my send and receive asyncronous algorithm: I send the length of algorithm and then actual message.
I have multiple messages to send which they are over and over again, i mean i send user name and them immediately i wanna send password, but the problem is when i wanna receive the password size, Surprisingly i receive the actual password.....and so the size would be something awsome and the program blows....or sometimes in the BegindReceive procedure or anything like this while i have sent data there is no data to peak but i know there is.......this is about 1 month that i got this one, any one can help me out of this morass:
is there any problem with my send and receive functions Please help
Send
-------------------------------------------------------------------
private static bool Special_Send(Socket sck_Socket, byte[] data)
{
//Sending the size
int size_Total = 0;
int size_Size = 4;
int size_Dataleft = size_Size;
int size_Sent = 0;
byte[] size_Container = new byte[4];
size_Container = BitConverter.GetBytes(data.Length);
while( size_Total < size_Size )
{
//i have defined this class at the end of this post: look down, this is for getting the //async function information
AsyncState obj_Size_AsyncState=new AsyncState();
obj_Size_AsyncState.sck_AsyncSocket=sck_Socket;
sck_Socket.BeginSend(size_Container, size_Total, size_Dataleft, 0,new AsyncCallback(Size_Send_Delegate),obj_Size_AsyncState);
size_Total += obj_Size_AsyncState.int_Sent;
size_Dataleft -= obj_Size_AsyncState.int_Sent;
}
//Sending Data
int data_Total = 0;
int data_Size = data.Length;
int data_Dataleft = data_Size;
int data_Sent = 0;
while( data_Total < data_Size )
{
AsyncState obj_Data_AsyncState=new AsyncState();
obj_Data_AsyncState.sck_AsyncSocket=sck_Socket;
sck_Socket.BeginSend(data, data_Total, data_Dataleft, 0,new AsyncCallback(Data_Send_Delegate),obj_Data_AsyncState);
data_Total += obj_Data_AsyncState.int_Sent;
data_Dataleft -= obj_Data_AsyncState.int_Sent;
}
//sck_Socket.Shutdown(SocketShutdown.Both);
//sck_Socket.Close();
return true;
}
-----------------------------------------------------------
Send Asynccallback delegate for Size
------------------------------------------------------------
public static void Size_Send_Delegate(IAsyncResult iar)
{
AsyncState obj_AsyncState=(AsyncState)iar.AsyncState;
obj_AsyncState.int_Sent=obj_AsyncState.sck_AsyncSocket.EndReceive(iar);
}
-----------------------------------------------------------
Send Asynccallback delegate for Data
------------------------------------------------------------
public static void Data_Send_Delegate(IAsyncResult iar)
{
AsyncState obj_AsyncState=(AsyncState)iar.AsyncState;
obj_AsyncState.int_Sent=obj_AsyncState.sck_AsyncSocket.EndReceive(iar);
}
---------------------------------------------------------------------------
Receive
-------------------------------- ---------------------------------------------
private static string Special_Receive(Socket sck_Socket)
{
//Receiving the Size
int size_Total = 0;
int size_Dataleft = 4;
int size_Received = 0;
byte[] size_Container = new byte[4];
while( size_Total < 4 )
{
AsyncState obj_Size_AsyncState=new AsyncState();
obj_Size_AsyncState.sck_AsyncSocket=sck_Socket;
sck_Socket.BeginReceive(size_Container, size_Total, size_Dataleft, 0,new AsyncCallback(Size_Recieve_Delegate),obj_Size_AsyncState);
size_Total += obj_Size_AsyncState.int_Received;
size_Dataleft -= obj_Size_AsyncState.int_Received;
}
int data_Size = BitConverter.ToInt32(size_Container, 0);
//Receiving data
int data_Total = 0;
int data_Dataleft = data_Size;
int data_Received = 0;
byte[] data = new byte[data_Size];
while( data_Total < data_Size )
{
AsyncState obj_Data_AsyncState=new AsyncState();
obj_Data_AsyncState.sck_AsyncSocket=sck_Socket;
sck_Socket.BeginReceive(data, data_Total, data_Dataleft, 0,new AsyncCallback(Data_Receive_Delegate),obj_Data_AsyncState);
data_Total += obj_Data_AsyncState.int_Received;
data_Dataleft -= obj_Data_AsyncState.int_Received;
}
//sck_Socket.Shutdown(SocketShutdown.Both);
//sck_Socket.Close();
return Encoding.ASCII.GetString(data, 0, data_Size);
}
-----------------------------------------------------------
Receive Asynccallback delegate for Size
------------------------------------------------------------
public static void Size_Recieve_Delegate(IAsyncResult iar)
{
AsyncState obj_AsyncState=(AsyncState)iar.AsyncState;
obj_AsyncState.int_Received=obj_AsyncState.sck_AsyncSocket.EndReceive(iar);
}
-----------------------------------------------------------
Receive Asynccallback delegate for Data
------------------------------------------------------------
public static void Data_Receive_Delegate(IAsyncResult iar)
{
AsyncState obj_AsyncState=(AsyncState)iar.AsyncState;
obj_AsyncState.int_Received=obj_AsyncState.sck_AsyncSocket.EndReceive(iar);
}
-----------------------------------------------------------------
and here is the class AsyncState that i use to get the amount byte sent and received:
-----------------------------------------------------------------
class AsyncState
{
public Socket sck_AsyncSocket;
public int int_Sent;
public int int_Received;
}
e_LA
in addition to that in TCP it's possible that a message would be delivered in diffrent times. This is not correct
I meant the sent method might not to send the complete byte buffer or the receive method to receive it. and this is correct. is't it
i know how to overcome this problem in ordinary send and receive.... but i don't know how to implement the tecnique that i told in the previous post like this:
private static string Special_Receive(Socket sck_Socket)
{
int size_Total = 0;
int size_Dataleft = 4;
int size_Received = 0;
byte[] size_Container = new byte[4];
while( size_Total < 4 )
{
size_Received = sck_Socket.Receive(size_Container, size_Total, size_Dataleft, 0);
if( size_Received == 0 )
return "Exit";
size_Total += size_Received;
size_Dataleft -= size_Received;
}
int data_Size = BitConverter.ToInt32(size_Container, 0);
}
I want to do this asyncrounously, but when i wanna do it i face some problems like this:
here is the tecnique please tell me how to do the red part:
When serializing the message to be sent, I serialize to a MemoryStream, get the length of the stream, add 8 bytes (Int64) to the beginning which are the length of the stream and convert it to a byte array and send it off.
The receiving side is the tricky part since you don't know how big of a packet you will get. So, you need a state object to store header information that tells you how much data of a particular message you already received and how much there is still to come.
Therefore, when the first bytes arrive you need to get the message size from the header. So, you start reading the first 8 bytes and you know how much more data there is to expect (But beware that you might receive only 1 byte so only set the validity of the header to true once you received all 8 bytes). Then you continue reading the rest of the available bytes into a memory stream of the stateobject. When you received all bytes for the message, you simply deserialize the message. If your message is e.g. 300bytes long, but you only received 100, store those 100 bytes in the stateobject memorystream and when the next call to endreceive occurs, attach the incoming buffer to the existing 100 bytes of the stream .. then continue reading the stream until you get your full message.
My question is how to repeat the EndReceive method for receiving the whole message
(I should mention that i have some continious send and receive).
dsutherland
Hello,
Thanks for your response , I'll check it out.