Thread: recv() second arg

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    recv() second arg

    I dont know if it makes any sense but its kinda strange what i found.

    From the client i send send(server, size, 8, 0); with the second arg: char* size.

    On the server i recv(client, size, 8, 0); with the second arg: char size[8].

    If i change the type to char* size on the server too, it stops working.

    Its not that important but maybe someone had an idea why is that.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If i change the type to char* size on the server too, it stops working.
    You become responsible for making sure it points to allocated memory.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Salem!

    So i guess it means i shouldnt use it if its not allocated.

    By allocated i guess you mean, dynamically like that:

    Code:
    char* size = new char[8];
    Unfortunately i have to use char* size because with char size[8] it wont compile on that line on the client side.

    Code:
                         strcpy(Cmd, "Upload");
                         send(server, Cmd, 7, 0);
                         Sleep(5);
                         send(server, RemoteP, MAX_PATH, 0);
                         ifstream r(LocalP, ios::in | ios::binary);
                         // get size of file:
                         r.seekg (0, ios::end);
                         sz = r.tellg();
                         r.seekg (0, ios::beg);
                         // error: need char* for size
                         size = itoa(sz, buff, 10);
                         send(server, size, 8, 0);
    
                         char* Buffer = new char [sz];
                         ZeroMemory(Buffer, sz);
                         r.read(Buffer, sz);
                         send(server, Buffer, sz, 0);
                         cout << Buffer << endl;
    
                         r.close();
                         delete[] Buffer;
    Last edited by Ducky; 07-04-2009 at 01:10 PM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try reading the manual page for itoa then.
    Quote Originally Posted by a man page
    RETURN VALUES

    The function itoa() always returns the value string. There is no error return.
    Forget about size, just use buff
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks a lot, its working!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to struct
    By Paul Johnston in forum C Programming
    Replies: 4
    Last Post: 06-11-2009, 03:01 AM
  2. Replies: 10
    Last Post: 06-08-2009, 02:42 PM
  3. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 02:10 PM
  4. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  5. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 05:32 PM