Thread: winsock, sending c++ style strings

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    winsock, sending c++ style strings

    hello!, you may remember i posted a while back about a winsock chat program..
    anyway, i have encountered another problem. i can send C style strings (char str[]) but when trying to send a c++ style string (string str) i get errors, why?

    error C2664: 'send' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<c
    har> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    now i don't know what this is saying...
    i know i could just convert the string every time i needed to send it, but that would be messy...

    any ideas?

  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
    Why not use
    mystring.c_str()
    to get at the const char * version of the string?
    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
    Jan 2003
    Posts
    648
    Its expecting a C-string (char *), not a C++ std::string object. Like Salem said, use that method to get the C-string from the string object.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    i already tried that, it didn't work?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your code
    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.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    the code is really really long...

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Post the part that gives you the error.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Code:
    string outMessage;
    getline(cin,outMessage);
    iferror = send(clientSocket, outMessage.c_str, sizeof(outMessage), 0);
    		
    if (iferror == SOCKET_ERROR) {
    cout << "Send Failed\n";
    shutdownServer(clientSocket);
    }
    i have just pulled out the parts of the code that could possibly cause the error.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    send(clientSocket, outMessage.c_str(), sizeof(outMessage.c_str()), 0);

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    already tried that, still got the error

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    ohh, im very sorry, i didn't see the brackets, i feel stupid now... but yes it works perfectly now.
    thankyou very much

  12. #12
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    the c_str() member of the std::string class is a function, so it needs brackets.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    yes i forgot,
    im now getting errors using the same method for:

    Code:
    recv(clientSocket, inMessage.c_str(), sizeof(inMessage.c_str()), 0);
    error:

    error C2664: 'recv' : cannot convert parameter 2 from 'const char *' to 'char *'
    Conversion loses qualifiers

    is there a solution?

  14. #14
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    the recv() function acts differently to send()

    What you will need to do is create a temporary c-style string buffer, use that in the call to recv() and then copy the c-string into your std::string.

    Code:
    std::string Buf;
    char tempBuf[256] = "";
    
           BytesRecv = recv(Socket, tempBuf, 256, 0);
    
           if(strcmp("", tempBuf))
                  Buf = tempBuf;
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sizeof(inMessage.c_str())
    Shouldn't this be
    strlen(inMessage.c_str())

    > BytesRecv = recv(Socket, tempBuf, 256, 0);
    You need to allow room for appending a \0, and actually add a \0 as well
    Code:
    BytesRecv = recv(Socket, tempBuf, sizeof(tempBuf)-1, 0);
    if ( BytesRecv > 0 ) {
        tempBuf[BytesRecv] = '\0';
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM