#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#pragma comment (lib,"WS2_32.lib")
#define USERPORT 996
//#define IP_ADDR "10.10.3.179"
#define IP_ADDR "10.10.3.194"
#define TS_PACKAGE_SIZE 188
#define TS_FRAGESIZE (8*8194)
//#define MAXSIZE (TS_FRAGESIZE/TS_PACKAGE_SIZE*TS_PACKAGE_SIZE)
#define MAXSIZE (4*8194)
main(int argc,char *argv[])
{
char buf[MAXSIZE];
SOCKET sclient;
//struct sockaddr_in client;
struct sockaddr_in server;
int num;
int count_buf=0;
int status;
WSADATA wsd;
FILE *desfp;
int res;
BOOL bConnDropped = FALSE;
INT iRet = 0;
BOOL bOK = TRUE;
struct timeval timeout = { 0, 0 };
fd_set readSocketSet;
if((status=WSAStartup(MAKEWORD(2,2),&wsd))!=0)
{
perror("wsastartup() failed:");
exit(1);
}
// printf("zhou--------------------------->1\n");
if((sclient=socket(AF_INET,SOCK_STREAM,0))< 0)
{
perror("socket failed :");
exit(1);
}
// printf("zhou--------------------------->2\n");
ZeroMemory(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(USERPORT);
server.sin_addr.s_addr=inet_addr(IP_ADDR);
// printf("zhou--------------------------->3\n");
if(connect(sclient,(struct sockaddr *)&server,sizeof(server))<0)
{
perror("connect() failed :");
printf("%d",WSAGetLastError());
exit(3);
}
perror("connect success");
//namelen=sizeof(sclient);
//printf("zhou--------------------------->4\n");
if((desfp=fopen("video.ts","wb"))==NULL)
{
perror("can not open des file");
exit(1);
}
//printf("zhou--------------------------->5\n");
while(1)
{
// count_buf++;
// printf("count_buf:%d\n",count_buf);
ZeroMemory(buf,sizeof(buf));
num=0;
//buf[0]='\0';
#if 0
if((num=recv(sclient,buf,MAXSIZE,0))==-1)
{
perror("recv data error");
exit(1);
}
#endif
FD_ZERO( &readSocketSet );
FD_SET( sclient, &readSocketSet );
iRet = select( 0, &readSocketSet, NULL, NULL, &timeout );
bOK = ( iRet > 0 );
if( bOK )
{
bOK = FD_ISSET( sclient, &readSocketSet );
}
if( bOK )
{
if((num=recv(sclient,buf,MAXSIZE,0))==-1)
{
INT iError = WSAGetLastError();
bConnDropped = ( ( iError == WSAENETRESET ) ||
( iError == WSAECONNABORTED ) ||
( iError == WSAECONNRESET ) ||
( iError == WSAEINVAL ) ||
( iRet == 0 ) ); //Graceful disconnect from other side.
printf("error:%d\n",bConnDropped);
perror("recv data error");
exit(1);
}
}
//printf("zhou--------------------------->6\n");
count_buf += num;
printf("count_buf:%d\n",num);
if(num>0)
res = write(fileno(desfp),buf,num);
// fputs(buf,desfp);
else
{
//printf("end file\n");
// break;
}
}
fclose(desfp);
closesocket(sclient);
printf("server ended successfully\n");
}
评论0