I've been working on some sockets programming in Linux and i am trying to do a concurrent proxy server. Basically i want that each client that connects to the proxy server gets encountered with 2 questions. The first one is to introduce the IP of destiny and the 2nd one its the port number. After this information been sent, the proxy server will connect each client to their chosen IP(wich is and echo server).
My question is how and where will i save the 2 questions answered by each user and then, connect them to their echo server that they had chosen.
Here's the part of the code that i think we must work on:
/*============ WARNS THAT SERVER IS READY TO ACCEPT REQUESTS ==================*/
if(listen(sockfd,5)==-1) Abort("Impossible to accept requests");
/*========== STARTS ANSWERING CLIENTS BY CONCURRENT FORM =============*/
cliaddr_len=sizeof(cli_addr);
while(1){
FD_ZERO(&fd_read);
FD_SET(STDIN_FILENO, &fd_read);
FD_SET(sockfd, &fd_read);
fflush(stdin);
n=select(32, &fd_read, NULL, NULL, NULL);
if(n<0)
if( errno!=EINTR){
close(sockfd);
Abort("Error on select");
}else
continue;
if(FD_ISSET(STDIN_FILENO, &fd_read)){
gets(comando);
if(strcmp(comand, "exit")==0){
close(sockfd);
printf("Goodbye...\n");
exit(0);
}
}
if(FD_ISSET(sockfd, &fd_read))
if((newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&cliaddr_len))
==-1)
fprintf(stderr,"<SERV>Impossible to accept clients...\n");
else{
switch(fork()){/*Goes to backgroud*/
case -1:fprintf(stderr,"Impossible to answer...");
close(newsockfd);
break;
case 0:close(sockfd); /* SON */
AnswerClient(newsockfd);
exit(EXIT_SUCCESS);
default:close(newsockfd); /* DAD */
}
}
}
}
/*___________________________ AnswerClient ____________________________________
______________________________________________________________________________*/
void AnswerClient(int sockfd){
static char buffer[BUFFERSIZE];
static unsigned int cont=0U;
int nbytes;
pid_t pid=getpid();
while(1){
/*==================== PROCESS REQUEST ==========================*/
cont++;
switch((nbytes=ReadLine(sockfd,buffer,BUFFERSIZE))){
case -1:fprintf(stderr,"Error receiving data...\n");
return;
case 0:fprintf(stderr,"Client didnt sent data...\n");
return;
default:printf("\n<%d>Message received: %s\n",pid,buffer);
if(!strncmp(buffer,"exit",4)){
printf("<%d>Going shutdown...\n",pid);
close(sockfd);
return;
}
/*============ Sends Confirmation =============*/
sprintf(buffer,"<%d>",pid);
nbytes=strlen(buffer);
if(WriteN(sockfd,buffer,nbytes)!=nbytes)
fprintf(stderr,"Impossible to confirm.\n");
}
}
}
/*_____________________________ ReadLine _______________________________________
Reads a line (until find the caracter '\n') of a socket.
Returns:
-1 : if error
0 : EOF
!= : if read some bytes
______________________________________________________________________________*/
int ReadLine(int sockfd,char* buffer,int maxlen){
int n,rc;
char c;
for(n=0;n<maxlen-1;n++){
if((rc=read(sockfd,&c,1))==1){
*buffer++=c;
if(c=='\n') break;
}else if (rc==0) {
if(n==0) return(0); /*EOF*/
else break; /*EOF but has already read some bytes*/
} else return(-1); /*Error*/
}
*buffer=0;
return(n);
}
/*______________________________ WriteN _______________________________________
Writes n bytes on socket in case. Returns the number of bytes writen.
______________________________________________________________________________*/
int WriteN(int sockfd,char * buffer,int nbytes){
int nleft,nwritten;
nleft=nbytes;
while(nleft>0){
if((nwritten=write(sockfd,buffer,nleft))<=0) return(nwritten);
nleft-=nwritten;
buffer+=nwritten;
}
return(nbytes-nleft);
}
void Abort(char *msg){
fprintf(stderr,"\a<SER1>Fatal error: <%s>\n",msg);
perror("Erro do sistema");
exit(EXIT_FAILURE);
}
void buryZombie()
{
static int status;
wait(&status);
}
Can someone please give me an hint or just show me the right way?
RicardoCosta