The UNIX and Linux Forums  


Go Back   UNIX e Linux Forum > Inizio Forum > Di programmazione ad alto livello
.
google unix.com



Di programmazione ad alto livello Pubblica domande su C, C + +, Java, SQL, e di altri linguaggi di programmazione qui.

Più di UNIX e Linux Forum Argomenti potreste trovare utili
Filo Thread Starter Forum Risposte Ultimo Post
programmi client e server JCR Di programmazione ad alto livello 8 04-28-2009 05:06 PM
Client / Server Issue geauxtn AIX 5 07-30-2007 09:38 AM
client / server Bole UNIX for Dummies Domande & Risposte 2 10-30-2006 02:19 AM
ntp server e client ntp bubba112557 SUN Solaris 1 05-10-2005 11:37 AM
Tubi e Named pipe (FIFO) è la dimensione del buffer Jus Filesystem, memoria e dischi 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 Thread Tools Cerca in questo Thread Rate Thread Modalità di visualizzazione
  #1 (permalink)  
Old 08-23-2008
tolkki tolkki is offline
Utente Registrato
  
 

Iscriviti Data: marzo 2008
Interventi: 7
client / server tubi

qui è il concetto:

il client legge un percorso da standard input e scrive a pipe1.The server legge questo percorso dal pipe1 e cerca di aprire il file reading.If per il server è in grado di aprire il file, il server risponde con la lettura e la scrittura di file è a pipe2, altrimenti il server scrive un messaggio di errore per la stessa pipe.The cliente quindi dalla legge e scrive pipe2 riceve per quanto riguarda il livello di uscita

qui è il mio codice:


Codice:
#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);
    }

}

In realtà, mi sembra di capire che ci potrebbero essere alcuni problemi con condizioni di gara che è il motivo per cui ho messo 2 comandi sonno, ma ancora non funziona ..
generalmente i wanna sychronize il leggere e scrivere i comandi del client e server, ma non so come ...
se qualcuno ha il tempo di vedere il codice e darmi qualche consiglio, che sarà veramente apprezzato ..... grazie in anticipo ..
  #2 (permalink)  
Old 08-23-2008
redoubtable redoubtable is offline
Utente Registrato
  
 

Join Date: Aug 2008
Posizione: Portogallo
Interventi: 242
Wow! Ecco alcuni brutto codice che hai lì. Ecco la versione di lavoro:

Codice:
#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);
    }

}

Ci sono molte cose sbagliate. SD per trovare gli errori.

Hope it helps!
  #3 (permalink)  
Old 08-24-2008
tolkki tolkki is offline
Utente Registrato
  
 

Iscriviti Data: marzo 2008
Interventi: 7
grazie aiutato molto!
In realtà, ho un altro problema in questo momento ...
Sto cercando di fare il comando cat <nomefile> | wc-l

bene, sembra essere un problema nella lettura della riga dal buffer


Codice:
//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);
   }
}

uno sguardo, se è possibile ... thx in anticipo ...
Closed Thread

Segnalibri

Thread Tools Cerca in questo Thread
Cerca in questo Thread:

Ricerca Avanzata
Modalità di visualizzazione Vota questo thread
Vota questo thread:

Distacco regolamento
Tu non può post nuovo thread
Tu non può inviare una risposta
Tu non può postare allegati
Tu non può modificare i tuoi post

BB codice è Su
Smilies sono Su
[IMG] codice Su
Codice HTML è Chiuso
Trackbacks sono Su
Pingbacks sono Su
Refbacks sono Su




Tutti gli orari sono GMT -4. La data di oggi è 02:33 PM.


Powered by: vBulletin, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Traduzioni Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
UNIX e Linux Forum Content Copyright © 1993-2009. Tutti i diritti Reserved.Ad di gestione da RedTyger

Contenuti pertinenti URL da vBSEO 3.2.0