Click here to Skip to main content
15,888,293 members
Home / Discussions / C#
   

C#

 
AnswerRe: CSV file Pin
Eddy Vluggen7-Sep-09 1:22
professionalEddy Vluggen7-Sep-09 1:22 
GeneralThanks a lot Pin
Abbas_here7-Sep-09 1:37
Abbas_here7-Sep-09 1:37 
AnswerRe: CSV file Pin
Vivek Vijayan7-Sep-09 1:25
Vivek Vijayan7-Sep-09 1:25 
AnswerRe: CSV file Pin
DaveyM697-Sep-09 1:35
professionalDaveyM697-Sep-09 1:35 
AnswerRe: CSV file Pin
Henry Minute9-Sep-09 11:40
Henry Minute9-Sep-09 11:40 
GeneralRe: CSV file Pin
Abbas_here9-Sep-09 20:30
Abbas_here9-Sep-09 20:30 
QuestionHow to stream an FLV vdo on a device application Pin
Vivek Vijayan7-Sep-09 1:00
Vivek Vijayan7-Sep-09 1:00 
QuestionC# Socket.BeginReceive/EndReceive Pin
staticv7-Sep-09 0:24
staticv7-Sep-09 0:24 
In what order is the Socket.BeginReceive/EndReceive functions called?

For instance, I call BeginReceive twice, once to get the message length and the second time to get the message itself. Now the scenario is like that, for every message I send, I start waiting for its completion (actually acknowledgment of the message sent, also I wait for the action's completion after receiving the acknowledgment), so I call BeginReceive with each BeginSend, but in each BeginReceive's callback, I check if I'm receiving the length or the message. If I'm receiving the message and have received it completely, then I call another BeginReceive to receive the completion of the action. Now this is where things get out of sync. Because one of my receive callback is receiving bytes which it interprets as the length of them message when in fact it is the message itself.

Now how do I resolve it?

Here is the code, basically it is too big, sorry for that

public void Send(string message)
        {
                try
                {
                        bytesSent = 0;

                        writeDataBuffer = System.Text.Encoding.ASCII.GetBytes(message);
                        writeDataBuffer = WrapMessage(writeDataBuffer);
                        messageSendSize = writeDataBuffer.Length;

                        clientSocket.BeginSend(writeDataBuffer, bytesSent, messageSendSize, SocketFlags.None,
                                                                new AsyncCallback(SendComplete), clientSocket);
                }
                catch (SocketException socketException)
                {
                        MessageBox.Show(socketException.Message);
                }
        }

        public void WaitForData()
        {
                try
                {
                        if (! messageLengthReceived)
                        {
                                clientSocket.BeginReceive(receiveDataBuffer, bytesReceived, MESSAGE_LENGTH_SIZE - bytesReceived,
                                                                                SocketFlags.None, new AsyncCallback(RecieveComplete), clientSocket);
                        }
        public void Send(string message)
{
    try
    {
        bytesSent = 0;

        writeDataBuffer = System.Text.Encoding.ASCII.GetBytes(message);
        writeDataBuffer = WrapMessage(writeDataBuffer);
        messageSendSize = writeDataBuffer.Length;

        clientSocket.BeginSend(writeDataBuffer, bytesSent, messageSendSize, SocketFlags.None,
                                                new AsyncCallback(SendComplete), clientSocket);
    }
    catch (SocketException socketException)
    {
        MessageBox.Show(socketException.Message);
    }
}

public void WaitForData()
{
    try
    {
        if (! messageLengthReceived)
        {
                clientSocket.BeginReceive(receiveDataBuffer, bytesReceived, MESSAGE_LENGTH_SIZE - bytesReceived,
                                                                SocketFlags.None, new AsyncCallback(RecieveComplete), clientSocket);
        }
        else 
        {
                clientSocket.BeginReceive(receiveDataBuffer, bytesReceived, messageLength - bytesReceived,
                                                                SocketFlags.None, new AsyncCallback(RecieveComplete), clientSocket);
        }
    }
    catch (SocketException socketException)
    {
        MessageBox.Show(socketException.Message);
    }
}

public void RecieveComplete(IAsyncResult result)
{
    try
    {
        Socket socket = result.AsyncState as Socket;
        bytesReceived = socket.EndReceive(result);

        if (! messageLengthReceived)
        {
                if (bytesReceived != MESSAGE_LENGTH_SIZE)
                {
                        WaitForData();
                        return;
                }

                // unwrap message length
                int length = BitConverter.ToInt32(receiveDataBuffer, 0);
                length = IPAddress.NetworkToHostOrder(length);

                messageLength = length;
                messageLengthReceived = true;

                bytesReceived = 0;

                // now wait for getting the message itself
                WaitForData();
        }
        else
        {
                if (bytesReceived != messageLength)
                {
                        WaitForData();
                }
                else
                {
                string message = Encoding.ASCII.GetString(receiveDataBuffer);

                MessageBox.Show(message);

                bytesReceived = 0;
                messageLengthReceived = false;

                // clear buffer
                receiveDataBuffer = new byte[AsyncClient.BUFFER_SIZE];

                WaitForData();
                }
        }
    }
    catch (SocketException socketException)
    {
        MessageBox.Show(socketException.Message);
    }

}

public void SendComplete(IAsyncResult result)
{
    try
    {
        Socket socket = result.AsyncState as Socket;
        bytesSent = socket.EndSend(result);

        if (bytesSent != messageSendSize)
        {
                messageSendSize -= bytesSent;

                socket.BeginSend(writeDataBuffer, bytesSent, messageSendSize, SocketFlags.None,
                                                new AsyncCallback(SendComplete), clientSocket);
                return;
        }

        // wait for data
        messageLengthReceived = false;
        bytesReceived = 0;

        WaitForData();
    }
    catch (SocketException socketException)
    {
        MessageBox.Show(socketException.Message);
    }
}


Top Web Hosting Providers[^]

Do, or do not. There is no 'try'.

AnswerRe: C# Socket.BeginReceive/EndReceive Pin
Luc Pattyn7-Sep-09 2:31
sitebuilderLuc Pattyn7-Sep-09 2:31 
GeneralRe: C# Socket.BeginReceive/EndReceive Pin
staticv7-Sep-09 22:02
staticv7-Sep-09 22:02 
QuestionConvert doc file to image file Pin
Member 46007157-Sep-09 0:19
Member 46007157-Sep-09 0:19 
AnswerRe: Convert doc file to image file Pin
Christian Graus7-Sep-09 0:36
protectorChristian Graus7-Sep-09 0:36 
QuestionPlease Help Pin
itsravie6-Sep-09 23:59
itsravie6-Sep-09 23:59 
AnswerRe: Please Help [modified] Pin
DaveyM697-Sep-09 0:05
professionalDaveyM697-Sep-09 0:05 
AnswerRe: Please Help Pin
Nicholas Butler7-Sep-09 0:15
sitebuilderNicholas Butler7-Sep-09 0:15 
GeneralRe: Please Help Pin
itsravie7-Sep-09 0:17
itsravie7-Sep-09 0:17 
GeneralRe: Please Help Pin
Kevin McFarlane7-Sep-09 0:19
Kevin McFarlane7-Sep-09 0:19 
GeneralRe: Please Help Pin
itsravie7-Sep-09 0:38
itsravie7-Sep-09 0:38 
GeneralRe: Please Help Pin
Not Active7-Sep-09 2:59
mentorNot Active7-Sep-09 2:59 
GeneralRe: Please Help Pin
Nicholas Butler7-Sep-09 0:20
sitebuilderNicholas Butler7-Sep-09 0:20 
GeneralRe: Please Help Pin
DaveyM697-Sep-09 0:23
professionalDaveyM697-Sep-09 0:23 
GeneralRe: Please Help Pin
Nicholas Butler7-Sep-09 0:39
sitebuilderNicholas Butler7-Sep-09 0:39 
QuestionProblem in setup project [modified] Pin
xodeblack6-Sep-09 23:55
xodeblack6-Sep-09 23:55 
AnswerRe: Problem in setup project Pin
Keith Barrow7-Sep-09 0:02
professionalKeith Barrow7-Sep-09 0:02 
GeneralRe: Problem in setup project Pin
xodeblack7-Sep-09 0:09
xodeblack7-Sep-09 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.