473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

STDIN and STDOUT in C

Hi,
Can the 'stdin' and 'stdout' streams point to a Network socket? 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?

thanks,
Nov 17 '05 #1
1 1891
"Pravin Prabhu" <Pr**********@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
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.
thanks,


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
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
3
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
1
by: BenjaMinster | last post by:
I want to read and write unicode on stdin and stdout. I can't seem to find any way to force sys.stdin.encoding and sys.stdout.encoding to be utf-8, so I've got the following workaround: import...
2
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.