C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-23-2004, 02:39 PM   #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?
b00l34n is offline   Reply With Quote
Old 04-23-2004, 02:44 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 04-23-2004, 03:10 PM   #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   Reply With Quote
Old 04-23-2004, 03:33 PM   #4
Registered User
 
Join Date: Apr 2004
Posts: 102
i already tried that, it didn't work?
b00l34n is offline   Reply With Quote
Old 04-23-2004, 03:46 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 04-24-2004, 06:00 AM   #6
Registered User
 
Join Date: Apr 2004
Posts: 102
the code is really really long...
b00l34n is offline   Reply With Quote
Old 04-24-2004, 06:31 AM   #7
mustang benny
 
bennyandthejets's Avatar
 
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   Reply With Quote
Old 04-24-2004, 08:04 AM   #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.
b00l34n is offline   Reply With Quote
Old 04-24-2004, 08:06 AM   #9
Registered User
 
Join Date: Jan 2003
Posts: 648
Code:
send(clientSocket, outMessage.c_str(), sizeof(outMessage.c_str()), 0);
Speedy5 is offline   Reply With Quote
Old 04-24-2004, 08:36 AM   #10
Registered User
 
Join Date: Apr 2004
Posts: 102
already tried that, still got the error
b00l34n is offline   Reply With Quote
Old 04-24-2004, 08:39 AM   #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   Reply With Quote
Old 04-24-2004, 09:04 AM   #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
HybridM is offline   Reply With Quote
Old 04-24-2004, 09:08 AM   #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?
b00l34n is offline   Reply With Quote
Old 04-24-2004, 09:13 AM   #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
HybridM is offline   Reply With Quote
Old 04-24-2004, 09:26 AM   #15
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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