recv() second arg

This is a discussion on recv() second arg within the Networking/Device Communication forums, part of the General Programming Boards category; I dont know if it makes any sense but its kinda strange what i found. From the client i send ...

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

    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Thanks a lot, its working!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 01: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, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21