Pravin Prabhu said:
Can the 'stdin' and 'stdout' streams point to a Network socket?
Yes, they can.
I have a socket connection from Windows to UNIX, can I set
the two streams to point to the socket connection to send/receive
data?
If there was a slogan to go with Win32, it would be "everything is a
handle". In fact a socket can be used as handle. Since WriteFile() takes a
handle, you can use a socket in place of a file handle. That's useful
because I just stepped through the runtime source code and found that
printf() (which is what I think you have in mind) eventually winds its way
to WriteFile().
When I tried the obvious hack, WriteFile() failed with an "invalid
parameter" error. That's because the runtime performs only synchronous I/O.
By default, sockets under Windows are created for "overlapped" (aka
asynchronous) I/O. Overlapped I/O operations require a pointer to a
structure that tracks the I/O. Since the runtime doesn't pass one, Win32
issues the error.
So, what you have to do is to insure that the socket is created for
"non-overlapped" (aka synchronous) operations.
In my little hack, I had to toss the call to socket() in favor of one to
WSASocket() because the former creates sockets that use overlapped I/O while
the latter can create either overlapped or non-overlapped sockets. This line
sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0);
creates a socket for non-overlapped I/O.
I used this litte hack
------------------------------------------------------------------------
int main()
{
int fd;
FILE *fp;
SOCKET sock;
WSADATA wsa;
if ( WSAStartup( MAKEWORD(2,2), &wsa) == 0 )
{
// Connects to port 9999 on local machine
sock = ConnectMe("127.0.0.1", 9999);
if ( sock != INVALID_SOCKET )
{
// Output using standard Winsock
send(sock, "This is line 1\r\n", 16, 0);
send(sock, "This is line 2\r\n", 16, 0);
// Redirect standard out to the socket
fd = _open_osfhandle( (intptr_t) sock, _O_APPEND);
fp = _fdopen(fd, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
// Output hack to standard out
printf("This is line 3\r\n");
printf("This is line 4\r\n");
// Make sure it gets there
shutdown(sock, SD_BOTH);
closesocket(sock);
WSACleanup();
}
}
return 0;
}
------------------------------------------------------------------------
to connect to port 9999 on my machine. The two lines that call the WinSock
function send() transmit data as do the two printf(). But printf() only
works after the stanard output device is redirected by the four lines above
the first call.
You are welcome.
I didn't play with the inbound stream, I'm just not as curious as I was when
I started investigating do what you want.
That's in large part due to the fact that I think it is a big hack.
I know that the pipe provides the "component object model" on 'nix <GD&R>
but I think that the technique was hackneyed even back in the day. Besides,
the console and the network have different semantics - when was the last
time you worried about a "broken" connection or a timeout to a console? But
for anyone who writes to the network those _are_ concerns.
In fact the first time I tried running my hack not under the debugger, I saw
no output on the receiving side. That's because the sender was exiting
before the data was transmitted. That's the reason for the calls to
shutdown() and closesocket().
To sum up, yes, you can do what you want. No, I don't think it is a good
idea.
Regards,
Will