[英]Cross-platform socket send, buffer const void* on linux const char* on windows, best way to handle?
I am porting a program which uses posix (BSD) sockets to windows (using winsock). 我正在将使用posix(BSD)套接字的程序移植到Windows(使用Winsock)。 This is going ok, but I have come up against an issue with send
(I think recv
might have the same issue). 可以,但是我遇到了send
的问题(我认为recv
可能有同样的问题)。 On Linux this function has the signature (see here ): 在Linux上,此函数具有签名(请参见此处 ):
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
on windows it is (see here ): 在Windows上(参见此处 ):
int send(
_In_ SOCKET s,
_In_ const char *buf,
_In_ int len,
_In_ int flags
);
The first place I have a problem is in a destructor like this: 我首先遇到的问题是在这样的析构函数中:
ExtSocketHandler::~ExtSocketHandler(void)
{
if (pUS->GetSock() >= 0) {
uint8_t u = ES_ABORT;
// ignore result
(void)send(pUS->GetSock(), (void *)&u, sizeof(u), send_flags);
}
SAFEDELETE(pUS);
}
where the line 哪里行
(void)send(pUS->GetSock(), (void *)&u, sizeof(u), send_flags);
results in a compiler error on windows as void*
cannot be cast to const char*
. 导致Windows上的编译器错误,因为void*
无法转换为const char*
。
Now I suppose I can wrap all calls to send in an #ifdef
for windows, but is this the best way to handle this? 现在,我想可以包装所有调用以在Windows中发送#ifdef
,但这是处理此问题的最佳方法吗? Is there a more elegant way? 有没有更优雅的方式? There are quite a few calls to send
and recv
to deal with. 有很多电话要send
和recv
。
将其强制转换为const char *
,应在两个平台上均进行编译
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.