![]() |
| | #1 |
| Registered User Join Date: Apr 2004
Posts: 102
| winsock, sending c++ style strings 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? |
| b00l34n is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #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. |
| Speedy5 is offline | |
| | #4 |
| Registered User Join Date: Apr 2004
Posts: 102
| i already tried that, it didn't work? |
| b00l34n is offline | |
| | #5 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Post your code
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #6 |
| Registered User Join Date: Apr 2004
Posts: 102
| the code is really really long... |
| b00l34n is offline | |
| | #7 |
| mustang benny Join Date: Jul 2002
Posts: 1,401
| Post the part that gives you the error.
__________________ benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials |
| bennyandthejets is offline | |
| | #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);
}
|
| b00l34n is offline | |
| | #9 |
| Registered User Join Date: Jan 2003
Posts: 648
| Code: send(clientSocket, outMessage.c_str(), sizeof(outMessage.c_str()), 0); |
| Speedy5 is offline | |
| | #10 |
| Registered User Join Date: Apr 2004
Posts: 102
| already tried that, still got the error |
| b00l34n is offline | |
| | #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 |
| b00l34n is offline | |
| | #12 |
| Grammar Police 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 |
| HybridM is offline | |
| | #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 C2664: 'recv' : cannot convert parameter 2 from 'const char *' to 'char *' Conversion loses qualifiers is there a solution? |
| b00l34n is offline | |
| | #14 |
| Grammar Police 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 |
| HybridM is offline | |
| | #15 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Programming using strings | jlu0418 | C++ Programming | 5 | 11-26-2006 08:07 PM |
| Problems with strings as key in STL maps | all_names_taken | C++ Programming | 3 | 01-17-2006 11:34 AM |
| Winsock Messaging Program | Morgul | Windows Programming | 13 | 04-25-2005 04:00 PM |
| Where do I initialize Winsock and catch messages for it? | Lithorien | Windows Programming | 10 | 12-30-2004 12:11 PM |
| array of strings + more | null | C Programming | 10 | 10-01-2001 03:39 PM |