int socket_server()
{
int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
struct sockaddr_in my_addr; /*本机地址信息*/
struct sockaddr_in remote_addr; /*客户端地址信息*/
int sin_size;
charrecv_buf[MAX_BUF_SIZE]={0};
intrecv_size=0;
charsend_buf[MAX_BUF_SIZE]={0};
intsend_size=0;
intpid;
intstatus;
struct timeval tv;
struct in_addr clientaddr ;
charstr[32]={0};
charbuf_log[MAX_BUF_SIZE]={0};
enumstat_actionaction;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
sprintf(buf_log,"socke errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
return 1;
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) {
sprintf(buf_log,"bind errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
return 1;
}
if (listen(sockfd, BACKLOG) == -1) {
sprintf(buf_log,"bind errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
return 1;
}
tv.tv_sec= SOCKET_TIMEOUT_SECOND;
tv.tv_usec=0;
while(1) {
memset(&remote_addr, 0, sizeof(struct sockaddr));
sin_size = sizeof(remote_addr);
if ((client_fd = accept(sockfd, (struct sockaddr *)(&remote_addr), &sin_size)) == -1) {
sprintf(buf_log,"accept errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
continue;
}
//sprintf(buf_log,"received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
if ( NULL== (char*) (inet_ntop(AF_INET,&remote_addr.sin_addr.s_addr,str,31)) )
{
printf("inet_ntop error,errno=%d,desc=%s\r\n",errno,strerror(errno) );
}
sprintf(buf_log,"received a connection from %s\n", str);
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
if (setsockopt(client_fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1)
{
sprintf(buf_log,"warning!setsockopterrno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
}
if ( (pid=fork()) == 0) { /*子进程代码段*/
while(1)
{
memset(recv_buf,0,MAX_BUF_SIZE);
recv_size=recv(client_fd,recv_buf,MAX_BUF_SIZE -1 ,0);
if(recv_size ==-1)
{
sprintf(buf_log,"recv errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
close(client_fd);
exit(1);
}
else if( 0== recv_size)
{
sprintf(buf_log,"connection reset by peer!\r\n");
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
exit(1);
}
else//if (1 == recv_size)
{
action=atoi(recv_buf);
sprintf(buf_log,"recv_buf=%s,recv_size=%d,action=%d\r\n",recv_buf,recv_size,action);
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
memset(send_buf,0,MAX_BUF_SIZE);
if (exec_command(2,action,send_buf) ==-1 )
{
continue;
}
send_buf[ strlen(send_buf)] = 0;
sprintf(buf_log,"begin send...,send_buf=%s\r\n",send_buf);
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
if (send(client_fd, send_buf,strlen(send_buf), 0) == -1) {
sprintf(buf_log,"send errno=%d,desc=%s\r\n",errno,strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
close(client_fd);
exit(1);
}
}
}
}
else if(pid <0)
{
sprintf(buf_log,"socket_server fork errno=%s\n",strerror(errno));
buf_log[strlen(buf_log)]=0;
writelog(g_logfile,buf_log);
}
else
{
//parent;
close(client_fd);
waitpid(pid,&status,0);
}
}
return 0;
}