本文整理汇总了C++中TCPSocket::read方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::read方法的具体用法?C++ TCPSocket::read怎么用?C++ TCPSocket::read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TCPSocket
的用法示例。
在下文中一共展示了TCPSocket::read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void TcpSocketTest::run(){
TCPSocket* server = new TCPSocket(9586);
TCPSocket* peer = server->listenAndAccept();
cout<<"server got peer connection"<<endl;
char buff[1024];
int rc = peer->read(buff,sizeof(buff));
buff[rc] = '\0';
cout<<"server recv: "<<buff<<endl;
peer->write(buff, strlen(buff));
cout<<"server sent: "<<buff<<endl;
peer->close();
server->close();
delete peer;
delete server;
}
示例2: update_client
//-----------------------------------------------------------------------------
ReadResult ConsoleServer::update_client(TCPSocket client)
{
uint32_t msg_len = 0;
ReadResult rr = client.read_nonblock(&msg_len, 4);
// If no data received, return
if (rr.error == ReadResult::NO_ERROR && rr.bytes_read == 0) return rr;
if (rr.error == ReadResult::REMOTE_CLOSED) return rr;
if (rr.error != ReadResult::NO_ERROR) return rr;
// Else read the message
Array<char> msg_buf(default_allocator());
array::resize(msg_buf, msg_len);
ReadResult msg_result = client.read(array::begin(msg_buf), msg_len);
array::push_back(msg_buf, '\0');
if (msg_result.error == ReadResult::REMOTE_CLOSED) return msg_result;
if (msg_result.error != ReadResult::NO_ERROR) return msg_result;
process(client, array::begin(msg_buf));
return msg_result;
}
示例3: runServer
void runServer(const char * host, int port, bool withBlocking)
{
printf("Setting up a server socket to %s:%d\n", host, port);
char buf[1024];
TCPServerSocket server;
int i, err = server.open(host, port, 5);
// err = server.open(0, port, 5)
if(err) {
const int msgSize = 128;
char msgBuf[msgSize] = "";
printf("%s cannot open server socket to %s:%d -> %s\n",
appname, host, port, strerror_s(msgBuf, msgSize, err));
return;
}
for(i = 0; i < 10; i++) {
puts("Waiting for a connection");
while(withBlocking && !server.isPendingConnection(1000000)) {
printf(".");
fflush(0);
}
TCPSocket * socket = server.accept();
if(!socket) {
Radiant::error("Could not create accept a socket connection.");
return;
}
socket->setNoDelay(true);
printf("Got a new socket %p\n", socket);
fflush(0);
for(int j = 0; j < iterations; j++) {
int32_t len = 0;
buf[0] = '\0';
int n = socket->read( & len, 4);
if(n != 4) {
Radiant::error("Could not read 4 header bytes from the socket, got %d", n);
delete socket;
return;
}
n = socket->read(buf, len);
if(n != len) {
Radiant::error("Could not read %d data bytes from the socket, got %d",
len, n);
}
printf("Received \"%s\"\n", buf);
if(withreplies) {
socket->write( & len, 4);
socket->write( & buf, len);
}
}
delete socket;
}
fflush(0);
info("%s %d clients handled, returning", appname, i);
}