Thread: Socket Programming??

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    Socket Programming??

    Hi folks,
    I'm triying to send message form the client to the server in BCB 6.0 but when I send the message I got immediately "Error receiving" I couldn't find where is the wrong..
    Any answer will appreciate..

    My code look like this
    --- client ---
    Code:
        bool __fastcall SendBuffer(TCustomWinSocket *Socket, void *Buffer, int 
    Length) 
        { 
            int Sent = 0; 
            int Total_Sent = 0; 
            BYTE *Buf = (LPBYTE)Buffer; 
    
            while( Total_Sent < Length ) 
            { 
                Sent = Socket->SendBuf(&Buf[Total_Sent], Length - Total_Sent); 
                if( Sent <= 0 ) 
                { 
                    if( WSAGetLastError() == WSAEWOULDBLOCK ) 
                        continue; 
                    return false; 
                } 
                Total_Sent += Sent; 
            } 
            return true; 
        } 
    
        //... 
    
        AnsiString text = Edit1->Text; 
        int length = text.Length(); 
    
        bool ok = SendBuffer(Form1->ClientSocket1->Socket, &length, 
    sizeof(int)); 
        if( ok ) 
            ok = SendBuffer(Form1->ClientSocket1->Socket, text.c_str(), length); 
        if( !ok ) 
            ShowMessage("Error"); 
    
    
        --- server --- 
    
        bool __fastcall ReadBuffer(TCustomWinSocket *Socket, void *Buffer, int 
    Length) 
        { 
            int Read = 0; 
            int Total_Read = 0; 
            BYTE *Buf = (LPBYTE)Buffer; 
    
            while( Total_Read < Length ) 
            { 
                Read = Socket->ReceiveBuf(&Buf[Total_Read], Length - 
    Total_Read); 
                if( Read <= 0 ) 
                { 
                    if( WSAGetLastError() == WSAEWOULDBLOCK ) 
                        continue; 
                    return false; 
                } 
                Total_Read += Read; 
            } 
            return true; 
        } 
    
        //... 
    
        AnsiString buffer; 
        int length = 0; 
    
        bool ok = ReadBuffer(Socket, &length, sizeof(int)); 
        if( ok ) 
        { 
            buffer.SetLength(length); 
            ok = ReadBuffer(Socket, buffer.c_str(), length); 
        } 
        if( !ok ) 
            ShowMessage("Error receiving");
    Thanks in Advance
    BigSky_(*
    Last edited by Salem; 05-08-2004 at 03:18 PM. Reason: Code tagging

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I might look at the code after you put it in code tags.
    [ code ] ... [/ code ] (without the spaces).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    if( WSAGetLastError() == WSAEWOULDBLOCK )
    continue;
    return false;
    Instead of just returning false, why not print out the value returned by WSAGetLastError(), so you have more information to hand

    > ok = ReadBuffer(Socket, buffer.c_str(), length)
    As far as I know, these c_str() things are supposed to be constant, so its not correct to go writing into them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    2
    Well,
    In fact this code works but when exactly message received after that I get

    > if( !ok )
    > ShowMessage("Error receiving");
    I couldn't understand if code works Why I am receiving this error...


    BigSky_(*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM