本文整理汇总了C++中TcpServer::accept方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpServer::accept方法的具体用法?C++ TcpServer::accept怎么用?C++ TcpServer::accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpServer
的用法示例。
在下文中一共展示了TcpServer::accept方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
void InetWvIn :: listen( int port, unsigned int nChannels,
Stk::StkFormat format, Socket::ProtocolType protocol )
{
mutex_.lock();
if ( connected_ ) delete soket_;
if ( nChannels < 1 ) {
oStream_ << "InetWvIn()::listen(): the channel argument must be greater than zero.";
handleError( StkError::FUNCTION_ARGUMENT );
}
if ( format == STK_SINT16 ) dataBytes_ = 2;
else if ( format == STK_SINT32 || format == STK_FLOAT32 ) dataBytes_ = 4;
else if ( format == STK_FLOAT64 ) dataBytes_ = 8;
else if ( format == STK_SINT8 ) dataBytes_ = 1;
else {
oStream_ << "InetWvIn(): unknown data type specified!";
handleError( StkError::FUNCTION_ARGUMENT );
}
dataType_ = format;
unsigned long bufferBytes = bufferFrames_ * nBuffers_ * nChannels * dataBytes_;
if ( bufferBytes > bufferBytes_ ) {
if ( buffer_) delete [] buffer_;
buffer_ = (char *) new char[ bufferBytes ];
bufferBytes_ = bufferBytes;
}
data_.resize( bufferFrames_, nChannels );
lastFrame_.resize( 1, nChannels, 0.0 );
bufferCounter_ = 0;
writePoint_ = 0;
readPoint_ = 0;
bytesFilled_ = 0;
if ( protocol == Socket::PROTO_TCP ) {
TcpServer *socket = new TcpServer( port );
oStream_ << "InetWvIn:listen(): waiting for TCP connection on port " << socket->port() << " ... ";
handleError( StkError::STATUS );
fd_ = socket->accept();
if ( fd_ < 0) {
oStream_ << "InetWvIn::listen(): Error accepting TCP connection request!";
handleError( StkError::PROCESS_SOCKET );
}
oStream_ << "InetWvIn::listen(): TCP socket connection made!";
handleError( StkError::STATUS );
soket_ = (Socket *) socket;
}
else {
soket_ = new UdpSocket( port );
fd_ = soket_->id();
}
connected_ = true;
mutex_.unlock();
}
示例2: main
int main()
{
TcpServer server;
if (server.listen(2000) != AbstractSocket::Done)
{
perror("Error :");
return -1;
}
TcpSocket* s = server.accept();
if (s == NULL)
{
cout << "Error: cannot accept conection" << endl;
return -1;
}
cout << "Connected client" << endl;
int i = 4;
i++;
cout << "Remote host : " << s->getRemotePort() << endl;
cout << s->getLocalPort() << endl;
cout << server.getRemoteAddress().toString() << endl;
cout << s->getRemoteAddress().toString() << endl;
s->sendInt32(10);
sleep(2);
s->sendString("hello");
s->sendCharArray("world", 5);
Frame f;
f << "This is a frame " << 666 << '\n';
if (s->sendFrame(f) != AbstractSocket::Done)
perror("error on send frame");
delete s;
server.close();
s->close();
return 1;
}