Advertisement
paulogp

SISTC T2 - Cliente / Servidor Sockets

Jul 13th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. /* SISTC T2 - Cliente / Servidor Sockets
  2. No cliente pode indicar o endereço IP 127.0.0.1 ("localhost") */
  3.  
  4. // Apple Xcode
  5.  
  6.  
  7. //  main.c
  8. //  servidor
  9.  
  10. #include <stdio.h>
  11. #include <netinet/in.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. //inet_ntoa
  16. #include <arpa/inet.h>
  17.  
  18.  
  19. int main (int argc, const char * argv[])
  20. {
  21.     int ns, s;
  22.     unsigned int clilen;
  23.     struct sockaddr_in serv_addr, cli_addr;
  24.  
  25.     FILE *fp;
  26.     int teste;
  27.  
  28.     if (argc != 2)
  29.     {
  30.         printf("Usage: %s port_number\n", argv[0]);
  31.         exit(1);
  32.     }
  33.  
  34.     s = socket(PF_INET, SOCK_STREAM, 0);
  35.     if (s < 0)
  36.     {
  37.         perror("socket");
  38.         exit(1);
  39.     }
  40.  
  41.     memset(&serv_addr, 0, sizeof(serv_addr));
  42.     serv_addr.sin_family = AF_INET;
  43.     serv_addr.sin_addr.s_addr = INADDR_ANY;
  44.     serv_addr.sin_port = htons(atoi(argv[1]));
  45.  
  46.     if (bind(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  47.     {
  48.         perror("bind");
  49.         exit(1);
  50.     }    
  51.  
  52.     listen(s, 5);
  53.  
  54.     clilen = sizeof(cli_addr);
  55.     while(1)
  56.     {
  57.         printf("waiting connection\n");  
  58.         ns = accept(s, (struct sockaddr *) &cli_addr, &clilen);  
  59.         printf("connection from %s\n", inet_ntoa(*((struct in_addr *) &(cli_addr.sin_addr) )));
  60.  
  61.         fp = fdopen(ns, "r+");
  62.         if (fp == NULL)
  63.         {
  64.             perror("fdopen");
  65.             close(ns);
  66.             return 1;
  67.         }
  68.  
  69.         // exemplo de transferencia de dados
  70.         while(1)
  71.         {
  72.             if (fread(&teste, sizeof(teste), 1, fp) < 1)
  73.             {
  74.                 perror("read");
  75.                 break;    
  76.             }
  77.             else
  78.             {    
  79.                 printf("valor recebido: %d\n", teste);
  80.             }
  81.         }
  82.  
  83.         // nao esquecer de fechar o socket
  84.         fclose(fp);
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
  90.  
  91. /*
  92.  http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html
  93.  
  94.  11.1.1 Streams and File Descriptors
  95.  
  96.  When you want to do input or output to a file, you have a choice of two basic mechanisms for
  97.  representing the connection between your program and the file: file descriptors and streams.
  98.  File descriptors are represented as objects of type int, while streams are represented as
  99.  FILE * objects.
  100.  
  101.  File descriptors provide a primitive, low-level interface to input and output operations.
  102.  Both file descriptors and streams can represent a connection to a device (such as a terminal),
  103.  or a pipe or socket for communicating with another process, as well as a normal file.
  104.  But, if you want to do control operations that are specific to a particular kind of device,
  105.  you must use a file descriptor; there are no facilities to use streams in this way. You must
  106.  also use file descriptors if your program needs to do input or output in special modes, such
  107.  as nonblocking (or polled) input (see File Status Flags).
  108.  
  109.  Streams provide a higher-level interface, layered on top of the primitive file descriptor
  110.  facilities. The stream interface treats all kinds of files pretty much alike‚Äîthe sole exception
  111.  being the three styles of buffering that you can choose (see Stream Buffering).
  112.  
  113.  The main advantage of using the stream interface is that the set of functions for performing
  114.  actual input and output operations (as opposed to control operations) on streams is much richer
  115.  and more powerful than the corresponding facilities for file descriptors. The file descriptor
  116.  interface provides only simple functions for transferring blocks of characters, but the stream
  117.  interface also provides powerful formatted input and output functions (printf and scanf) as well
  118.  as functions for character- and line-oriented input and output.
  119.  
  120.  Since streams are implemented in terms of file descriptors, you can extract the file descriptor
  121.  from a stream and perform low-level operations directly on the file descriptor. You can also
  122.  initially open a connection as a file descriptor and then make a stream associated with that
  123.  file descriptor.
  124.  
  125.  In general, you should stick with using streams rather than file descriptors, unless there is some
  126.  specific operation you want to do that can only be done on a file descriptor. If you are a beginning
  127.  programmer and are not sure what functions to use, we suggest that you concentrate on the formatted
  128.  input functions (see Formatted Input) and formatted output functions (see Formatted Output).
  129.  
  130.  If you are concerned about portability of your programs to systems other than GNU, you should also
  131.  be aware that file descriptors are not as portable as streams. You can expect any system running
  132.  ISO C to support streams, but non-GNU systems may not support file descriptors at all, or may only
  133.  implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor
  134.  functions in the GNU library are included in the POSIX.1 standard, however.
  135.  
  136.  OUTROS:
  137.  man 2 read
  138.  man 2 write
  139.  
  140.  http://www.gnu.org/software/libc/manual/html_node/Connections.html
  141.  http://www.unixprogram.com/socket/socket-faq.html
  142.  http://www.unixprogram.com/
  143.  
  144.  Index GNU LIBC -> http://www.gnu.org/software/libc/manual/html_node/index.html
  145.  */
  146.  
  147.  
  148. //
  149. //  main.c
  150. //  cliente
  151.  
  152. #include <stdio.h>
  153. #include <netinet/in.h>
  154. #include <netdb.h>
  155. #include <unistd.h>
  156. #include <stdlib.h>
  157. #include <string.h>
  158.  
  159. int main (int argc, const char * argv[])
  160. {
  161.     int port;
  162.     struct hostent *hostinfo;
  163.  
  164.     if (argc != 3)
  165.     {
  166.         printf("Usage: %s server_ip_address port_number\n", argv[0]);
  167.         exit(1);
  168.     }
  169.  
  170.     hostinfo = gethostbyname(argv[1]);
  171.     port = atoi(argv[2]);
  172.  
  173.     struct sockaddr_in name;
  174.     int s;
  175.  
  176.     s = socket (PF_INET, SOCK_STREAM, 0);
  177.     if (s < 0)
  178.     {
  179.         perror("socket");
  180.         exit(1);
  181.     }
  182.  
  183.     memset(&name, 0, sizeof(name));
  184.     name.sin_family = AF_INET;
  185.     name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
  186.     name.sin_port = htons (port);
  187.  
  188.     if (connect(s, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) < 0)
  189.     {
  190.         perror("connect");
  191.         exit(1);
  192.     }
  193.  
  194.     // exemplo de transferencia de dados
  195.     int teste;
  196.     for(teste = 0; teste < 10; ++teste)
  197.     {
  198.         sleep(1);
  199.         write(s, &teste, sizeof(int));
  200.         //printf("%d\n",teste);
  201.     }
  202.  
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement