本文整理汇总了C++中StreamSocket::receiveBytes方法的典型用法代码示例。如果您正苦于以下问题:C++ StreamSocket::receiveBytes方法的具体用法?C++ StreamSocket::receiveBytes怎么用?C++ StreamSocket::receiveBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StreamSocket
的用法示例。
在下文中一共展示了StreamSocket::receiveBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void EchoServer::run()
{
_ready.set();
Poco::Timespan span(250000);
char* pBuffer = new char[_bufferSize];
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
int n = ss.receiveBytes(pBuffer, _bufferSize);
while (n > 0 && !_stop)
{
ss.sendBytes(pBuffer, n);
n = ss.receiveBytes(pBuffer, _bufferSize);
}
}
catch (Poco::Exception& exc)
{
std::cerr << "EchoServer: " << exc.displayText() << std::endl;
}
}
}
delete pBuffer;
}
示例2: handleRequest
/** Handling requests */
bool KeyValueStore::handleRequest(StreamSocket& client, Config::ThreadControl& control, char*& buf)
{
char req;
unsigned contentLength = 0;
client.receiveBytes(&req, sizeof(req));
if(req == Protocol::EXIT) {
return true;
}
// Always expect that content-length will be the next sent.
client.receiveBytes(&contentLength, sizeof(contentLength));
if(contentLength < 0)
throw Exception("Invalid content-length detected. Dropping client");
buf = new char[contentLength+1];
buf[contentLength] = '\0';
client.receiveBytes(buf, contentLength);
KeyValueStore::handleQuery(client, control, req, buf);
delete [] buf;
buf = NULL;
return false;
}
示例3: sizeof
pair<char, string> KeyValueStore::rcvResponse(StreamSocket& sock, char*& buf)
{
unsigned contentLength = 0;
char resp;
pair<char, string> ret;
char* msg = NULL;
sock.receiveBytes(&resp, sizeof(resp));
if(resp == Protocol::LOOKUP_SUC) {
sock.receiveBytes(&contentLength, sizeof(contentLength));
unsigned rem = contentLength;
unsigned totalRead = 0;
buf = new char[contentLength+1];
msg = buf;
buf[contentLength] = '\0';
while((totalRead += sock.receiveBytes(msg, rem)) != contentLength) {
msg = buf + totalRead;
rem = contentLength - totalRead;
}
ret = pair<char, string>(resp, buf);
delete [] buf;
buf = NULL;
} else {
ret = pair<char, string>(resp, "");
}
return ret;
}
示例4: run
void HTTPTestServer::run()
{
_ready.set();
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
_lastRequest.clear();
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
_lastRequest.append(buffer, n);
if (!requestComplete())
n = ss.receiveBytes(buffer, sizeof(buffer));
else
n = 0;
}
std::string response = handleRequest();
ss.sendBytes(response.data(), (int) response.size());
Poco::Thread::sleep(1000);
}
catch (Poco::Exception& exc)
{
std::cerr << "HTTPTestServer: " << exc.displayText() << std::endl;
}
}
}
}
示例5: run
void EchoServer::run()
{
_ready.set();
pi::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
ss.sendBytes(buffer, n);
n = ss.receiveBytes(buffer, sizeof(buffer));
}
}
catch (pi::Exception& exc)
{
std::cerr << "EchoServer: " << exc.displayText() << std::endl;
}
}
}
}
示例6: testSelect
void SocketTest::testSelect()
{
Timespan timeout(250000);
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Socket::SocketList readList;
Socket::SocketList writeList;
Socket::SocketList exceptList;
readList.push_back(ss);
assert (Socket::select(readList, writeList, exceptList, timeout) == 0);
assert (readList.empty());
assert (writeList.empty());
assert (exceptList.empty());
ss.sendBytes("hello", 5);
ss.poll(timeout, Socket::SELECT_READ);
readList.push_back(ss);
writeList.push_back(ss);
assert (Socket::select(readList, writeList, exceptList, timeout) == 2);
assert (!readList.empty());
assert (!writeList.empty());
assert (exceptList.empty());
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例7: testFIFOBuffer
void SocketTest::testFIFOBuffer()
{
Buffer<char> b(5);
b[0] = 'h';
b[1] = 'e';
b[2] = 'l';
b[3] = 'l';
b[4] = 'o';
FIFOBuffer f(5, true);
f.readable += delegate(this, &SocketTest::onReadable);
f.writable += delegate(this, &SocketTest::onWritable);
assert(0 == _notToReadable);
assert(0 == _readableToNot);
assert(0 == _notToWritable);
assert(0 == _writableToNot);
f.write(b);
assert(1 == _notToReadable);
assert(0 == _readableToNot);
assert(0 == _notToWritable);
assert(1 == _writableToNot);
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
int n = ss.sendBytes(f);
assert (n == 5);
assert(1 == _notToReadable);
assert(1 == _readableToNot);
assert(1 == _notToWritable);
assert(1 == _writableToNot);
assert (f.isEmpty());
n = ss.receiveBytes(f);
assert (n == 5);
assert(2 == _notToReadable);
assert(1 == _readableToNot);
assert(1 == _notToWritable);
assert(2 == _writableToNot);
assert (f[0] == 'h');
assert (f[1] == 'e');
assert (f[2] == 'l');
assert (f[3] == 'l');
assert (f[4] == 'o');
f.readable -= delegate(this, &SocketTest::onReadable);
f.writable -= delegate(this, &SocketTest::onWritable);
ss.close();
}
示例8: testEcho
void SocketTest::testEcho()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
int n = ss.sendBytes("hello", 5);
assert (n == 5);
char buffer[256];
n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例9: testAvailable
void SocketTest::testAvailable()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Timespan timeout(1000000);
ss.sendBytes("hello", 5);
char buffer[256];
assert (ss.poll(timeout, Socket::SELECT_READ));
int av = ss.available();
assert (av > 0 && av <= 5);
int n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例10: main
int main()
{
StreamSocket sock;
SocketAddress addr("70.79.74.121:80");
sock.connect(addr);
const char* data="GET / HTTP/1.1\r\nHost: xa.us.to\r\n\r\n";
sock.sendBytes(data,strlen(data));
const int bs=4096;
char* buf=new char[bs];
int br;
while((br=sock.receiveBytes(buf,bs))>0)
write(1,buf,br);
}
示例11: test
void CSimpleSocket::test(){
using namespace Poco;
using namespace Poco::Net;
StreamSocket sk;
bool connected = false;
try{
sk.connect(SocketAddress("s1.goyou.cn", 80), Timespan(6, 0));
connected = true;
}catch(Poco::Exception& exp){
std::cout << exp.displayText() << std::endl;
}
if (connected) {
try{
UInt8 buffer[2048] = "GET / HTTP/1.0\r\nUser-Agent: Mozilla/5.0\r\n\r\n";
int nSendLen = strlen((char*)buffer);
int nHasSendLen = 0;
while (nHasSendLen != nSendLen) {
int nLen = sk.sendBytes(buffer+nHasSendLen, nSendLen-nHasSendLen);
nHasSendLen += nLen;
}
int nRecvLen = 0;
do {
Socket::SocketList readList, writeList, expList;
readList.push_back(sk);
Socket::select(readList, writeList, expList, Timespan(3, 0));
nRecvLen = 0;
if (readList.size()) {
nRecvLen = sk.receiveBytes(buffer, 1024);
std::cout << nSendLen << " -- " << nRecvLen << std::endl;
buffer[nRecvLen] = 0;
std::cout << buffer << std::endl;
}
} while (nRecvLen>0);
sk.close();
}catch(Poco::Exception& exp){
std::cout <<exp.displayText() << std::endl;
}
}
}
示例12: regTest3
void regTest3()
{
tracef("reg test 3 begin: reg send and receive data, register fail");
StreamSocket ss;
SocketAddress sa("127.0.0.1", 13333);
ss.connect(sa, Timespan(3, 0));
DynamicStruct ds;
ds["type"] = "request";
ds["action"] = "server.register";
DynamicStruct param;
param["token"] = "1234567890";
param["uuid"] = "SC000000001";
ds["param"] = param;
ss.sendBytes(ds.toString().c_str(), ds.toString().length());
tracef("reg data send: %s.", ds.toString().c_str());
char buf[1024] = {0, };
ss.receiveBytes(buf, 1024);
tracef("receive bytes: %s.", buf);
tracef("socket closed.");
tracef("register test 3 finished.\n");
}
示例13: testPoll
void SocketTest::testPoll()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Stopwatch sw;
sw.start();
Timespan timeout(1000000);
assert (!ss.poll(timeout, Socket::SELECT_READ));
assert (sw.elapsed() >= 900000);
sw.restart();
assert (ss.poll(timeout, Socket::SELECT_WRITE));
assert (sw.elapsed() < 100000);
ss.sendBytes("hello", 5);
char buffer[256];
sw.restart();
assert (ss.poll(timeout, Socket::SELECT_READ));
assert (sw.elapsed() < 100000);
int n = ss.receiveBytes(buffer, sizeof(buffer));
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}
示例14: testTimeout
void SocketTest::testTimeout()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
Timespan timeout0 = ss.getReceiveTimeout();
Timespan timeout(250000);
ss.setReceiveTimeout(timeout);
Timespan timeout1 = ss.getReceiveTimeout();
std::cout << "original receive timeout: " << timeout0.totalMicroseconds() << std::endl;
std::cout << "requested receive timeout: " << timeout.totalMicroseconds() << std::endl;
std::cout << "actual receive timeout: " << timeout1.totalMicroseconds() << std::endl;
// some socket implementations adjust the timeout value
// assert (ss.getReceiveTimeout() == timeout);
Stopwatch sw;
try
{
char buffer[256];
sw.start();
ss.receiveBytes(buffer, sizeof(buffer));
fail("nothing to receive - must timeout");
}
catch (TimeoutException&)
{
}
assert (sw.elapsed() < 1000000);
timeout0 = ss.getSendTimeout();
ss.setSendTimeout(timeout);
timeout1 = ss.getSendTimeout();
std::cout << "original send timeout: " << timeout0.totalMicroseconds() << std::endl;
std::cout << "requested send timeout: " << timeout.totalMicroseconds() << std::endl;
std::cout << "actual send timeout: " << timeout1.totalMicroseconds() << std::endl;
// assert (ss.getSendTimeout() == timeout);
}
示例15: testSocketsPerformance
void LocalSocketTest::testSocketsPerformance()
{
Timestamp::TimeDiff local = 0, net = 0;
std::size_t initBufSize = 1;
std::size_t initReps = 1;
bool noDelay[2] = { true, false };
std::cout << std::endl << "OS Name: " << Environment::osName() << std::endl;
std::cout << "OS Version: " << Environment::osVersion() << std::endl;
std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl;
for (int d = 0; d < 2; ++d)
{
double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0;
std::ostringstream os;
os << Environment::osName() << '-'
<< Environment::osVersion() << '-'
<< Environment::osArchitecture() << "-TCP";
if (noDelay[d]) os << "-nodelay";
os << ".csv";
File f(os.str());
if (f.exists()) f.remove();
FileOutputStream fos(os.str());
for (std::size_t repetitions = initReps;
repetitions <= 100000;
repetitions *= 10)
{
for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2)
{
char* pBuf = new char[bufSize];
{
SocketAddress sas("/tmp/poco.server.tcp.sock");
EchoServer echoServer(sas, bufSize);
SocketAddress sac("/tmp/poco.client.tcp.sock");
StreamSocket ss(sas, &sac);
int recv = 0, sent = 0;
Stopwatch sw; int i = 0;
for (; i < repetitions; ++i)
{
sent = 0; recv = 0; local = 0;
do
{
int s;
sw.restart();
s = ss.sendBytes(pBuf + sent, bufSize - sent);
sw.stop();
local += sw.elapsed();
sent += s;
} while (sent < bufSize);
do
{
int r;
sw.restart();
r = ss.receiveBytes(pBuf + recv, bufSize - recv);
sw.stop();
local += sw.elapsed();
recv += r;
} while (recv < bufSize);
locData += sent;
locData += recv;
locTime += local;
poco_assert (sent == bufSize && recv == bufSize);
}
std::cout << "Local TCP socket, " << i << " repetitions, " << bufSize << " bytes, "
<< local << " [us]." << std::endl;
ss.close();
}
{
SocketAddress sa("localhost", 12345);
EchoServer echoServer(sa, bufSize);
StreamSocket ss;
ss.connect(SocketAddress(sa.host(), echoServer.port()));
if (noDelay[d]) ss.setNoDelay(true);
int recv = 0, sent = 0;
Stopwatch sw; int i = 0;
for (; i < repetitions; ++i)
{
sent = 0; recv = 0; net = 0;
do
{
int s;
sw.restart();
s = ss.sendBytes(pBuf + sent, bufSize - sent);
sw.stop();
net += sw.elapsed();
sent += s;
} while (sent < bufSize);
do
{
int r;
sw.restart();
r = ss.receiveBytes(pBuf + recv, bufSize - recv);
//.........这里部分代码省略.........