CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2005
    Location
    Chandigarh(India)
    Posts
    97

    Question How to find data encoding of received buffer with WSARecv()

    Can any one help me to find data encoding of received buffer with WSARecv() command.

    Weather data send by client is in UNICODE or in ASCII format

    E.g.:

    WSARecv (AcceptSocket, &DataBuf, 1, &RecvBytes, &Flags, &AcceptOverlapped, NULL)

    DataBuf.buff is in ASCII or in UNICODE

    Regads,

    Madhu S. Kapoor

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How to find data encoding of received buffer with WSARecv()

    Quote Originally Posted by madhusudankapoor
    Can any one help me to find data encoding of received buffer with WSARecv() command.

    Weather data send by client is in UNICODE or in ASCII format ....
    This should be part of your protocol. The client should tell you, at some time during initail handshaking between server and client, whter it will be sending data in ASCII or Unicode. If the client tells you, then you don't need to guess.

    Mike

  3. #3
    Join Date
    Oct 2005
    Location
    Chandigarh(India)
    Posts
    97

    Re: How to find data encoding of received buffer with WSARecv()

    Fisrt of all thanks mike,

    Suppose i make a message header that tell me messges is in ASCII or in UNICODE and every time client connect to server he send me this header as fisrt message so server can able to get encoding. Is there any case where i get second message form client fist and later i get first message(Header message)

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: How to find data encoding of received buffer with WSARecv()

    Quote Originally Posted by madhusudankapoor
    Suppose i make a message header that tell me messges is in ASCII or in UNICODE and every time client connect to server he send me this header as fisrt message so server can able to get encoding. Is there any case where i get second message form client fist and later i get first message(Header message)
    On a single TCP connection, there's zero possibility that this might happen. TCP is designed so that if the server is getting the n-th byte of a transmission, then it is 100% guaranteed that it has already received every byte up to the (n-1)-th. In other words, the server will get every single byte and it will get these bytes in the exact same order that they were sent.

    Note that if you have opened two or more TCP connections, then there is no guarantee at all on the relative ordering between the connections. So, even if a client is sending bytes such that it alternates sendings between two connections, it still is possible that the server will see all of the bytes on the first connection before it sees any of the bytes on the second.

    Please also note that we're talking about byte streams here, not "messages" as mentioned in your post. The difference between the two is critical to understanding TCP. A client might send out 100 bytes in a single call to send(), but that does not guarantee that the server will see all 100 bytes in a single call to revc(). It's possible that the server will need to call recv() multiple times, each time receiving a few more bytes from the stream. It is your responsiblity to impose a protocol for delimiting message boundaries, to check the value returned by the call to recv() to find out how many bytes were actually received, and to extract messages from the received bytes according to your protocol.

    Mike

  5. #5
    Join Date
    Oct 2005
    Location
    Chandigarh(India)
    Posts
    97

    Re: How to find data encoding of received buffer with WSARecv()

    Thanks Mike

  6. #6
    Join Date
    Oct 2005
    Location
    Chandigarh(India)
    Posts
    97

    Re: How to find data encoding of received buffer with WSARecv()

    Can you pls help me why I my server lost data , What should i do in case of Error

    Here is my server Code

    DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
    {
    /*-- Notify Starting of server -- */

    printf("\nHello Server Started, I am in worker thread! \n");

    HANDLE hIocp = (HANDLE) CompletionPortID;
    ULONG_PTR *PerHandleKey;
    OVERLAPPED *Overlap;
    OVERLAPPEDPLUS *OverlapPlus,*newolp;
    DWORD dwBytesXfered;
    int ret;
    char *strData ;
    while (1)
    {
    ret = GetQueuedCompletionStatus(hIocp,&dwBytesXfered,(PULONG_PTR)&PerHandleKey,&Overlap,INFINITE);
    if (ret == 0){
    continue;
    }
    OverlapPlus = CONTAINING_RECORD(Overlap, OVERLAPPEDPLUS, Overlapped);

    /*--- Process Data According to Operation Code ---*/
    switch (OverlapPlus->nOperationCode )
    {
    case OP_READ:
    // Process the data read Repost the read if necessary, reusing the same receive buffer as before

    ZeroMemory(&(OverlapPlus->Overlapped),sizeof(OVERLAPPED));
    ret = WSARecv(OverlapPlus->sClient,&OverlapPlus->wbuf,1,&OverlapPlus->dwBytes,&OverlapPlus->dwFlags,&OverlapPlus->Overlapped, NULL);

    if(0 == OverlapPlus->dwBytes)
    continue;

    strData = (char*) calloc(OverlapPlus->dwBytes+1,sizeof(char));
    strcpy(strData,&OverlapPlus->wbuf.buf[0]);

    appendLog(strData);
    memset(&OverlapPlus->Overlapped, 0, sizeof(OVERLAPPED));

    free(strData);

    if (ret == SOCKET_ERROR)
    {
    ret = WSAGetLastError();
    if ( ret != WSA_IO_PENDING)
    {
    // What should i do in case of Error???
    printf("Error occur at WSARecv() : %d", ret);
    break;
    }
    }
    break;
    case OP_WRITE:
    appendLog("WRITE");
    // Process the data sent, etc.
    break;//…
    }
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured