The UNIX and Linux Forums  

Go Back   A UNIX és Linux Forums > Top Fórumok > Magas szintű Programozás
.
google unix.com



Magas szintű Programozás Post kérdések C, C + +, Java, SQL, és más programozási nyelvek itt.

Több, UNIX és Linux fórum témák Ön által talált Hasznos
Szál Thread Starter Fórum Válaszok Utolsó hozzászólás
kliens és szerver programok JCR Magas szintű Programozás 8 04-28-2009 05:06 PM
Client / Server Kiadás geauxtn AIX 5 07-30-2007 09:38 AM
kliens / szerver fatörzs A UNIX a dummies Kérdések és válaszok 2 10-30-2006 02:19 AM
ntp ntp szerver és kliens bubba112557 SUN Solaris 1 05-10-2005 11:37 AM
Csövek és Named Pipes (FIFO) puffer Ius Fájlrendszerek, lemez és memória 1 08-20-2004 11:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Téma eszközök Keresés a téma Rate Thread Megjelenítési módok
  #1 (permalink)  
Old 08-23-2008
tolkki tolkki is offline
Regisztrált felhasználó
  
 

Join Date: Mar 2008
Hozzászólások: 7
kliens / szerver csövek

Itt a koncepció:

az ügyfél elolvassa az elérési út a standard input és írja meg a szerver pipe1.The olvassa ezt az elérési út pipe1 és megpróbálja megnyitni a fájlt a szerver reading.If tudja nyitni a fájlt, a szerver válaszol olvassuk a fájlt, és azt írásbeli a pipe2, egyébként a szerver hibaüzenetet írja, hogy az azonos pipe.The kliens majd elolvassa a pipe2 és kiírja, hogy megkapja a szabványos kimenet

Itt van a kód:


Kód:
#include <stdio.h>
#include <string.h>
#define maxsize 1000

char buffer [maxsize];

void client(int readfd,int writefd)
{
   puts("give pathname:");
   gets(buffer);

   write(writefd,buffer,strlen(buffer));
   //sleep(10);
   while((read(readfd,buffer,strlen(buffer)))>0);
     printf("%s/n",buffer);

}

void server(int readfd,int writefd)
{
   FILE * fp;
   char line[100];
   //sleep(10);
   read(readfd,buffer,strlen(buffer));
   fp=fopen(buffer,"r");
   if(fp==NULL)
   {
       strcpy(buffer,"cannot oepn file");
       write(writefd,buffer,strlen(buffer));
   }
   else
     while(fgets(line,100,fp)!=NULL)
       write(writefd,line,100);
}

int main()
{
    int pipe1[2],pipe2[2];
    int childpid;
    int status;
    pipe(pipe1);
    pipe(pipe2);
    childpid=fork();
    if(childpid>0)
    {
        close(pipe1[0]);
        close(pipe2[1]);
        client(pipe2[0],pipe1[1]);
        wait(&status);
        exit(0);
    }
    else
    {
        close(pipe1[1]);
        close(pipe2[0]);
        server(pipe1[0],pipe2[1]);
        exit(0);
    }

}

Igazából, már értem, hogy lehet valami probléma verseny feltételeit, ezért tettem 2 alvás parancsokat, de még mindig nem megy ..
általánosan akarok sychronize az írási és olvasási parancsokat a kliens és a szerver, de nem tudom, hogyan ...
ha valaki azt az időt, hogy a kódot, és adjon nekem néhány tanácsot, akkor igazán lehetne ..... thanks in advance ..
  #2 (permalink)  
Old 08-23-2008
félelmetes redoubtable is offline
Regisztrált felhasználó
  
 

Join Date: Aug 2008
Helyszín: Portugália
Posts: 242
Wow! Ez néhány csúnya kód van. Itt a munka verzió:

Kód:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 1000

char buffer [maxsize];
void client(int readfd,int writefd)
{
   printf ("give pathname: ");
   fflush (stdout);
   fgets (buffer, sizeof (buffer), stdin);

   write(writefd,buffer, sizeof (buffer));
   while (read(readfd,buffer,sizeof(buffer)) > 0)
        printf("%s",buffer);
}

void server(int readfd,int writefd)
{
   FILE * fp;
   char line[1000];

   read(readfd,buffer,sizeof(buffer));
   if (strchr (buffer, '\n'))
        *strchr(buffer, '\n') = '\0';
   fp=fopen(buffer,"r");
   if(fp==NULL)
   {
       strcpy(buffer,"cannot open file");
       write(writefd,buffer,strlen(buffer));
   }
   else
     while(fgets(line,sizeof(line),fp)!=NULL)
       write(writefd,line,sizeof(line));
}

int main()
{
    int pipe1[2],pipe2[2];
    int childpid;
    int status;
    pipe(pipe1);
    pipe(pipe2);
    childpid=fork();
    if(childpid>0)
    {
        close(pipe1[0]);
        close(pipe2[1]);
        client(pipe2[0],pipe1[1]);
        wait(&status);
        exit(0);
    }
    else
    {
        close(pipe1[1]);
        close(pipe2[0]);
        server(pipe1[0],pipe2[1]);
        exit(0);
    }

}

Volt néhány dolog rossz. Diff megtalálni a hibákat.

Hope it helps!
  #3 (permalink)  
Old 08-24-2008
tolkki tolkki is offline
Regisztrált felhasználó
  
 

Join Date: Mar 2008
Hozzászólások: 7
köszönöm, hogy segített egy csomó!
Valójában, van egy másik prob most ...
Én arra törekedjünk, hogy a parancs macska <fájlnév> | wc-l

Nos, úgy tűnik, a probléma az olvasás és az utolsó sorban a puffer


Kód:
//implementation of cat <filename> | wc -l

#include <stdio.h>
#include <string.h>
#define size 500
int main(int argc,char * argv[])
{

   int pipe1[2];
   int childpid;
   FILE * fp;
   char buffer[size];
   int status;
   int lines;
   if(argc!=2)
   {
     printf("usage : mycat <file name>\n");
     exit(1);
   }
   pipe(pipe1);
   childpid=fork();
   if(childpid>0)
   {
      close(pipe1[0]);
      fp=fopen(argv[1],"r");
      if(fp==NULL)
      {
         printf("Cannot open file");
         exit(1);
      }
     while(fgets(buffer,sizeof(buffer),fp)!=NULL)
       write(pipe1[1],buffer,strlen(buffer));

      fclose(fp);
      wait(&status);
      close(pipe1[1]);
      exit(0);
   }
   else
   {
      close(pipe1[1]);
      lines=0;
       while (read(pipe1[0],buffer,sizeof(buffer)) > 0)
           lines++;
      printf("%d",lines);
      close(pipe1[0]);
      exit(0);
   }
}

Nézze meg, ha tudsz ... thx előre ...
Closed Thread

Könyvjelzõk

Téma eszközök Keresés a téma
Keresés a téma:

Részletes keresés
Megjelenítési módok Rate this thread
Rate this thread:

Posting szabályzat
Ön nem post new threads
Ön nem post válaszok
Ön nem post Csatolmányok
Ön nem szerkeszteni az üzeneteidet

BB kód van Be
Smilies vannak Be
[IMG] kód Be
HTML kód Ki
Trackbacks vannak Be
Pingbacks vannak Be
Refbacks vannak Be




Minden idő GMT -4. Az idő most 07:56 PM.


Powered by: vBulletin, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Nyelvre lefordítva Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
A UNIX és Linux Fórum Tartalom Copyright © 1993-2009. Minden jog Reserved.Ad menedzsment RedTyger

Content Relevant URLs by vBSEO 3.2.0