#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/wait.h>
#define SERV_IP "127.0.0.1"
#define SERV_PORT 8000
void wait_child(int signo)
{
while(waitpid(0, NULL, WNOHANG)>0);
return;
}
int main(int argc,char *argv[])
{
pid_t pid;//Process ID
int sfd, cfd;//Receive the connected sfd and the cfd communicating with the client
struct sockaddr_in serv_addr, clie_addr;//Create server and client structures
socklen_t clie_addr_len;//Client structure length
char buf[BUFSIZ], clie_IP[BUFSIZ];//buf stores received data
int n , i;//Number of data read n, cycle factor i
sfd = Socket(AF_INET, SOCK_STREAM, 0);//Create socket
bzero(&serv_addr, sizeof(serv_addr));//Zero clearing
serv_addr.sin_family = AF_INET;//Set protocol family to IPv4
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //Set the network card to any valid local network card
//inet_pton(AF_INET, SERV_IP, &serv_addr.sin_addr.s_addr);
serv_addr.sin_port = htons(SERV_PORT);//Set port
bind(sfd, (struct sockaddr * )&serv_addr, sizeof(serv_addr));//Set socket to associate with sfd
listen(sfd, 128);//Set the maximum number of incomplete accept. Start listening
while(1)//Loop receive client connection
{
clie_addr_len = sizeof(clie_addr);//Initialize client structure length
cfd = accept(sfd, (struct sockaddr *)&clie_addr, &clie_addr_len);//Receive client connection
printf("client IP:%s, port:%d\n", inet_ntop(AF_INET, &clie_addr.sin_addr.s_addr, clie_IP, sizeof(clie_IP)), ntohs(clie_addr.sin_port));
pid = fork();//Create a new process
if(pid< 0)//error
{
perror("fork error");
exit(1);
}
else if(pid == 0)//Subprocess
{
close(sfd);//Close parent process file descriptor first in child process
break;
}
else if(pid>0)//Parent process
{
close(cfd);//Close child process file descriptor first in parent process
signal(SIGCHLD, wait_child);
}
}
if(pid == 0)//Here is the real business processing part of the subprocess
{
while(1)//Loop client business
{
n = read(cfd, buf, sizeof(buf));
if(n == 0)//If read returns 0, the client is disconnected
{
close(cfd);//Close client file descriptor
return 0;
}
else if(n == -1)//error
{
perror("read error");
exit(1);
}
else
{
write(STDOUT_FILENO, buf, n);
for(i= 0; i< n;++i)
{
buf[i] = toupper(buf[i]);
}
write(cfd, buf, n);
}
}
}
return 0;
}