📄 vecho.c
字号:
//---------------------------------------------------------------------------
// TEST
//---------------------------------------------------------------------------
// VECHO.C
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <winsock.h>
char Buffer[720*480*2];
int main( int argc, char *argv[] )
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKET slisten,srecv,ssend;
struct sockaddr_in sin;
int size,readsize,tmp;
int tmp1,tmp2,tmp3,tmp4;
time_t ts,tn;
int frames,size_lo,size_hi,size_tot;
int recording=0,playing=0,recframes=0;
FILE *vidfile;
unsigned char c;
printf("\nVECHO - Windows Peer (Echo) App for JPEG_Network Demo\n");
if( argc != 2 ||
sscanf(argv[1],"%03d.%03d.%03d.%03d",&tmp1,&tmp2,&tmp3,&tmp4)!=4 ||
(tmp1 < 0 || tmp1 > 255 || tmp2 < 0 || tmp2 > 255 ||
tmp3 < 0 || tmp3 > 255 || tmp4 < 0 || tmp4 > 255) )
{
printf("\nUsage: VECHO <x.x.x.x>\n");
exit(0);
}
tmp1 |= tmp2 << 8;
tmp1 |= tmp3 << 16;
tmp1 |= tmp4 << 24;
wVersionRequested = MAKEWORD(1, 1);
tmp = WSAStartup(wVersionRequested, &wsaData);
if (tmp != 0)
{
printf("\r\nUnable to initialize WinSock for host info");
exit(0);
}
// Create our listening socket. This allows a peer to
// connect to us to receive our video stream (in this case
// the video will simply be echoed).
slisten = socket( AF_INET, SOCK_STREAM, 0 );
if( slisten == INVALID_SOCKET )
{
printf("failed socket (%d)\n",WSAGetLastError());
goto leave;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(2000);
// Bind socket
if ( bind( slisten, &sin, sizeof(sin) ) < 0 )
{
printf("ERROR: failed bind (%d)\n",WSAGetLastError());
goto leave;
}
// Start listening
if ( listen( slisten, 1) < 0 )
{
printf("ERROR: failed listen (%d)\n",WSAGetLastError());
goto leave;
}
// Creat the recv socket. This is the socket on which we'll
// receive image data
srecv = socket( AF_INET, SOCK_STREAM, 0 );
if( srecv == INVALID_SOCKET )
{
printf("failed socket (%d)\n",WSAGetLastError());
goto leave;
}
// Connect to the same IP address on port 2000
sin.sin_addr.s_addr = tmp1;
sin.sin_port = htons(2000);
if( connect( srecv, &sin, sizeof(sin) ) < 0 )
{
printf("failed connect (%d)\n",WSAGetLastError());
goto leave;
}
printf("\nVECHO Initialized\nPress CTRL-C to Exit\n");
// Get a connection (our send socket)
size = sizeof( sin );
ssend = accept( slisten, &sin, &size );
if( ssend == INVALID_SOCKET )
goto leave;
printf("Fully Connected!\n");
// Send the file size
size = 0;
send( ssend, (char *)&size, 4, 0 );
ts = time(0);
frames = 0;
size_lo = 999999;
size_hi = 0;
size_tot = 0;
client_loop:
while(!kbhit())
{
// Read the file size if the next JPG image
readsize = 0;
while( readsize < 4 )
{
tmp = recv( srecv, ((char *)&size)+readsize, 4-readsize, 0 );
if( tmp < 0 )
{
printf("Socket Read Error (%d)\n",WSAGetLastError());
goto leave;
}
if( !tmp )
{
printf("Socket Read Peer Disconnect!\n");
goto leave;
}
readsize += tmp;
}
// When the size is non-zero, read the JPG file data next
readsize = 0;
while( readsize < size )
{
tmp = recv( srecv, Buffer+readsize, size-readsize, 0 );
if( tmp < 0 )
{
printf("Socket Read Error (%d)\n",WSAGetLastError());
goto leave;
}
if( !tmp )
{
printf("Socket Read Peer Disconnect!\n");
goto leave;
}
readsize += tmp;
}
// If we're recording, recode this video
if( recording )
{
fwrite(&size,4,1,vidfile);
fwrite(Buffer,size,1,vidfile);
if( ++recframes > 5000 )
{
printf("STOPPED [5000 frame limit]\n");
recording = 0;
fclose(vidfile);
}
}
// If we're playing, replace the video with a file read
if( playing )
{
tmp = fread(&readsize,1,4,vidfile);
if( tmp != 4 )
{
printf("STOPPED [EOF]\n");
playing = 0;
fclose(vidfile);
}
else
{
fread(Buffer,1,readsize,vidfile);
size = readsize;
}
}
// Send the data
if( size )
{
// Send the encoded file
// Send the file size
if( send( ssend, (char *)&size, 4, 0 ) < 0 )
break;
// Send the file data
if( send( ssend, Buffer, size, 0 ) < 0 )
break;
}
size_tot += size;
if( size < size_lo )
size_lo = size;
if( size > size_hi )
size_hi = size;
tn = time(0);
frames++;
if( tn > ts )
{
printf("%d frames Lo: %d Hi: %d Avg: %d\n",frames,size_lo,size_hi,size_tot/frames);
ts = tn;
frames = 0;
size_lo = 999999;
size_hi = 0;
size_tot = 0;
}
}
c = getch();
if( c>='A' && c<='Z' )
c += 'a'-'A';
if( c == 's' || c=='p' || c=='r' || c== 'b' || c=='q' )
{
if( recording || playing )
{
fclose(vidfile);
playing = 0;
recording = 0;
}
if( c == 's' )
{
printf("STOPPED\n");
goto client_loop;
}
if( c == 'r' )
{
if(!(vidfile = fopen("video.dat","wb")))
printf("Can't open output file\n");
else
{
printf("RECORDING\n");
recframes = 0;
recording = 1;
}
goto client_loop;
}
if( c == 'p' || c == 'b' )
{
if( c == 'p' )
vidfile = fopen("video.dat","rb");
else
vidfile = fopen("battle.dat","rb");
if( !vidfile )
printf("Can't open input file\n");
else
{
printf("PLAYING\n");
playing = 1;
}
goto client_loop;
}
if( c == 'q' )
{
printf("QUIT\n");
closesocket( ssend );
closesocket( srecv );
goto leave;
}
}
printf("Keys: [Q]uit, [R]ecord, [P]lay, [S]top\n");
goto client_loop;
leave:
printf("\nABORT\n");
WSACleanup();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -