⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 staserver1.txt

📁 该短代码实现了和上位机的通信功能
💻 TXT
字号:

 
#include <stdlib.h>
#include <limits.h>  
#include <errno.h>
#include <stdio.h>
#include <string.h> /*bzero()*/
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>   
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "staserver.h"

#define FIFO_NAME1 "/tmp/my_fifo1"
#define FIFO_NAME2 "/tmp/my_fifo2"

 
 

 char  station_data[DAT_SIZE]; /*data sended to client begin at 1*/
 char  control_info[MSG_SIZE];  /*data received from client begin at 0, control :24-31*/
 char  identify_data[9]={0x4B,0x4A,0x38,0x32,0x56,0x31,0x30,0x91}; /* main station identify */
 char  identify_result=0x55;    /* the result to compare main station identify */


char *point=".";
char returnIP[3];
char ipstring[16]="192.168.000.157";
char maskstring[16]="255.255.255.0";

int main()
     {
     struct sockaddr_in myAddr;		/* Server socket address */
     struct sockaddr_in clientAddr;	/* Socket address for client */
     struct request clientRequest;	/* Request/Message from client */
     int sFd;				/* Server's socket file descriptor */
     //char *inetAddr[INET_ADDR_LEN];	/* Buffer for dot notation * *****************88
                         		 /* internet addr of client */
     char * p = control_info;
     
     int sockAddrSize = sizeof (struct sockaddr_in);/* Size of socket address structure */
     int i;
 
     static char replyMsg[] = "Server received your message";
     
     int pipe_fd1=-1;
     int pipe_fd2=-1;
     int res;
     
     fd_set  readfds;
     struct  timeval tv;    
     
     char openadc[5]={0xaa,0xbb,0xcc};

     pid_t ppid;
     pid_t pidip;

     int ipfd;
     int maskfd;
     ssize_t ipread;
     ssize_t maskread;
     ssize_t ipwrite;
     ssize_t maskwrite;
     off_t newpos;

     unsigned char k;

     ppid=getppid();

   if((ipfd=open("/mnt/jffs2/ipaddr",O_RDWR|O_CREAT|O_EXCL,0644))==-1)
     
       {
	 ipfd=open("/mnt/jffs2/ipaddr",O_RDONLY);
	 if((ipread=read(ipfd,ipstring,15))==-1)
	 {
	   printf("ip read error\n");
	   exit(1);
	 }
	    close(ipfd);
	    
	}
       ipModify();

      if((maskfd=open("/mnt/jffs2/maskaddr",O_RDWR|O_CREAT|O_EXCL,0644))==-1)
     
       {
	 maskfd=open("/mnt/jffs2/maskaddr",O_RDONLY);
	 if((maskread=read(maskfd,maskstring,15))==-1)
	 {
	   printf("mask read error\n");
	   exit(1);
	 }
	    close(maskfd);
	    
	}
    
      maskModify();
  
     bzero(station_data, DAT_SIZE); /*clear the data buffer*/
     bzero(control_info, MSG_SIZE); /*bzero(void *s,int n)*/
     bzero ((char *) &myAddr, sockAddrSize);
     myAddr.sin_family = AF_INET;
     myAddr.sin_port = htons (SERVER_PORT_NUM);
     myAddr.sin_addr.s_addr = htonl (INADDR_ANY);
     
     
    
     /*open two pipe*/
     pipe_fd1 = open(FIFO_NAME1,O_RDONLY);
	if(pipe_fd1==-1)
	{
		printf("open pipe_fd1 failed\n");
		exit(1);
	}
	
	else
	printf("Process %d result %d \n",getpid(),pipe_fd1);
	
	pipe_fd2 = open(FIFO_NAME2,O_WRONLY);
	if(pipe_fd1==-1)
	{
		printf("open pipe_fd1 failed\n");
		exit(1);
	}
	else
	printf("Process %d result %d \n",getpid(),pipe_fd2);
 
   
     if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
       {
 	printf ("socket failed");
 	close (sFd);
 	_exit(1);
       } /*creat socket*/

	
    
  
     if (bind (sFd, (struct sockaddr *) &myAddr, sockAddrSize) == -1)
         {
         printf ("bind failed");
 	close (sFd);
 	_exit(1);
	 }/*bind socked to local address*/
     printf("\n Bind sucessful!\n\n"); 
 
     while(1)
         {
     
           
 	if (recvfrom (sFd, &clientRequest, sizeof (clientRequest), 0,
 		      (struct sockaddr *) &clientAddr,&sockAddrSize) == -1)
             {
             printf ("recvfrom failed");
             close (sFd);
             _exit(1);
             }
          
      
  		
  	for (i=0;i<=7;i++)             
{
  		  		  		
  		if ( clientRequest.reply[i]!=identify_data[i] )
  		{
  			identify_result=0x66;
	
  			
  			break;
  		}	
  		else
  		{
  			identify_result=0x55;			
  		}	 
  		 		
  	}
	printf("\n identify_result  %x,",identify_result);
  	
  
        bcopy((const char *)clientRequest.message, p, MSG_SIZE);  
       
      
        p = control_info;
        
      
	     if (identify_result==0x55 && clientRequest.action == 0x55)
     		{ 
     			 printf(" \n now change output data! \n");
     			 res=write(pipe_fd2,control_info,5);
		         if(res==-1)
		         {
			       printf("write error on pipe \n");
			        exit(1);
		         }      		 
     		} 
     		  
     if (identify_result==0x55 && clientRequest.action == 0x51)
    	{ 
     			 
     		
	       
     		fixipstring(clientRequest.message);
	 
		printf("ipstring is modified now:%s\n",ipstring);
		if((ipfd=open("/mnt/jffs2/ipaddr",O_WRONLY))==-1)
		{  printf("open ipaddr error\n");
		   exit(1);
		}
	
		//    if((newpos=lseek(ipfd,0,SEEK_SET))==-1)
		//    printf("lseek error\n");
		 if(( ipwrite=write(ipfd,ipstring,15))<15)
		 printf("write ipaddr error\n");
		 
		close(ipfd);
		ipModify();  //该子程序放在fixipstring(clientRequest.message)后面就会出错,????
                    		 
     	}     
 	
     if (identify_result==0x55 && clientRequest.action == 0x56)
       {
	 printf(" \n Mask has modified !\n");

	 fixmaskstring(clientRequest.message);

	
		if((maskfd=open("/mnt/jffs2/maskaddr",O_WRONLY))==-1)
		{  printf("write maskaddr error\n");
		   exit(1);
		}
	
		  if((newpos=lseek(maskfd,0,SEEK_SET))==-1)
		   printf("lseek error\n");
		   if(( maskwrite=write(maskfd,maskstring,15))<15)
		   printf("write maskaddr error\n");

		close(maskfd);
		 maskModify();
        
       }

     if (identify_result==0x55 && clientRequest.action == 0x53) 
 	{
	 
	 res=write(pipe_fd2,openadc,5);
		         if(res==-1)
		         {
			       printf("write error on pipe \n");
			        exit(1);
		         }      
         tv.tv_usec = 0;
         tv.tv_sec=1.5;
         FD_ZERO(&readfds);
	
	 FD_SET(pipe_fd1,&readfds);
         select(pipe_fd1+1, &readfds, NULL, NULL, &tv);
	        if(FD_ISSET(pipe_fd1, &readfds)) 
	        {
	             read(pipe_fd1,station_data,42);
	             printf("get the station_data\n");
	             
	        }	
		if (sendto (sFd, station_data, 42, 0,
                         (struct sockaddr *) &clientAddr,sockAddrSize) == -1)
                {
                    printf ("sendto failed");
                    close (sFd);
                    _exit(1);
                 }
        }
      }
     close (sFd); /* Just in case. Should never get here. */
     close(pipe_fd1);
     close(pipe_fd2);
     return 0;
}   

    
void ipModify()
{
  pid_t pid;
  int status,exit_s;
  
  switch(pid=vfork())
 {
    case -1:
      {
	perror("fork failed\n");
        exit(1);
      }break;
 case 0:
   {
      execl("/bin/ifconfig","ifconfig","eth0",ipstring,(char *)0);
      printf("ifconfig is failed\n");
      _exit(1);
   }break;
 default:
   {
      while(waitpid(pid,&status,WNOHANG)==0)
	printf("still waiting...\n");
    
  if(WIFEXITED(status))
    {
      exit_s=WEXITSTATUS(status);
      printf("exit status from %d was %d\n",pid,exit_s);
    }
   }break;
 }
}


void maskModify()
{
  pid_t pid;
  int status,exit_s;
  
  switch(pid=vfork())
 {
    case -1:
      {
	perror("fork failed\n");
        exit(1);
      }break;
 case 0:
   {
      execl("/bin/ifconfig","ifconfig","eth0","netmask",maskstring,(char *)0);
      printf("ifconfig is failed\n");
      _exit(1);
   }break;
 default:
   {
      while(waitpid(pid,&status,WNOHANG)==0)
	printf("still waiting...\n");
    
  if(WIFEXITED(status))
    {
      exit_s=WEXITSTATUS(status);
      printf("exit status from %d was %d\n",pid,exit_s);
    }
   }break;
 }
}



char *int2char(char t)
{
char *i=returnIP;  
char t0,t1,t2;
    
    
    
  if(t>=100)
  {
  t0=t/100+48;
  t1=t%100;
  t2=t1;
  t1=(t1/10)+48;
  t2=t2%10+48;
  }
  else if((t>=10)&&(t<100))
 {
  t0=48;
  t1=t/10+48;
  t2=t%10+48;
  }
  else
{
  t0=48;
  t1=48;
  t2=t+48;
  } 
 


  returnIP[0]=t0;
  returnIP[1]=t1;
  returnIP[2]=t2;
 
      

      //        printf("child%s\n",i);
    return(i);
}

 void fixipstring(char *p)
{
  char *tm=0;     //指针先指向空指针,以防出错
  char *ips;
  char a[16];
 
  unsigned char i;
  ips=a;
  for(i=0;i<4;i++)
{
    tm=int2char(*(p+i));
    if(i==0)
      strcpy(ips,tm);
    else 
    ips=strcat(ips,tm);
    if(i<3)
    ips=strcat(ips,point);
    strcpy(ipstring,ips);
}

}


 void fixmaskstring(char *p)
{
  char *tm=0;     //指针先指向空指针,以防出错
  char *ips;
  char a[16];
  unsigned char i;
  ips=a;
  for(i=0;i<4;i++)
{
    tm=int2char(*(p+i));
    if(i==0)
    strcpy(ips,tm);
    else 
    ips=strcat(ips,tm);
    if(i<3)
    ips=strcat(ips,point);
    strcpy(maskstring,ips);
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -