473,883 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Receive d 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 17427

"richard" <by****@sbcglob al.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("Receive d 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_F ILE, S_IFIFO|0666, 0))
{
fprintf(stderr, "mknod() failed\n");
return 1;
}

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

printf("Receivi ng...\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_FIL E); // 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("Receive d 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_F ILE, S_IFIFO|0666, 0))
{
fprintf(stderr, "mknod() failed\n");
return 1;
}

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

printf("Receivi ng...\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_FIL E); // 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
7468
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
1557
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 menu executeble that opens other executables depending on the user selection. In order to establish that communication I use named pipes. Actually I use 1 named pipe as the main tube for data transfer. What happens is that sometimes the session of...
3
2643
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. Does anybody know how I can write to the named pipe from a .NET program. I have tried using the traditional file access methods, and it appears that pipes have a problem with file streams. I have tried API calls, but since I do not have the...
2
5071
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 communication method for this task. I set about creating, on the pipe's server side, a new thread which would instantiate a blocking inbound message pipe, and the client side which would write to that pipe when needed. Being on the same PC, security is...
0
1219
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. Does anybody know how I can write to the named pipe from a .NET program. I have tried using the traditional file access methods, and it appears that pipes have a problem with file streams. I have tried API calls, but since I do not have the...
0
2917
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 and writes the text file into the named pipe. the second program is doing the db2Load() call.
3
5038
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 the client end of the pipe directly from php rather than creating an intermediate process. Thanks,
14
19850
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
1464
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 Pro SP2), I cannot connect to my named pipe. Are there any simple permission/access issues that need to be considered with named pipes? The pipe is being accessed locally, so I don't need to access it accross a network. The code for creating my...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.