Guest User

Untitled

a guest
Oct 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. /*
  2.  *  clstub.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <string.h>
  11.  
  12. #include "prototipo.h"
  13. #include "strustub.h"
  14.  
  15. char buffcom[ MAX_DATA ];
  16. CLSVBUFF clsvbuff;
  17.  
  18. void error(char *msg)
  19. {
  20.     perror(msg);
  21.     //exit(0);
  22. }
  23.  
  24. /*
  25.  *INTERFASE CON CAPA TRANSPORTE
  26. */
  27.  
  28. static int send_rcv( int s, CLSVBUFF *p, int opcode, int qty )
  29.   {
  30.   int qtyrec;
  31.   p->opc = opcode;
  32.   send_packet( s, p, qty + sizeof( OPC ) );
  33.   qtyrec = receive_packet( s, p, sizeof( *p ) );
  34.   return qtyrec - sizeof( OPC );
  35.   }
  36.  
  37. //Emisión
  38. int send_packet( int sockfd, const void *p, int qty )
  39.     {
  40.     int n;
  41.     memcpy( buffcom, p, qty );
  42.     n = write(sockfd,buffcom,qty);
  43.     if (n < 0)
  44.          error("ERROR al escribir socket");
  45.     bzero(buffcom, MAX_DATA);
  46.     return n;
  47.     }
  48.  
  49. //Recepción
  50. int receive_packet( int sockfd, void *p, int lim )
  51.     {
  52.     int n;
  53.     n = read(sockfd,buffcom,lim);
  54.     if (n < 0)
  55.          error("ERROR al leer socket");
  56.     else
  57.     {
  58.     memcpy( p, buffcom, n );
  59.     bzero(buffcom, MAX_DATA);
  60.     }
  61.     return n;
  62.     }
  63.  
  64. //INICIALIZACION
  65. int sockcl_init(char *arg1, char *arg2)
  66.     {
  67.     int sockfd, portno, n;
  68.  
  69.     struct sockaddr_in serv_addr;
  70.     struct hostent *server;
  71.  
  72.     portno = atoi(arg2);
  73.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  74.     if (sockfd < 0)
  75.         error("ERROR opening socket");
  76.     server = gethostbyname(arg1);
  77.     if (server == NULL) {
  78.         fprintf(stderr,"ERROR, no such host\n");
  79.         return(-1);
  80.     }
  81.     bzero((char *) &serv_addr, sizeof(serv_addr));
  82.     serv_addr.sin_family = AF_INET;
  83.     bcopy((char *)server->h_addr,
  84.          (char *)&serv_addr.sin_addr.s_addr,
  85.          server->h_length);
  86.     serv_addr.sin_port = htons(portno);
  87.     if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
  88.         error("ERROR connecting");
  89.  
  90.     //Una vez conectado, pruebo escribir y leer.
  91.     n = write(sockfd,"Hola Server..",13);
  92.     if (n < 0)
  93.          error("ERROR writing to socket");
  94.      n = read(sockfd,buffcom,MAX_DATA);
  95.      if (n < 0)
  96.     error("ERROR reading from socket");
  97.      else
  98.     printf("mensaje: %s\n",buffcom);
  99.      bzero(buffcom, MAX_DATA);
  100.  
  101.    return sockfd;
  102. }
  103.  
  104. /*
  105.  *CAPA MIDDLEWARE (STUB)
  106.  */
  107.  
  108.  
  109. int rmtread( int s, char *pathname, void *data, int qty, int binicio )
  110.   {
  111.   CLSV_RREAD *pc;
  112.   SVCL_RREAD *ps;
  113.  
  114.   int status;
  115.  
  116.   pc = &clsvbuff.data.clsv_rread;
  117.   ps = &clsvbuff.data.svcl_rread;
  118.  
  119.   strcpy(pc->pathname, pathname);
  120.   pc->qty = qty;
  121.   pc->binicio = binicio;
  122.   send_rcv( s, &clsvbuff, RREAD, sizeof( CLSV_RREAD ) );
  123.  
  124.   status = ps->status;
  125.   if( status > 0 )
  126.     memcpy( data, ps->data, status );
  127.   return status;
  128.   }
  129.  
  130. int rmtwrite( int s, char *pathname, const void *data, int qty )
  131.   {
  132.   CLSV_RWRITE *pc;
  133.   SVCL_RWRITE *ps;
  134.  
  135.   pc = &clsvbuff.data.clsv_rwrite;
  136.   ps = &clsvbuff.data.svcl_rwrite;
  137.  
  138.   strcpy(pc->pathname, pathname);
  139.   pc->qty = qty;
  140.   if( qty > 0 )
  141.     memcpy( pc->data, data, qty );
  142.   send_rcv( s, &clsvbuff, RWRITE, sizeof( CLSV_RWRITE ) );
  143.  
  144.   return ps->status;
  145.   }
Add Comment
Please, Sign In to add comment