Recently, customers adopt multiple processes to Set/get SNMP servers at the same time, which can cause network server NET-SNMP blocking without server feedback.
In order to improve, I wrote a simple server, in order to improve the processing effect, create a child process to process, and by the parent process to recycle
The code is as follows:
Header file:
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 1234struct 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) {//I have received the signal of SIGCHLD int status; int pid = Waitpid ( -1, &status, Wnohang); if (wifexited (status)) {printf ("the child%d Exit with code%d\n", PID, Wexitstatus (status)); }} int main () {memdata = Null;memdata = Shmdata (); if (memdata==null) {return-1;} Network SERVICE 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 ... \ n"); while (1) {Cliaddr_len = sizeof (CLIADDR); CONNFD = Accept (LISTENFD, (struct sockaddr *) &cliaddr, &cliaddr_len); n = Read (CONNFD, buf, MAXLINE);//Read network data buf[n]= ' + '; printf ("received from%s at PORT%d,buf:%s\n", 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)//child process processing data {for (index=0;index<10;index++) {for (outindex=0;outindex<24;outindex++) {Memse T (cmd1,0,sizeof (char) *255); sprintf (CMD1, "OFF%d%d", index,outindex+1); if (strcmp (CMD1,BUF) ==0) {printf ("is Readcmd succed!\n "), char sworder[255];//off output bit for (n=0;n<2;n++) {Memdata->swicth[index][outindex]=1;memset ( Sworder,0,sizeof (char) *255);p rintf ("\ n"); spriNTF (Sworder, "/clever/bin/order%d%d 2 1", index,outindex+1), System (Sworder);p rintf ("The Off Order is dnoe.\n");p rintf ( "\ n");} break;} memset (cmd1,0,sizeof (char) *255); sprintf (Cmd1, "on%d%d", index,outindex+1); if (strcmp (CMD1,BUF) ==0) {printf ("are in Readcmd succed!\n ");//off Output bit char sworder[255];for (n=0;n<2;n++) {Memdata->swicth[index][outindex]=2;memset ( Sworder,0,sizeof (char) *255);p rintf ("\ n"), sprintf (Sworder, "/clever/bin/order%d%d 1 1", index,outindex+1); System ( Sworder);p rintf ("The Order is dnoe.\n");p rintf ("\ n");} 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\n", memdata->swicth[index][outindex]==0?) --":(memdata->swicth[index][outindex]==1?" OFF ":" on "); Write (CONNFD, show, strlen (show)); Processing completed write (CONNFD, "Exit sucessed!\n", strlen ("Exit sucessed!\n")); Close (CONNFD);//Short connection end exit (0);} /*for (i = 0; i < n; i++) Buf[i] = ToUpper (buF[i]); write (CONNFD, buf, n); *///write (CONNFD, "Exit sucessed!\n", strlen ("Exit sucessed!\n"); Close (CONNFD); }return 0;}
Client:
#include "socket_include.h" #define MAXLINE 80#define serv_port 1234int Main (int argc, char *argv[]) { struct sockaddr _in servaddr; Char Buf[maxline]; int SOCKFD, n; char *str; if (argc! = 2) { fputs ("Usage:./client message\n", 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\n", buf); Write (Stdout_fileno, buf, n); Close (SOCKFD); return 0;}
Test procedure:
#include <stdio.h>int main () {int i=0;for (i=0;i<100;i++) {printf ("./client ' on 0 9 ' \ n"); system ("./client ' on 0 9 Sleep (1), System ("./client ' status 0 9 '"), sleep (1);p rintf ("./client ' off 0 9 ' \ n"); system ("./client ' off 0 9 '"); sleep (1); System ("./client ' status 0 9 '"); sleep (1);} return 0;}
Network server handles blocking issues