fprintf on socket using mingw

2019-08-01 15:52发布

问题:

There is an interesting post Use fprintf on a socket in Windows that advise to replace fprintf with a wrapper that send a formated buffer to the socket.

However, it seems possible to use _open_osfhandle to convert the socket to a filedescriptor.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <windows.h>

int main(int argc, char* argv[])
{
    if (argc < 3) 
    {
        fprintf(stderr,"usage %s <hostname> <port>\n", argv[0]);
        exit(0);
    }

    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    int port = atoi(argv[2]);
    struct hostent *server = gethostbyname(argv[1]);
    if (server == NULL) 
    {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(struct sockaddr_in));
    memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr,  server->h_length);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1)
    {
        perror("socket");
        exit(0);
    }

    int result = connect(sock, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in));
    if(result == -1)
    {
        perror("connect");
        exit(0);
    }
    int OSFileHandle = _open_osfhandle(sock, _O_APPEND);
    FILE * fd = fdopen(OSFileHandle, "w+");
    if (fd == NULL)
    {
        perror("fdopen");
        exit(0);
    }   
    fprintf(fd, "Hello World\r\n");
    fclose(fd);

    closesocket(sock);
    WSACleanup();
}

Using this code to connect to a server (for instance nc -l -p ) doesnot work. Nothing is received, netcat just exit.

回答1:

MSDN documentation says The socket that is created will have the overlapped attribute as a default

Using WSASocket in order to create a socket without the WSA_FLAG_OVERLAPPED flag solve the problem :

int sock = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);


回答2:

I would guess that the problem is that sock is a socket handle, not a file handle. According to the MSDN docs, _open_osfhandle only works for file handles and not any other type of handle.



标签: c sockets mingw