472,362 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

named pipe problem on linux

I have a simple test to pass information from a client to a server
using named pipe. what I really want is: when I type a line on the client,
the server will output the line immediately. but to my surprise, I always
have to terminate the client to get the server in action, i.e. prints out
what I typed.

anything I missed? I am compiling using gcc without any option.

thanks,
---RICH

---------------------
server
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);

while(1)
{
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 10, fp);
printf("Received string: %s\n", readbuf);
fclose(fp);
}

return(0);
}

-------
client
-------
#include <stdio.h>
#include <stdlib.h>

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}

while( scanf("%s", str) != EOF )
{ fputs(str, fp);
fputs(str, stdout);
}

fclose(fp);
return(0);
}

Jul 22 '05 #1
5 17250

"richard" <by****@sbcglobal.net> wrote in message
news:pa****************************@sbcglobal.net. ..
I have a simple test to pass information from a client to a server
using named pipe. what I really want is: when I type a line on the client,
the server will output the line immediately. but to my surprise, I always
have to terminate the client to get the server in action, i.e. prints out
what I typed.

anything I missed? I am compiling using gcc without any option.


Sorry but this question is off-topic on c.l.c++. Try
news:comp.unix.programmer

Sharad

Jul 22 '05 #2
["Followup-To:" header set to comp.os.linux.]
richard enlightened us with:
I have a simple test to pass information from a client to a server
using named pipe.
Using C. But posting to a C++ group. Make up your mind, but at least
don't crosspost.
anything I missed?


Try flushing.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Jul 22 '05 #3
richard wrote:
I have a simple test to pass information from a client to a server
using named pipe. what I really want is: when I type a line on the client,
the server will output the line immediately. but to my surprise, I always
have to terminate the client to get the server in action, i.e. prints out
what I typed.

anything I missed? I am compiling using gcc without any option.

thanks,
---RICH

---------------------
server
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);

while(1)
{
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 10, fp);
printf("Received string: %s\n", readbuf);
fclose(fp);
}

return(0);
}

-------
client
-------
#include <stdio.h>
#include <stdlib.h>

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}

while( scanf("%s", str) != EOF )
{ fputs(str, fp);
fputs(str, stdout);
}

fclose(fp);
return(0);
}


<---- updated server: ---->

// named pipe server

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

fprintf(stderr, "Pipe server running...\n");

// try to open my fifo file. it may already exist from
// an earlier aborted execution
fp = fopen(FIFO_FILE, "r");

// if the fopen failed, the fifo file does not exist
if (NULL == fp)
{
// Create the FIFO file
umask(0);
if(mknod(FIFO_FILE, S_IFIFO|0666, 0))
{
fprintf(stderr, "mknod() failed\n");
return 1;
}

fp = fopen(FIFO_FILE, "r"); // now open the fifo file
}

printf("Receiving...\n");

// while we can read (up to 9) chars from the fifo...
// Note: readbuf[] will be nul-terminated and will
// include any newlines read from the pipe. since
// we are reading so few bytes at once (9) it will
// take several iterations of the 'while loop' to read
// any long lines written to the pipe by clients.
while(NULL != fgets(readbuf, 10, fp))
{
// print the string just read to my stdout
printf("%s", readbuf);
fflush(stdout);
}

fclose(fp); // close the fifo file

remove(FIFO_FILE); // delete the fifo file

fprintf(stderr, "Pipe server terminating.\n");

return(0);
}
<---- updated client: ---->

// named pipe client

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

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

// try to open an existing fifo file
if((fp = fopen(FIFO_FILE, "w")) == NULL)
{
perror("fopen");
exit(1);
}

// get up to 19 chars from stdin into str[].
// str[] will be nul-terminated and any newlines in
// the input will be written to the pipe (fp).
while( fgets(str, sizeof(str), stdin) != NULL )
{
// write the chars read from stdin to the pipe
fprintf(fp, "%s", str);
fflush(fp); // flush any buffered IO to the pipe

// echo what I wrote to the pipe to my stdout
fprintf(stdout, "Sent: %s\n", str);
fflush(stdout);
}

fclose(fp); // close the pipe

return(0);
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 22 '05 #4
On Mon, 01 Nov 2004 19:15:35 +0000, Larry I Smith wrote:
richard wrote:
I have a simple test to pass information from a client to a server
using named pipe. what I really want is: when I type a line on the client,
the server will output the line immediately. but to my surprise, I always
have to terminate the client to get the server in action, i.e. prints out
what I typed.

anything I missed? I am compiling using gcc without any option.

thanks,
---RICH

---------------------
server
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);

while(1)
{
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 10, fp);
printf("Received string: %s\n", readbuf);
fclose(fp);
}

return(0);
}

-------
client
-------
#include <stdio.h>
#include <stdlib.h>

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}

while( scanf("%s", str) != EOF )
{ fputs(str, fp);
fputs(str, stdout);
}

fclose(fp);
return(0);
}


<---- updated server: ---->

// named pipe server

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
FILE *fp;
char readbuf[80];

fprintf(stderr, "Pipe server running...\n");

// try to open my fifo file. it may already exist from
// an earlier aborted execution
fp = fopen(FIFO_FILE, "r");

// if the fopen failed, the fifo file does not exist
if (NULL == fp)
{
// Create the FIFO file
umask(0);
if(mknod(FIFO_FILE, S_IFIFO|0666, 0))
{
fprintf(stderr, "mknod() failed\n");
return 1;
}

fp = fopen(FIFO_FILE, "r"); // now open the fifo file
}

printf("Receiving...\n");

// while we can read (up to 9) chars from the fifo...
// Note: readbuf[] will be nul-terminated and will
// include any newlines read from the pipe. since
// we are reading so few bytes at once (9) it will
// take several iterations of the 'while loop' to read
// any long lines written to the pipe by clients.
while(NULL != fgets(readbuf, 10, fp))
{
// print the string just read to my stdout
printf("%s", readbuf);
fflush(stdout);
}

fclose(fp); // close the fifo file

remove(FIFO_FILE); // delete the fifo file

fprintf(stderr, "Pipe server terminating.\n");

return(0);
}
<---- updated client: ---->

// named pipe client

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

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
FILE *fp;

char str[20];

// try to open an existing fifo file
if((fp = fopen(FIFO_FILE, "w")) == NULL)
{
perror("fopen");
exit(1);
}

// get up to 19 chars from stdin into str[].
// str[] will be nul-terminated and any newlines in
// the input will be written to the pipe (fp).
while( fgets(str, sizeof(str), stdin) != NULL )
{
// write the chars read from stdin to the pipe
fprintf(fp, "%s", str);
fflush(fp); // flush any buffered IO to the pipe

// echo what I wrote to the pipe to my stdout
fprintf(stdout, "Sent: %s\n", str);
fflush(stdout);
}

fclose(fp); // close the pipe

return(0);
}

Regards,
Larry

thanks, Larry.
Jul 22 '05 #5
["Followup-To:" header set to comp.os.linux.]
richard enlightened us with:
thanks, Larry.


Geesh, you quoted 183 lines just to write one! Learn how to properly
snip your post!

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Jul 22 '05 #6

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

Similar topics

4
by: Rajarshi Guha | last post by:
Hi, I'm having a little trouble when I read from a named pipe. I create a pipe by os.mkfifo('/tmp/mypipe') and then open it for reading with fin = open('/tmp/mypipe','r+')
0
by: Spiros | last post by:
Hi everybody I am creating an application using VC++ that runs on a terminal server with 30 end users. The end users will use thin clients as front end machines. The application consists of a main...
3
by: EricR | last post by:
I am trying to use .NET to "tap into" a named pipe created by a non .NET 3rd party application. Specifically, the application is a table loading utility. It opens a named pipe and waits for input....
2
by: FB's .NET Dev PC | last post by:
I am writing two services in VB.NET, one of which needs to send text strings to the other. After reading, I decided (perhaps incorrectly) that named pipes would be the best interprocess...
0
by: EricR | last post by:
I am trying to use .NET to "tap into" a named pipe created by a non .NET 3rd party application. Specifically, the application is a table loading utility. It opens a named pipe and waits for input....
0
by: olaf.dietsche | last post by:
Hi, The system is Windows XP and DB2 v8.1.7. I'm trying to load a text file via named pipe into a table. I have two programs: the first program creates the named pipe, waits for a client...
3
by: a | last post by:
Hi everybody, Is it possible to open a named pipe from a php script on Windows? More specifically, I have a Windows service that receives commands through a named pipe, and I'd like to open...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
0
by: jeb2000 | last post by:
I am rather new to pipes, but I have two c# programs that are communicating via a named pipe. This is working fine on two of my XP Pro SP2 machines. However, when I port to a third machine (also XP...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.