Go to the documentation of this file.00001 #include <sys/types.h>
00002 #include <sys/socket.h>
00003 #include <netinet/in.h>
00004 #include <netdb.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007
00008 #define TAMMSG 80
00009
00010 int main()
00011 {
00012 int sock;
00013 struct sockaddr_in server;
00014
00015 struct sockaddr client;
00016 socklen_t sc = sizeof(struct sockaddr);
00017
00018 int j;
00019
00020 int rval, i=0, f, s0;
00021 char msg[TAMMSG],porta[16] ;
00022
00023
00024
00025 sock = socket(AF_INET, SOCK_STREAM, 0);
00026 if (sock < 0) {
00027 perror("Abrindo stream socket");
00028 exit(1);
00029 }
00030
00031 printf("Porta:"); fflush(stdin);
00032 scanf("%15s", porta);
00033 printf("Servidor esperando mensagens na porta .%s.\n",porta);
00034
00035 server.sin_family = AF_INET;
00036 server.sin_addr.s_addr = INADDR_ANY;
00037 server.sin_port = htons(atoi(porta));
00038 if (bind(sock, (struct sockaddr *)&server, sizeof server ) < 0) {
00039 perror("Binding stream socket");
00040 exit(1);
00041 }
00042
00043
00044
00045
00046 listen(sock,10);
00047
00048
00049 while (1) {
00050
00051 s0 = accept(sock,&client,&sc);
00052
00053 do {
00054
00055 rval = recv(s0, msg, TAMMSG, 0) ;
00056 msg[rval] = 0;
00057 printf("%d: %s",i,msg);
00058 i++;
00059 } while (msg[0] != '.' && rval != 0);
00060
00061 close(s0);
00062 return 0;
00063 }
00064
00065 close(sock);
00066
00067 }