📄 proc_func.c
字号:
#include "sock_func.h"
#include "proc_func.h"
int process_request( int sockfd )
{
int rval ;
struct PG_HEAD pg_head ;
char * ptr ;
int should_return ;
FILE * fp ;
char buff[1024] ;
char * ptrBuff ;
struct DATA_BIN data_bin ;
ptr = NULL ;
for ( ; ; )
{
rval = socket_recv( sockfd , &pg_head , &ptr ) ;
if ( rval < 0 )
{
rval = socket_send( sockfd , NG_PACKAGE , NULL ) ;
if ( rval < 0 )
{
should_return = 1 ; /*should stop this linking*/
}
}
/*In case that : this linking has been killed*/
else if ( rval == 1 )
{
should_return = 1 ; /*should stop this linking*/
}
/* success */
else
{
switch( pg_head.reqType )
{
case DATA_PACKAGE :
/* get the data from client */
/* should save ptr and give response to client */
rval = socket_send( sockfd , OK_PACKAGE , NULL ) ;
should_return = 1 ; /*should stop this linking*/
break ;
case ENQ_PACKAGE :/* client want data*/
/*In case of this package has been processed*/
if(pg_head.binType == 1) /* bin mode */
{
fp = fopen("data.bin","rb");
if ( fp == NULL )
{
TraceLog_str("Can not find any stored data(data.bin not exist).");
}
else
{
fseek( fp , 0, SEEK_SET ) ;
memset( buff , '\0' , 1024 );
memset( &data_bin , '\0' , sizeof(data_bin) ) ;
/* read a line every time until EOF */
while( !feof( fp ) )
{
if ( fread(&data_bin,sizeof(data_bin),1,fp) == 0 )
break ;
memcpy( buff , data_bin.byte , data_bin.w_len);
rval = socket_send( sockfd , DATA_PACKAGE , buff ) ;
if ( rval < 0 ) break ;
memset( &data_bin , '\0' , sizeof(data_bin) ) ;
memset( buff , '\0' , 1024 );
}
}
TraceLog_str("Read from data.bin successfully .");
fclose(fp) ;
should_return = 1 ; /*should stop this linking*/
rval = socket_send( sockfd , OK_PACKAGE , NULL ) ;
}
else if(pg_head.binType == 0) /* text mode */
{
fp = fopen("data.txt","rt");
if ( fp == NULL )
{
TraceLog_str("Can not find any stored data(data.txt not exist).");
}
else
{
fseek( fp , 0, SEEK_SET ) ;
memset( buff , '\0', 1024 ) ;
while ( fgets(buff,1023, fp) != NULL )
{
rval = socket_send( sockfd , DATA_PACKAGE , buff) ;
if ( rval < 0 ) break ;
memset( buff , '\0', 1024 ) ;
}
}
TraceLog_str("Read from data.txt successfully.");
fclose(fp) ;
should_return = 1 ; /*should stop this linking*/
rval = socket_send( sockfd , OK_PACKAGE , NULL ) ;
}
else { printf("error_2");}
break ;
case OK_PACKAGE :
should_return = 1 ; /*should stop this linking*/
break ;
case NG_PACKAGE :
should_return = 1 ; /*should stop this linking*/
break ;
case CLEAR_PACKAGE :
TraceLog_clear(pg_head.binType ) ;
rval = socket_send( sockfd , OK_PACKAGE , NULL ) ;
should_return = 1 ; /*should stop this linking*/
break ;
}
}
if ( ptr != NULL )
{
free( ptr ) ;
ptr = NULL ;
}
/* in case : should stop this linking*/
if ( should_return == 1 )
{
break ;
}
}
/*-------------------------------------*/
/* Here : should Stop this socket-link */
/*-------------------------------------*/
TraceLog_str("this talking has been finished,linking has been closed!\n");
shutdown(sockfd,SHUT_RDWR);
close( sockfd ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -