#include <iostream>
#include <arpa/inet.h>
#include <sys/socket.h>//套接信息需要的头文件
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>//用信号回收进程
#include <stdlib.h>//标准库头文件
#include <sys/wait.h>//waitpid需要的头文件
- #if 0
- #defineSOCK_UNIX_FILE "/tmp/video1_sock"
- #else
- #defineSOCK_UNIX_FILE "/tmp/video_sock"
- #endif
- #defineMAX_TRANSMIT_DATA_LEN 10240
- #define S_TIME_OUT 2000 //2s
- inttcp_write(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
- {
- int ret;
- int total_lenth =0;
- int len_remain =len;
- char *write_position =buf;
- structtimevaltimeout;
- fd_set wset, eset;
- int status =1;
- timeout.tv_sec =timeout_ms/1000;
- timeout.tv_usec = timeout_ms00;
- printf("%s %d\n",__func__,__LINE__);
- while(1){
- FD_ZERO(&wset);
- FD_ZERO(&eset);
- FD_SET(socketfd, &eset);
- FD_SET(socketfd, &wset);
- ret = select(socketfd+1, NULL, &wset, &eset,&timeout);
- printf("%s %d\n",__func__,__LINE__);
- if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
- //timeout
- printf(" writetimeout \n");
- break;
- }
- printf("%s %d\n",__func__,__LINE__);
- if(FD_ISSET(socketfd,&wset)){
- printf("%s %d\n",__func__,__LINE__);
- ret = send(socketfd, write_position, len_remain,0);
- printf("%s %d\n",__func__,__LINE__);
- if(ret <0){
- printf(" write err \n");
- return-1;
- }
- if(ret ==0){
- printf(" write==0 ... \n");
- }
- printf("%s %d\n",__func__,__LINE__);
- write_position += ret;
- total_lenth += ret;
- len_remain -= ret;
- if(0 == len_remain){
- break;
- }
- }
- if(FD_ISSET(socketfd,&eset)){
- printf("writeerr \n");
- break;
- }
- }
- //printf("write %d \n",len -len_remain);
- returnlen -len_remain;
- }
- inttcp_read(intsocketfd,char* buf,intlen,unsignedinttimeout_ms)
- {
- int ret;
- int total_lenth =0;
- int len_remain =len;
- char *read_position =buf;
- structtimevaltimeout;
- fd_set rset, eset;
- timeout.tv_sec =timeout_ms/1000;
- timeout.tv_usec = timeout_ms00;
- //printf("read timeout set %d%d\n",timeout.tv_sec,timeout.tv_usec);
- while(1){
- FD_ZERO(&rset);
- FD_ZERO(&eset);
- FD_SET(socketfd, &eset);
- FD_SET(socketfd, &rset);
- ret = select(socketfd+1, &rset, NULL,&eset, &timeout);
- if( ( timeout.tv_sec == 0)&& ( timeout.tv_usec == 0) ){
- //timeout
- //printf(" read timeout \n");
- break;
- }
- if(FD_ISSET(socketfd,&rset)){
- ret = recv(socketfd, read_position, len_remain,0);
- if