server.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include "readLine.h"

Include dependency graph for server.c:

Go to the source code of this file.

Defines

#define BOOL   int
#define TRUE   0
#define FALSE   1
#define PORT   7777
#define BACKLOG   10

Functions

int comunicacao (int socket)
void sigchld_handler (int s)
int main (int argc, char *argv[])

Variables

int done [2]


Define Documentation

#define BACKLOG   10

Definition at line 22 of file server.c.

#define BOOL   int

Definition at line 16 of file server.c.

#define FALSE   1

Definition at line 18 of file server.c.

#define PORT   7777

Definition at line 21 of file server.c.

#define TRUE   0

Definition at line 17 of file server.c.


Function Documentation

int comunicacao ( int  socket  ) 

Definition at line 26 of file server.c.

00026                            {
00027   char tmp[1];
00028   char *buff = NULL;
00029   unsigned int size = 0;
00030   unsigned int i = 0;
00031 
00032   while(recv(socket,&tmp,1,(int)NULL) != 0){
00033     size++;
00034     if((buff=(char*)realloc(buff,sizeof(char)*size))==NULL){
00035       perror("realloc");
00036       close(socket);
00037       write(done[1],"0",1); /* Major failure, stop the server */
00038       exit(EXIT_FAILURE);
00039     }
00040     buff[size]='\0';
00041     buff[i++]=tmp[0];
00042     if(tmp[0]=='\n') break;
00043   }
00044   if(send(socket,buff,size,(int)NULL) < 0){
00045     perror("send");
00046     close(socket);
00047     free(buff);
00048     exit(EXIT_FAILURE); /* Apesar de ser um erro e inutilizar o servidor na maioria das vezes...eh apenas na maioria das vezes */
00049   }
00050   if(strncmp(buff,"STOP SERVER\n",strlen("STOP SERVER\n"))==0){
00051     if(send(socket,"STOPING\n",strlen("STOPING\n"),MSG_DONTWAIT) < 0){
00052       perror("send");
00053     }
00054     write(done[1],"0",1);
00055   }
00056   free(buff);
00057 
00058   close(socket);
00059   return 0;
00060 }

int main ( int  argc,
char *  argv[] 
)

Definition at line 71 of file server.c.

00071                                 {
00072   struct sockaddr_in server, client;
00073   int serverSock, clientSock;
00074   socklen_t clientSize = 0;
00075   pid_t clientPid;
00076   struct sigaction sa;
00077   BOOL stop = FALSE;
00078   char tmp;
00079   tmp='1';
00080 
00081   memset(&server,'\0',sizeof(struct sockaddr_in));
00082   memset(&client,'\0',sizeof(struct sockaddr_in));
00083 
00084   if(pipe(done) < 0){
00085     perror("pipe");
00086     exit(EXIT_FAILURE);
00087   }
00088   
00089   if(fcntl(done[0],F_SETFL,O_NONBLOCK) < 0)
00090     perror("fcntl");
00091   if(fcntl(done[1],F_SETFL,O_NONBLOCK) < 0)
00092     perror("fcntl");
00093 
00094   if((serverSock = socket(AF_INET,SOCK_STREAM,0))==0){
00095     perror("socket");
00096     exit(EXIT_FAILURE);
00097   }
00098 
00099   if(fcntl(serverSock,F_SETFL,O_NONBLOCK) < 0)
00100     perror("fcntl");
00101 
00102   server.sin_family = AF_INET;
00103   server.sin_port = htons(PORT);
00104   server.sin_addr.s_addr = INADDR_ANY;
00105 
00106   if(bind(serverSock,(struct sockaddr *)&server,sizeof(struct sockaddr)) < 0){
00107     perror("bind");
00108     exit(EXIT_FAILURE);
00109   }
00110 
00111   if(listen(serverSock,BACKLOG) < 0){
00112     perror("bind");
00113     exit(EXIT_FAILURE);
00114   }
00115 
00116   clientSize = sizeof(client);
00117 
00118   sa.sa_handler = sigchld_handler;
00119   sigemptyset(&sa.sa_mask);
00120   sa.sa_flags = SA_RESTART;
00121   if(sigaction(SIGCHLD,&sa,NULL) < 0){
00122     perror("sigaction");
00123     exit(EXIT_FAILURE);
00124   }
00125 
00126   while(stop==FALSE){
00127     read(done[0],&tmp,1);
00128     if(tmp=='0') stop=TRUE;
00129     tmp='1';
00130 
00131     if((clientSock = accept(serverSock,(struct sockaddr*)&client,&clientSize)) < 0){
00132       if(errno!=EAGAIN || errno!=EWOULDBLOCK)
00133         perror("accept");
00134     }
00135     if(clientSock > 0){
00136       clientPid = fork();
00137       if((int)clientPid < 0 ){
00138         perror("fork");
00139       }
00140       if((int)clientPid == 0){ /* Se este for o cliente */
00141         close(serverSock);
00142         comunicacao(clientSock);
00143         exit(0); /* Saimos do cliente */
00144       
00145       }else{      
00146         close(clientSock); /* O servidor nao precisa disso */
00147         read(done[0],&tmp,1);
00148         if(tmp=='0') stop=TRUE;
00149         tmp='1';
00150       }
00151     }
00152   }
00153   close(serverSock);
00154   exit(EXIT_SUCCESS);
00155 }

void sigchld_handler ( int  s  ) 

Definition at line 62 of file server.c.

00062                            {
00063   int childStatus;
00064   while(waitpid(-1,&childStatus,WNOHANG) > 0);
00065   if(WIFEXITED(childStatus)){
00066     if(WEXITSTATUS(childStatus)!=0)
00067       perror("child");
00068   }
00069 }


Variable Documentation

int done[2]

Definition at line 24 of file server.c.


Generated on Wed Apr 30 11:09:59 2008 for EchoServer by  doxygen 1.5.5