简体   繁体   中英

sending uchar* over winsock2

I have a uchar buffer that needs to be sent. It can't be converted to char.

I have a sample project that casts:

send(SOCKET s, void *buf, size_t length) { return send(s, buf, length, 0);

My attempt is:

err = send( ConnectSocket, (void*)buf, (int)strlen(sendbuf), 0 );

The only thing I know to be different is the version of VS(2008).

That line will not compile in my 2010 project. The error is cannot convert paramerter 2 void* to const char *

尝试这个:

err = send( ConnectSocket, (char*)buf, (int)strlen(sendbuf), 0 );

Since send wants a const char* and not just a char* , you cannot just pass a plain void* , as C++ doesn't support implicit casting to/from void* . So you have to explicitly convert it to char* or const char* :

err = send( ConnectSocket, (const char*)buf, (int)strlen(sendbuf), 0 );    

Keep in mind that casting the pointer doesn't magically change the signedness of the data in your buffer, because send doesn't care about any signedness and just copies raw data.

But as to why it worked with VS2008 I don't know, because C++ doesn't cast to/from void* implicitly by standard. Maybe send was defined differently (I doubt that) or the rules were just laxed (non-standard-conformant). Or maybe you compiled it as C, which in turn casts to/from void* implicitly, by standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM