• 网络服务器处理阻塞问题


    最近客户采用采用多个进程来同时对snmp服务器来set/get  ,有时候会造成网络服务器 net-snmp 的阻塞而无服务器反馈信息

    为了改善,自己写了一个简单服务器,为了提高处理效果,创建子进程来处理,并由父进程来回收

    代码如下:

    头文件:

    socket_include.h

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    

      

    server.c

    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <termios.h>
    #include <string.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <sys/shm.h>
    #include "socket_include.h"
    
    #define MAXLINE 80
    #define SERV_PORT 1234
    
    
    struct serialdata{
    	int cur[10][24];
    	int mincur[10][24];
    	int maxcur[10][24];
    	int minTcur[10][3];
    	int maxTcur[10][3];
    	int minTvol[10][3];
    	int maxTvol[10][3];
    	int minTH[10][8];
    	int maxTH[10][8];
    	int Tcur[10][3];
    	int Tvol[10][3];
    	int temp[10][4];
    	int hum[10][4];
    	int water[10];
    	int smoke[10];
    	int door[10][2];
    	int kwh[10][24];
    	int Tpowerfactor[10][3];
    	int Tkwh[10][3];
    	int swicth[10][24];
    	int outputnum[10];
    	int online[10];
    	int type[10];
    	int m_s;
    	int alramflag[10][24+3+3+9];//output cur vol th
    	////////////////////////////////////////////////
    	int numperswicth[6];
    	int ondelaytime;
    	int offdelaytime;
    	char slavename[10][32];
    	char outputname[10][24][32];
    
    };
    
    struct serialdata *memdata;
    struct serialdata *shmdata()
    {
    	key_t shmkey;
    	int shmid;
    	shmkey=ftok("/",'b');
    	shmid=shmget(shmkey,sizeof(struct serialdata)*2,0666|IPC_CREAT);
    	if(shmid==-1)
    		return NULL;
    	struct serialdata * addr;
    		addr=(struct serialdata*)shmat(shmid,0,0);
    	if(addr==(struct serialdata*)-1)
    		return NULL;
    	return addr;
    }
    
    
    void handler(int num) {   
        //我接受到了SIGCHLD的信号啦   
        int status;   
        int pid = waitpid(-1, &status, WNOHANG);   
        if (WIFEXITED(status)) {   
            printf("The child %d exit with code %d
    ", pid, WEXITSTATUS(status));   
        }   
    }   
    
    int main()
    {
    	memdata = NULL;
    	memdata = shmdata();
    	if(memdata==NULL)
    	{
    		return -1;
    	}
    	//网络服务
    	
    	pid_t pid;
    	struct sockaddr_in servaddr, cliaddr;
        socklen_t cliaddr_len;
        int listenfd, connfd;
        char buf[MAXLINE];
        char str[INET_ADDRSTRLEN];
        int i, n;
    	int index=0;
    	int outindex=0;
    	
    	char cmd1[256];
    	char show[255];
    	
    	
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(SERV_PORT);
        
        bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    
        listen(listenfd, 20);
    
        printf("Accepting connections ...
    ");
        while (1) {
            cliaddr_len = sizeof(cliaddr);
            connfd = accept(listenfd, 
                    (struct sockaddr *)&cliaddr, &cliaddr_len);
         
            n = read(connfd, buf, MAXLINE);//读取网络数据
    		
    		buf[n]='';
            printf("received from %s at PORT %d,buf:%s
    ",
             inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
             ntohs(cliaddr.sin_port),buf);
    		
    		signal(SIGCHLD, handler); 
    	
    		pid=fork();
            if(pid<0)
            {
    			 
                  perror("fork error");
    			  
            }
            else if(pid==0)//子进程处理数据
            {
    			
    			
    		for(index=0;index<10;index++)
    		{
    			for(outindex=0;outindex<24;outindex++)
    				{
    						memset(cmd1,0,sizeof(char)*255);
    						sprintf(cmd1,"OFF %d %d",index,outindex+1);
    						if(strcmp(cmd1,buf)==0)
    						{
    							printf("is in readcmd succed!
    ");
    							char sworder[255];
    							
    							//关输出位
    							for(n=0;n<2;n++)
    							{
    								memdata->swicth[index][outindex]=1;
    								memset(sworder,0,sizeof(char)*255);
    								printf("
    ");
    								sprintf(sworder,"/clever/bin/order %d %d 2 1",index,outindex+1);
    								system(sworder);
    								printf("the off order is dnoe.
    ");
    								printf("
    ");
    							}
    							break;
    						}
    						
    						memset(cmd1,0,sizeof(char)*255);
    						sprintf(cmd1,"ON %d %d",index,outindex+1);
    						if(strcmp(cmd1,buf)==0)
    						{
    							printf("is in readcmd succed!
    ");
    							
    							
    							//关输出位
    							char sworder[255];
    							for(n=0;n<2;n++)
    							{
    								memdata->swicth[index][outindex]=2;
    								memset(sworder,0,sizeof(char)*255);
    								printf("
    ");
    								sprintf(sworder,"/clever/bin/order %d %d 1 1",index,outindex+1);
    								system(sworder);
    								printf("the on order is dnoe.
    ");
    								printf("
    ");
    							}
    							break;
    						}
    						
    						memset(cmd1,0,sizeof(char)*255);
    						sprintf(cmd1,"status %d %d",index,outindex+1);
    						if(strcmp(cmd1,buf)==0)
    						{
    							memset(show,0,sizeof(char)*255);
    							sprintf(show,"status:%s
    ",memdata->swicth[index][outindex]==0?"--":
    									(memdata->swicth[index][outindex]==1?"OFF":"ON"));
    							write(connfd, show, strlen(show));
    							break;
    						}
    				}
    		}
    		//处理完成
    			
    			
    			 write(connfd, "exit sucessed!
    ", strlen("exit sucessed!
    "));
    			close(connfd);//短连接结束
                exit(0);
    		}
    		
    		
    
    /*
    		for (i = 0; i < n; i++)
                buf[i] = toupper(buf[i]);
    		write(connfd, buf, n);
    */       
    	   //write(connfd, "exit sucessed!
    ", strlen("exit sucessed!
    "));
    	  // close(connfd);
    		
        }
    	
    	
    	return 0;
    	
    	
    }
    

    客户端: 

    #include "socket_include.h"
    
    #define MAXLINE 80
    #define SERV_PORT 1234
    
    int main(int argc, char *argv[])
    {
        struct sockaddr_in servaddr;
        char buf[MAXLINE];
        int sockfd, n;
        char *str;
        
        if (argc != 2) {
            fputs("usage: ./client message
    ", stderr);
            exit(1);
        }
        str = argv[1];
        
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        //inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
        inet_pton(AF_INET, "192.168.1.239", &servaddr.sin_addr);
        servaddr.sin_port = htons(SERV_PORT);
        
        connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    
        write(sockfd, str, strlen(str));
    
        n = read(sockfd, buf, MAXLINE);
    	buf[n]='';
        printf("Response from server:%s
    ",buf);
    
        //write(STDOUT_FILENO, buf, n);
    
        close(sockfd);
        return 0;
    }
    

      

    测试程序:

    #include <stdio.h>
    
    int main()
    {
    	int i=0;
    	for(i=0;i<100;i++)
    	{
    		printf("./client 'ON 0 9'
    ");
    		system("./client 'ON 0 9'");
    		sleep(1);
    		
    		system("./client 'status 0 9'");
    		sleep(1);
    		
    		printf("./client 'OFF 0 9'
    ");
    		system("./client 'OFF 0 9'");
    		sleep(1);
    		
    		system("./client 'status 0 9'");
    		sleep(1);
    		
    	}
    
    
    
    	return 0;
    }
    

      

  • 相关阅读:
    JAXB基本使用
    Spring MVC手札
    Oracle创建表空间和表
    oracle删除表,让整个表从数据库中彻底消失
    oracle创建用户操作
    查看运行时某个java对象占用JVM大小及通过idea查看java的内存占用情况
    如何查看java进程
    win10下查看进程,杀死进程
    js计算两个时间相差天数
    windows 端口号占用和解决方法
  • 原文地址:https://www.cnblogs.com/hongzhunzhun/p/5698916.html
Copyright © 2020-2023  润新知