问题描述
我想在Windows上的插座上使用fprintf中。许多在线的例子是。等效code为Windows是什么,我问在这里。
I'm trying to use fprintf on a socket on Windows. Many of the online examples are UNIX examples. The equivalent code for Windows is what I'm asking about here.
最后,我想做到这一点:
Ultimately, I'd like to do this:
fprintf(file_handle_to_socket, "hello world\r\n");
我不希望使用WriteFile的,snprintf的或其他任何东西。只是fprintf中()。
I do not want to use WriteFile, snprintf or anything else. Just fprintf().
伪code或多或少显示的步骤将是有益的。
Pseudocode that more or less shows the steps would be helpful.
下面是我到目前为止有:
Here is what I have so far:
unsigned short port = enter_port;
int result = 0;
WSADATA wsaData;
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
struct sockaddr_in local;
memset(&local, 0, sizeof(struct sockaddr_in));
local.sin_port = htons(port);
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET)
{
fprintf(stdout,"invalid socket: error code %d\n", WSAGetLastError());
}
result = bind(sock, (struct sockaddr*)&local, sizeof(struct sockaddr_in));
if(result == SOCKET_ERROR)
{
fprintf(stdout,"bind() failed: error code %d\n", WSAGetLastError());
}
result = listen(sock, 5);
struct sockaddr peer;
memset(&peer, 0, sizeof(struct sockaddr));
SOCKET client = 0;
int size = sizeof(struct sockaddr);
client = accept(sock, &peer, &size);
int OSFileHandle = _open_osfhandle(client, _O_APPEND);
FILE * file_handle_to_socket = fdopen(OSFileHandle, "w+");
fprintf(file_handle_to_socket, "Hello World\r\n");
// hoping this helps -- it doesn't
fflush(file_handle_to_socket);
fclose(file_handle_to_socket);
closesocket(client);
// this throws an exception
WSACleanup();
当我运行这个,没事出来在另一端(使用腻子),我在WSACleanup得到一个异常();在WSACleanup唯一的例外是无效的句柄指定
。
When I run this, nothing comes out on the other end (using putty) and I get an exception at WSACleanup(); The exception at WSACleanup is An invalid handle was specified
.
更新我发现通过的的。
如果我更换:
UPDATE I found a strong lead by reading this post more closely.If I replace:
fprintf(file_handle_to_socket, "Hello World\r\n");
与
fprintfsock(client, "Hello World\r\n");
它的工作原理。
这仍然不是一个完整的回答我的问题虽然。我期待直接使用fprintf中()。
It's still not a complete answer to my question though. I'm looking to use fprintf() directly.
推荐答案
一个套接字不是一个文件句柄,所以你不能使用 _open_osfhandle()
来包装它。这就是为什么 fprintf中()
不工作。 fprintfsock()
是专门设计直接与插座的工作。它仅仅是将数据格式转换成一个本地缓冲区的包装,然后使用套接字API 发送()
函数来发送。如果你真的想使用 fprintf中()
用插座,你将不得不和#undef fprintf中
然后 fprintf中的#define()
如果您使用的是编译器重新映射它 fprintfsock()
(将只工作了支持复杂的宏)。否则,只要使用 fprintfsock()
直接
A socket is not a file handle, so you cannot use _open_osfhandle()
to wrap it. That is why fprintf()
is not working. fprintfsock()
is specifically designed to work with a socket directly. It is merely a wrapper that formats the data into a local buffer and then uses the socket API send()
function to send it. If you really want to use fprintf()
with a socket, you will have to #undef fprintf
and then #define fprintf()
to re-map it to fprintfsock()
(which will only work if you are using a compiler that supports variadic macros). Otherwise, just use fprintfsock()
directly.
这篇关于在Windows套接字上fprintf中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!