Named Pipes | ich verzweifle

Post Reply
Message
Author
andreas80
Posts: 54
Joined: 22. May 2001 14:00
Location: Stp

Named Pipes | ich verzweifle

#1 Post by andreas80 »

Hi,

Ich hab ein Client/Server Programm. Der Server erstellt eine Named Pipe np_logger und Strings die dort hineingeschrieben werden auf den Bildschirm ausgeben. Das Hineinschreiben erledigt das Programm client. Naja zumindest sollte es das tun. Wenn ich den Server starte mit ./logger & und anschliessend ein PS mache dann terminiert der Server. Warum ??? Wenn ich den Server starte und dann den Client dann kommt auch nichts auf am Bildschirm (obwohl ein Test ausgegeben werden sollte).

Irgendwas mach ich falsch.....

Andreas


/********************************************************
* Module: server *
* Autor : *
* Zweck : *
* Datum : *
* Bsp.Nr: *
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
//#inlcude <sys/stat.h>

volatile static FILE *pNamedPipe = (FILE *) 0;

void Handler(int nSignal) // Schiesse NamedPipe
{
(void) fclose((FILE *) pNamedPipe);
exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
char szFileName[100];

(void) signal(SIGINT, Handler);
(void) signal(SIGTERM, Handler);

while(1)
{
if (mkfifo("np_logger",0666) == -1) // Erstelle NamedPipe
{
while (fgets(szFileName,sizeof(szFileName),(FILE *) pNamedPipe) != NULL)
{
fprintf(stdout,"Reading from Pipe: %s",szFileName);
}
pNamedPipe = (FILE *) 0; // Pipe ist nicht mehr geöffnet
}
return 0;
}
}

--------------------------------------------------------------------------------------------

/********************************************************
* Module: client *
* Autor : *
* Zweck : *
* Datum : *
* Bsp.Nr: *
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
//#inlcude <sys/stat.h>

static FILE *pNamedPipe = (FILE *) 0;

int main(int argc, char **argv)
{
char szFileName[100];

if ((pNamedPipe = fopen("np_logger","w")) == (FILE *) 0)
{
fprintf(stdout,"Can't open named pipe");
}
if(fprintf(pNamedPipe,"Test") <0)
{
fprintf(stdout,"Can't write to pipe");
}
(void) fclose((FILE *) pNamedPipe);
exit(EXIT_SUCCESS);
}

quaak

Re: Named Pipes | ich verzweifle

#2 Post by quaak »

schaue in die manpage dazu
mkfifo erstellt nur ne named pipe
dann mussu die mit pNamedPipe=fopen("np_logger","r");
öffnen und dann kannsu erst mit fgets davon lesen!!!
ebenfalls beim Client da aber
fopen("np_logger","w");
vieleicht mussu das append flag bei fopen noch angeben.
cyas quaak

Udo Maslo

Re: Named Pipes | ich verzweifle

#3 Post by Udo Maslo »

http://www.pronix.de/ckurs/ckurs184.html

Spendiere Deinem Programm aber bitte ein ordentliches Arbeitsverzeichnis, ich meine das anlegen von Dateien mit feststehendem Namen unter /tmp ist grober Unfug und nur für Testzwecke duldbar. (Auch wenn sie YaST.tdir, .X11-unix oder sonstwie heißen!)

Post Reply