本文整理汇总了C++中SocketServer::acceptConnection方法的典型用法代码示例。如果您正苦于以下问题:C++ SocketServer::acceptConnection方法的具体用法?C++ SocketServer::acceptConnection怎么用?C++ SocketServer::acceptConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer
的用法示例。
在下文中一共展示了SocketServer::acceptConnection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main( char inNumArgs, char **inArgs ) {
if( inNumArgs < 2 ) {
usage( inArgs[0] );
}
if( inNumArgs > 3 ) {
usage( inArgs[0] );
}
char useFile = true;
if( inNumArgs != 3 ) {
useFile = false;
}
long port;
int numRead = sscanf( inArgs[1], "%d", &port );
if( numRead != 1 ) {
printf( "port number must be a valid integer: %s\n", inArgs[2] );
usage( inArgs[0] );
}
File *outFile;
FileOutputStream *outStream;
if( useFile ) {
outFile = new File( NULL, inArgs[2],
strlen( inArgs[2] ) );
outStream = new FileOutputStream( outFile );
}
SocketServer *server = new SocketServer( port, 1 );
printf( "listening for a connection on port %d\n", port );
Socket *sock = server->acceptConnection();
if( sock == NULL ) {
printf( "socket connection failed\n" );
return( 1 );
}
printf( "connection received\n" );
SocketStream *inStream = new SocketStream( sock );
unsigned long checksum = 0;
unsigned char *buffer = new unsigned char[ BUFFER_SIZE ];
numRead = BUFFER_SIZE;
int numWritten = BUFFER_SIZE;
while( numWritten == numRead && numRead == BUFFER_SIZE ) {
// read a buffer full of data from standard in
numRead = inStream->read( buffer, BUFFER_SIZE );
// add the buffer to our checksum
for( int i=0; i<numRead; i++ ) {
checksum += buffer[i];
}
if( useFile ) {
// write the buffer out to our file stream
numWritten = outStream->write( buffer, numRead );
}
else {
// write to std out
numWritten = fwrite( buffer, 1, numRead, stdout );
}
}
if( numRead != numWritten ) {
printf( "file output failed\n" );
}
printf( "checksum = %d\n", checksum );
delete sock;
delete server;
delete inStream;
if( useFile ) {
delete outStream;
delete outFile;
}
delete [] buffer;
return 0;
}