Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SISTC T2 - Cliente / Servidor Sockets
- No cliente pode indicar o endereço IP 127.0.0.1 ("localhost") */
- // Apple Xcode
- // main.c
- // servidor
- #include <stdio.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- //inet_ntoa
- #include <arpa/inet.h>
- int main (int argc, const char * argv[])
- {
- int ns, s;
- unsigned int clilen;
- struct sockaddr_in serv_addr, cli_addr;
- FILE *fp;
- int teste;
- if (argc != 2)
- {
- printf("Usage: %s port_number\n", argv[0]);
- exit(1);
- }
- s = socket(PF_INET, SOCK_STREAM, 0);
- if (s < 0)
- {
- perror("socket");
- exit(1);
- }
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(atoi(argv[1]));
- if (bind(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- {
- perror("bind");
- exit(1);
- }
- listen(s, 5);
- clilen = sizeof(cli_addr);
- while(1)
- {
- printf("waiting connection\n");
- ns = accept(s, (struct sockaddr *) &cli_addr, &clilen);
- printf("connection from %s\n", inet_ntoa(*((struct in_addr *) &(cli_addr.sin_addr) )));
- fp = fdopen(ns, "r+");
- if (fp == NULL)
- {
- perror("fdopen");
- close(ns);
- return 1;
- }
- // exemplo de transferencia de dados
- while(1)
- {
- if (fread(&teste, sizeof(teste), 1, fp) < 1)
- {
- perror("read");
- break;
- }
- else
- {
- printf("valor recebido: %d\n", teste);
- }
- }
- // nao esquecer de fechar o socket
- fclose(fp);
- }
- return 0;
- }
- /*
- http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html
- 11.1.1 Streams and File Descriptors
- When you want to do input or output to a file, you have a choice of two basic mechanisms for
- representing the connection between your program and the file: file descriptors and streams.
- File descriptors are represented as objects of type int, while streams are represented as
- FILE * objects.
- File descriptors provide a primitive, low-level interface to input and output operations.
- Both file descriptors and streams can represent a connection to a device (such as a terminal),
- or a pipe or socket for communicating with another process, as well as a normal file.
- But, if you want to do control operations that are specific to a particular kind of device,
- you must use a file descriptor; there are no facilities to use streams in this way. You must
- also use file descriptors if your program needs to do input or output in special modes, such
- as nonblocking (or polled) input (see File Status Flags).
- Streams provide a higher-level interface, layered on top of the primitive file descriptor
- facilities. The stream interface treats all kinds of files pretty much alike—the sole exception
- being the three styles of buffering that you can choose (see Stream Buffering).
- The main advantage of using the stream interface is that the set of functions for performing
- actual input and output operations (as opposed to control operations) on streams is much richer
- and more powerful than the corresponding facilities for file descriptors. The file descriptor
- interface provides only simple functions for transferring blocks of characters, but the stream
- interface also provides powerful formatted input and output functions (printf and scanf) as well
- as functions for character- and line-oriented input and output.
- Since streams are implemented in terms of file descriptors, you can extract the file descriptor
- from a stream and perform low-level operations directly on the file descriptor. You can also
- initially open a connection as a file descriptor and then make a stream associated with that
- file descriptor.
- In general, you should stick with using streams rather than file descriptors, unless there is some
- specific operation you want to do that can only be done on a file descriptor. If you are a beginning
- programmer and are not sure what functions to use, we suggest that you concentrate on the formatted
- input functions (see Formatted Input) and formatted output functions (see Formatted Output).
- If you are concerned about portability of your programs to systems other than GNU, you should also
- be aware that file descriptors are not as portable as streams. You can expect any system running
- ISO C to support streams, but non-GNU systems may not support file descriptors at all, or may only
- implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor
- functions in the GNU library are included in the POSIX.1 standard, however.
- OUTROS:
- man 2 read
- man 2 write
- http://www.gnu.org/software/libc/manual/html_node/Connections.html
- http://www.unixprogram.com/socket/socket-faq.html
- http://www.unixprogram.com/
- Index GNU LIBC -> http://www.gnu.org/software/libc/manual/html_node/index.html
- */
- //
- // main.c
- // cliente
- #include <stdio.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- int main (int argc, const char * argv[])
- {
- int port;
- struct hostent *hostinfo;
- if (argc != 3)
- {
- printf("Usage: %s server_ip_address port_number\n", argv[0]);
- exit(1);
- }
- hostinfo = gethostbyname(argv[1]);
- port = atoi(argv[2]);
- struct sockaddr_in name;
- int s;
- s = socket (PF_INET, SOCK_STREAM, 0);
- if (s < 0)
- {
- perror("socket");
- exit(1);
- }
- memset(&name, 0, sizeof(name));
- name.sin_family = AF_INET;
- name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
- name.sin_port = htons (port);
- if (connect(s, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) < 0)
- {
- perror("connect");
- exit(1);
- }
- // exemplo de transferencia de dados
- int teste;
- for(teste = 0; teste < 10; ++teste)
- {
- sleep(1);
- write(s, &teste, sizeof(int));
- //printf("%d\n",teste);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement