Redirect IO of process to Windows socket

前端 未结 4 534
轻奢々
轻奢々 2021-01-14 02:50

I am new to winsock, I tried to write a server socket that accepts new connection, then it calls an external executable file. How can we redirect the stdin and stdout of the

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 03:44

    Overlapped sockets (such as created with socket()) cannot be used directly for I/O redirection.

    When launching the external process, use CreatePipe() instead to create anonymous pipes for the redirected STDIN/OUT/ERR handles of the process, then use ReadFile() and WriteFile() (or equivilents) with send() and recv() to manually proxy the data between the socket and the process yourself via the pipes.

    In other words, when data arrives to the socket, read the data from the socket and write it to the STDIN pipe. When the process outputs data, read from the STDOUT/ERR pipes and write it to the socket.

提交回复
热议问题