# Include <stdarg. h>
# Include <stdio. h>
# Include <signal. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <errno. h>
# Include <sys/time. h>
# Include <sys/Wait. H>
# Include <sys/select. h>
Void sig_child (){
Pid_t PID;
Int status;
While (pid = waitpid (-1, & status, wnohang)> 0)
Printf ("% d terminated! /N ", pid );
// While (wait (& Status)> 0 );
}
Void dealtcp (int s, struct sockaddr_in * remote ){
Int Len;
Time_t Ti;
Char buff [128];
Printf ("connection from: % s: % d/N", inet_ntoa (Remote-> sin_addr), ntohs (Remote-> sin_port ));
Ti = Time (null );
// Snprintf (buff, sizeof (buff), "% S/R/N", ctime (& Ti ));
While (LEN = Recv (S, buff, sizeof (buff), 0)> 0 ){
Buff [Len] = '/0 ';
Send (S, buff, strlen (buff), 0 );
Bzero (buff, sizeof (buff ));
}
}
Int main (INT argc, char ** argv ){
Struct sockaddr_in remote;
Char * hname, * sname;
Int S, C, Len, FD, S1, N;
Pid_t PID;
Fd_set rset, allset;
Int client [1024];
Int I, Maxi, maxfd, nready;
Char buff [1024];
If (argc <2)
Error (1, 0, "parameters less than 2 ");
Else if (argc = 2 ){
Hname = NULL;
Sname = argv [1];
}
Else if (argc = 3 ){
Hname = argv [1];
Sname = argv [2];
}
S = server (hname, sname, "TCP ");
S1 = server (hname, sname, "UDP ");
Signal (sigchld, sig_child );
Len = sizeof (struct sockaddr );
Maxfd = s> S1? S: S1;
While (1 ){
Fd_set (S, & rset );
Fd_set (S1, & rset );
If (nready = select (maxfd + 1, & rset, null) <0)
Error (1, 0, "select Error !! ");
If (fd_isset (S, & rset )){
If (C = accept (S, (struct sockaddr *) & remote, & Len) <= 0 ){
Error (1, errno, "Accept error! ");
}
If (Fork ()> 0 ){
Close (C );
}
Else {
Close (s );
Dealtcp (C, & remote );
Exit (0 );
}
}
If (fd_isset (S1, & rset )){
N = recvfrom (S1, buff, sizeof (buff), 0, (struct sockaddr *) & remote, & Len );
Sendto (S1, buff, N, 0, (struct sockaddr *) & remote, sizeof (remote ));
}
}
Close (s );
Close (S1 );
Exit (0 );
}