The UNIX and Linux Forums  


Go Back   O UNIX e Linux Forum > Top Fóruns > Alto Nível de programação
.
google unix.com



Alto Nível de programação Post perguntas sobre C, C + +, Java, SQL, e outras linguagens de programação aqui.

Mais UNIX e Linux Fórum Tópicos Você pode achar Helpfull
Fio Thread Starter Fórum Respostas Última postagem
programas cliente e servidor JCR Alto Nível de programação 8 04-28-2009 05:06
Cliente / Servidor Emissão geauxtn AIX 5 07-30-2007 09:38
cliente / servidor fuste UNIX para Dummies Perguntas & Respostas 2 10-30-2006 02:19
ntp ntp servidor e cliente bubba112557 Sun Solaris 1 05-10-2005 11:37
Tubos e pipes nomeados (FIFO) tamanho do buffer Jus Filesystems, Discos e Memória 1 08-20-2004 11:14

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 Pesquisar este Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-23-2008
tolkki tolkki is offline
Usuário
  
 

Join Date: Mar 2008
Posts: 7
cliente / servidor tubos

aqui é o conceito:

o cliente lê um caminho a partir da entrada padrão e escreve-lo para pipe1.The servidor lê este caminho a partir do pipe1 e tenta abrir o arquivo para o servidor reading.If pode abrir o arquivo, o servidor responde pela leitura e escrita é o arquivo a pipe2; caso contrário, o servidor escreve uma mensagem de erro para o mesmo cliente pipe.The em seguida lê e escreve a partir da pipe2 que recebe para a saída padrão

aqui é o meu código:

Código:
#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);
    }

}
Na verdade, eu entendo que pode haver alguns problemas com a raça condições que é a razão pela qual eu coloquei 2 comandos sono, mas ainda não funciona ..
geralmente quero sychronize a ler e escrever comandos do cliente e do servidor, mas não sei como ...
se alguém tem o tempo para ver o código e me dar alguns conselhos, vai ser realmente apreciados ..... thanks in advance ..
  #2 (permalink)  
Old 08-23-2008
temível redoubtable is offline
Usuário
  
 

Join Date: Aug 2008
Localização: Portugal
Lugares: 242
Uau! Isso é feio algum código que você tem aí. Aqui está o trabalho versão:
Código:
#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);
    }

}
Há várias coisas que estavam erradas. Diferenciadas de encontrar erros.

Hope it helps!
  #3 (permalink)  
Old 08-24-2008
tolkki tolkki is offline
Usuário
  
 

Join Date: Mar 2008
Posts: 7
graças que me ajudou imenso!
Na verdade, tenho outro problema agora ...
Estou tentando fazer o comando cat <filename> | wc-l

bem, parece haver um problema na leitura da última linha do buffer

Código:
//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);
   }
}
dê uma olhada se puder ... thx in advance ...
Closed Thread

Marcadores

Thread Tools Pesquisar este Thread
Pesquisar este Thread:

Pesquisa Avançada
Display Modes Esta taxa Thread
Esta taxa Thread:

Destacamento Regimento
Você não pode postar novas threads
Você não pode postar respostas
Você não pode postar anexos
Você não pode editar suas postagens

BB code é Ligado
Smilies são Ligado
[IMG] código é Ligado
Código HTML é Desligado
Trackbacks são Ligado
Pingbacks são Ligado
Refbacks são Ligado




Todos os horários são GMT -4. A hora é agora 12:54.


Powered by: vBulletinCopyright © 2000 - 2006, Jelsoft Enterprises Limited. Língua Traduções Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
O UNIX e Linux Fóruns Content Copyright © 1993-2009. Todos os Direitos Reserved.Ad Gestão por RedTyger

Content Relevant URLs por vBSEO 3.2.0