2

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.

Community
  • 1
  • 1
mpromonet
  • 11,326
  • 43
  • 62
  • 91

2 Answers2

0

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • I think this is a wrong assertion because _open_osfhandle return 3 and not -1. – mpromonet Jun 19 '14 at 21:47
  • In recent versions of Windows (XP onwards), all TCP/UDP socket handles are file handles. That wasn't always true – in old Windows versions (before XP), it was false. But that was over 20 years ago now–unless you need to support 20+ year old Windows versions, you can forget about it. – Simon Kissane Jan 03 '23 at 08:18
0

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);
mpromonet
  • 11,326
  • 43
  • 62
  • 91