comsocket_tool.cpp
来自「液晶电视完整代码可实现人机界面」· C++ 代码 · 共 680 行 · 第 1/2 页
CPP
680 行
#include "ComSocket_tool.h"//add zlp#include <sys/types.h>#include <sys/fcntl.h>#include <fcntl.h>#include "GCF400.h"int SocketTimeOut;void SetTimeOut(int i);jmp_buf Jmp_Environment;void (* ClearClockFnt)(int);/******************************/ /* SepPathName */ /******************************/ int SepPathName(char * pathname , char * path , char * filename) { int len , i; len = strlen(pathname); if(! len) return SUCCESS; for(i = len - 1; i >= 0 && pathname[i] != '/'; i --); if(i >= 0) { strcpy(filename , pathname + i + 1); memcpy(path , pathname , i + 1); if(i == 0) path[i + 1] = '\0'; else path[i] = '\0'; return SUCCESS; } strcpy(filename , pathname); strcpy(path , ""); return SUCCESS; } //////////////////LogPro日志记录函数///////////////////////////////////////////int LogPro( char * filename , long line , int type , const char * format, ...) { va_list args; FILE *LogFileID; char logfile[256]; sprintf(logfile, "%s/log/net.log", getenv("HOME") ); LogFileID = fopen(logfile, "a+"); if( LogFileID == NULL ){ printf("Open file error\n"); return ( -1 ); } if ( type == 0 ) fprintf( LogFileID , "I:[%s][%d]\t",filename,line ); else fprintf( LogFileID , "E:[%s][%d]\t",filename,line ); va_start( args , format ); vfprintf( LogFileID , format , args ); va_end( args ); fflush( LogFileID ); fclose( LogFileID ); return SUCCESS; } ////////////////////////////////////////////////////////void SetTimeOut(int i){ SocketTimeOut = 1; ClearClockFnt = signal( SIGALRM , ClearClockFnt ); alarm( 0 ); longjmp( Jmp_Environment , 1 ); }/////////////////////////////////////////////////////////******************************/ /* TimeFormat */ /******************************/ char *TimeFormat(struct tm *ptm, char *format){ static char buff[80]; int ctl_flg = 0; int i = 0; int year; if (format == NULL) return (char *)ptm; else if (strcmp(format, "ASC") == 0) return asctime(ptm); do { if (ctl_flg) { ctl_flg-=1; switch(*format) { case 'y' : year = 1900 + ptm->tm_year; sprintf(&buff[i], "%d", year); i = i+4; break; case 'm' : sprintf(&buff[i], "%02d",ptm->tm_mon+1); i = i+2; break; case 'd' : sprintf(&buff[i], "%02d", ptm->tm_mday); i = i+2; break; case 'h' : sprintf(&buff[i], "%02d", ptm->tm_hour); i = i+2; break; case 'i' : sprintf(&buff[i], "%02d", ptm->tm_min); i = i+2; break; case 's' : sprintf(&buff[i], "%02d", ptm->tm_sec); i = i+2; break; case 'n' : buff[i--] = '\n'; i = i+2; break; default : buff[i] = '%', buff[i+1] = *format; i = i+2; break; } } else if (*format == '%') ctl_flg = 1; else buff[i++] = *format; } while(*(++format) != 0); buff[i] = 0; return buff;}/******************************/ /* get_time */ /******************************/ char *get_time(char *format){ time_t clck; clck = time((time_t *)0); return TimeFormat(localtime(&clck), format);}/******************************/ /* Sub_Str */ /******************************/ int Sub_Str(char *pSend_buf,int csgs,char head[][20]){ char *ptag1,*ptag2; int ilen,i; ptag1 = pSend_buf; for(i=0;i<csgs-1;i++){ ptag2=strstr(ptag1,","); if(ptag2==NULL){ return -1; } ilen=ptag2-ptag1; memcpy((char *)head[i],ptag1,ilen); ptag1=ptag2+1; } strcpy(head[i],ptag2+1); return 0;}/******************************/ /* readconfig */ /******************************/ int readconfig(int bh,char * net_cs){ FILE *fd; char FileName[100],buff[100]; int yes=0; sprintf(FileName,"%s/etc/net.cfg",getenv("HOME")); fd = fopen(FileName,"r"); if(fd == NULL){ printf("Open config file error\n"); return -1; } while(fgets(buff,COMMAXBUFF,fd) != NULL){ if(bh == 1){ if(strncmp(buff,"client:",7)==0){ yes=1; strcpy(net_cs,buff+7); printf("net_cs is [%s]\n",net_cs); } } else{ if(strncmp(buff,"server:",7)==0){ yes=1; strcpy(net_cs,buff+7); printf("net_cs is [%s]\n",net_cs); } } } if(yes != 1){ printf("read config file error\n"); fclose(fd); return -1; } fclose(fd); return 0;}/***************************************************************************/int Close_Sock(int socket_id) { // shutdown(socket_id, 2); if (close(socket_id) != 0) return -1; return SUCCESS; } /***************************************************************************/ int Read_Sock(int Socket_ID , char *buff , int BuffSize , int TimeOutTime) { int nleft,nread,timeout; nleft = BuffSize; SocketTimeOut = 0; timeout = 30; setjmp( Jmp_Environment ); if(SocketTimeOut == 1) { printf("Read time out [%d,%d]\n",BuffSize,nleft); SocketTimeOut = 0; return TIMEOUTERRNO; } ClearClockFnt = signal( SIGALRM , SetTimeOut ); alarm(timeout); while(nleft > 0) { nread = read(Socket_ID , buff , nleft); if(errno == ENOSR) { errno = 0; sleep(WAITTIME); } if(nread < 0) { if(errno == EINTR) { errno = 0; continue; } /* client disconnect */ if(errno == 32||errno == 73) /* sco 32;aix 73 */ { alarm(0); printf("Read error[%d,%s]\n", errno, strerror(errno)); return -2; } alarm(0); printf("Read error[%d,%s]\n", errno, strerror(errno)); return(nread); } if(nread == 0) break; nleft -= nread; buff += nread; } alarm(0); return(BuffSize - nleft); } /***************************************************************************/ int Recvmbbuf(int Socket_ID , char *buff , int BuffSize , int TimeOutTime) { int nleft,nread; nleft = BuffSize; while(nleft > 0) { nread = read(Socket_ID , buff , nleft); if(errno == ENOSR) { errno = 0; sleep(WAITTIME); } if(nread < 0) { if(errno == EINTR) { errno = 0; continue; } /* client disconnect */ if(errno == 32||errno == 73) /* sco 32;aix 73 */ { alarm(0); printf("Read error[%d,%s]\n", errno, strerror(errno)); return -2; } alarm(0); printf("Read error[%d,%s]\n", errno, strerror(errno)); return(nread); } if(nread == 0) break; nleft -= nread; buff += nread; } alarm(0); return(BuffSize - nleft); } /***************************************************************************/ int Write_Sock(int Socket_ID , char *buff , int BuffSize) { int nleft,nwrite,timeout; nleft = BuffSize; SocketTimeOut = 0; timeout = 60; if(SocketTimeOut == 1) { SocketTimeOut = 0; printf("Write time out\n"); return TIMEOUTERRNO; } alarm(timeout); while(nleft > 0) { nwrite = write(Socket_ID , buff , nleft); if (errno == ENOSR) { errno = 0; sleep(WAITTIME);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?