当前位置: 首页>>代码示例>>C++>>正文


C++ SocketServer::acceptConnection方法代码示例

本文整理汇总了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;
	}
开发者ID:duralakun,项目名称:minorGems,代码行数:98,代码来源:netPipeReceiver.cpp


注:本文中的SocketServer::acceptConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。