本文整理汇总了C++中TCPStream类的典型用法代码示例。如果您正苦于以下问题:C++ TCPStream类的具体用法?C++ TCPStream怎么用?C++ TCPStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TCPStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: get_stream
ssize_t mbed_socket::doread(void *buf, size_t count) {
TCPStream* stream = get_stream();
socket_error_t err;
size_t count2;
if ((sock_flag_ & MBED_SOCKET_FLAG_CONNECTED) == 0) {
//TDLOG(".. doread, socket not connected");
set_errno(ENOTCONN);
// return 0 will let tuv as its EOF
return 0;
}
count2 = count;
err = stream->recv(buf, &count2);
if (err != SOCKET_ERROR_NONE) {
// dont call stream->error_check(err), make it EOF
TDLOG(".. socket recv error: %s (%d)", socket_strerror(err), err);
sock_flag_ &= ~MBED_SOCKET_FLAG_READABLE;
return 0;
}
if (count2 < count) {
// there can be a bug here.
// when count2 == count, we don't know if there is received
// packet in mbed socket(lwip?) or not.
sock_flag_ &= ~MBED_SOCKET_FLAG_READABLE;
}
return count2;
}
示例2: main
int main(int argc, char** argv) {
if (argc < 2 || argc > 4) {
printf("usage: server <port> [<ip>]\n");
exit(1);
}
TCPStream* stream = NULL;
TCPAcceptor* acceptor = NULL;
if (argc == 3) {
acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
} else {
acceptor = new TCPAcceptor(atoi(argv[1]));
}
printf("server started successfully\n");
if (acceptor->start() == 0) {
while(1) {
stream = acceptor->accept();
if (stream != NULL) {
size_t len;
char buffer[256];
while((len = stream->receive(buffer, sizeof(buffer))) > 0) {
buffer[len] = '\0';
printf("received: %s\n", buffer);
stream->send(buffer, len);
}
delete stream;
}
}
}
perror("Could not start the server");
exit(-1);
}
示例3: get_listener
void mbed_socket::doclose(void) {
socket_error_t err;
TCPListener* listener = get_listener();
if (listener != NULL) {
err = listener->stop_listening();
if (listener->error_check(err)) {
TDLOG(".. doclose listener 'stop_listening' error: %s (%d)",
socket_strerror(err), err);
}
err = listener->close();
if (listener->error_check(err)) {
TDLOG(".. doclose listener 'close' error: %s (%d)",
socket_strerror(err), err);
}
}
TCPStream* stream = get_stream();
if (stream != NULL) {
err = stream->close();
// it may be already closed by peer. so showing error can be a noise.
/*
if (stream->error_check(err)) {
TDLOG(".. doclose stream 'close' error: %s (%d)",
socket_strerror(err), err);
}
*/
}
// need to make object destrcution async
minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(this,
&mbed_socket::release).bind())
.delay(minar::milliseconds(1))
;
}
示例4: main
int main(int argc, char** argv)
{
if (argc != 5) {
printf("usage: %s <pause port> <time out port> <ip> <time out>\n", argv[0]);
exit(1);
}
int result, timeout = atoi(argv[4]);
string message;
char line[256];
printf("Connecting to the paused server...\n");
TCPConnector* connector = new TCPConnector();
TCPStream* stream = connector->connect(argv[3], atoi(argv[1]), timeout);
if (stream == NULL) {
printf("Timed out connecting to the server\n");
}
printf("Connecting to the time out server...\n");
stream = connector->connect(argv[3], atoi(argv[2]), timeout);
if (stream) {
message = "Is there life on Mars?";
stream->send(message.c_str(), message.size());
printf("sent - %s\n", message.c_str());
result = stream->receive(line, sizeof(line), timeout);
if (result == TCPStream::connectionTimedOut) {
printf("Timed out waiting for a server response\n");
}
delete stream;
}
exit(0);
}
示例5: main
int main(int argc, char** argv)
{
if (argc != 3) {
printf("usage: %s <port> <ip>\n", argv[0]);
exit(1);
}
int len;
string message;
char line[256];
TCPConnector* connector = new TCPConnector();
TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
message = "Is there life on Mars?";
stream->send(message.c_str(), message.size());
printf("sent - %s\n", message.c_str());
len = stream->receive(line, sizeof(line));
line[len] = NULL;
printf("received - %s\n", line);
delete stream;
}
stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
message = "Why is there air?";
stream->send(message.c_str(), message.size());
printf("sent - %s\n", message.c_str());
len = stream->receive(line, sizeof(line));
line[len] = NULL;
printf("received - %s\n", line);
delete stream;
}
exit(0);
}
示例6: main
int main(int argc, char** argv)
{
if (argc < 2 || argc > 4) {
printf("usage: server <port> [<ip>]\n");
exit(1);
}
TCPStream* stream = NULL;
TCPAcceptor* acceptor = NULL;
if (argc == 3) {
acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
}
else {
acceptor = new TCPAcceptor(atoi(argv[1]));
}
if (acceptor->start() == 0) {
while (1) {
stream = acceptor->accept();
if (stream != NULL) {
ssize_t len;
char line[256];
while ((len = stream->receive(line, sizeof(line))) > 0) {
line[len] = 0;
printf("received - %s\n", line);
stream->send(line, len);
}
delete stream;
}
}
}
exit(0);
}
示例7: assert
void Transport::InitTCPStack(const Peer &_peer)
{
assert(s_IsPlatformInit);
m_tcpMode = true;
// create stream
TCPStream *tcpstream = new TCPStream(_peer);
if(_peer.GetPort() != 0)
{
tcpstream->CreateAndBind();
}
else
{
tcpstream->Init();
}
//
m_streams.push_back(tcpstream);
// create listener - note: we do not need reliable listener on tcp
UnreliableListener *unreliablelistener = new UnreliableListener();
m_listeners.push_back(unreliablelistener);
// attach
tcpstream->AttachListener(*unreliablelistener);
RegisterObserver(m_observer);
}
示例8: operator
virtual void operator()(const Stream::ConnectionCallback &connectionCallback,
const Stream::BytesReceivedCallback &bytesReceivedCallback){
mCallbacks=new TCPStream::Callbacks(connectionCallback,
bytesReceivedCallback,
mStream->mSendStatus);
mMultiSocket->addCallbacks(mStream->getID(),mCallbacks);
}
示例9: main
int main(int argc, char** argv)
{
int conn_timeout = 10, rw_timeout = 0;
// As of now this is to choose the server to connect to
if(argc != 3){
printf("Usage: server <port> [<ip>]\n");
exit(1);
}
TCPStream* stream = NULL;
TCPAcceptor* acceptor = NULL;
acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
// This is hard coded to the server thats running the redis server
acl::string addr("135.44.219.148:6380");
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
acl::redis_pubsub redis(&client);
// Hard coded channel
acl::string channel = "channel", msg;
redis.clear();
// Have this program run continuously, and if an ip address is picked up, publish it to the redis server
if (acceptor->start() == 0) {
while (1) {
stream = acceptor->accept();
if (stream != NULL) {
printf("IP %s\n", stream->getPeerIP().c_str());
msg.format("%s", stream->getPeerIP().c_str());
redis.publish(channel, msg, msg.length());
}
}
}
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}
示例10: TCPStream
int mbed_socket::doconnect(const struct sockaddr *addr, socklen_t addrlen) {
(void)addrlen;
TCPStream* stream;
stream = new TCPStream(SOCKET_STACK_LWIP_IPV4);
if (stream == NULL) {
set_errno(ENOMEM);
return -1;
}
socket_error_t err;
err = stream->open(SOCKET_AF_INET4);
if (stream->error_check(err)) {
TDLOG(".. doconnect error: open failed %s (%d)",
socket_strerror(err), err);
set_errno(EBADF); // fix value to connect error no
return -1;
}
set_stream(stream, false);
sockaddr_in* inaddr = (sockaddr_in*)addr;
socket_addr s_addr;
SocketAddr saddr;
//char addrstr[20];
//makeaddr4string(addrstr, inaddr);
//TDDDLOG(".. connect to %s:%d\r\n", addrstr, inaddr->sin_port);
socket_addr_set_ipv4_addr(&s_addr, inaddr->sin_addr.s_addr);
saddr.setAddr(&s_addr);
stream->setOnError(on_error_t(this, &mbed_socket::on_error));
stream->setOnReadable(on_readable_t(this, &mbed_socket::on_readable));
stream->setOnSent(on_sent_t(this, &mbed_socket::on_sent));
stream->setOnDisconnect(on_disconnect_t(this, &mbed_socket::on_disconnect));
sock_flag_ |= MBED_SOCKET_FLAG_CONNECTING;
err = stream->connect(saddr, inaddr->sin_port,
on_connect_t(this, &mbed_socket::on_connect));
if (stream->error_check(err)) {
TDLOG(".. doconnect error: open failed %s (%d)",
socket_strerror(err), err);
set_errno(EBADF); // fix value to connect error no
return -1;
}
set_errno(EINPROGRESS);
return -1;
}
示例11: send
ssize_t Package::send(TCPStream& stream) const {
char buff[getPackageSize()];
memset(buff, 0, sizeof(buff));
writeData(buff);
ssize_t sent = stream.send(buff, getPackageSize());
return sent;
}
示例12: main
int main(int argc, char *argv[]) {
// TCP server
TCPStream *stream = NULL;
TCPAcceptor *acceptor = NULL;
if (argc != 1) {
usage(argv);
return -1;
}
initCommandMap();
acceptor = new TCPAcceptor(TCP_PORT);
if (acceptor->start() != 0) {
cerr << LOG_PREFIX "Could not start TCPAcceptor" << endl;
return -1;
}
cout << LOG_PREFIX "Initialized TLM Server" << endl;
while(1) {
cout << LOG_PREFIX "Waiting for connection." << endl;
stream = acceptor->accept();
cout << LOG_PREFIX "Connection Accepted, sending telemetry. " << endl;
if (stream != NULL) {
ssize_t len;
char line[256];
string retval;
const char *txstr;
while ((len = stream->receive(line, sizeof(line))) > 0) {
line[len] = 0;
retval = parse(line);
txstr = retval.c_str();
stream->send(txstr, retval.length());
}
delete stream;
cout << LOG_PREFIX "Connection closed." << endl;
}
}
delete acceptor;
return 0;
}
示例13: onError
void lws_conn_listener::onIncoming(TCPListener *tl, void *impl)
{
mbed::util::CriticalSectionLock lock;
TCPStream *ts = srv.accept(impl);
lws_conn *conn;
if (!impl || !ts) {
onError(tl, SOCKET_ERROR_NULL_PTR);
return;
}
conn = new(lws_conn);
if (!conn) {
lwsl_err("OOM\n");
return;
}
conn->ts = ts;
/*
* we use the listen socket wsi to get started, but a new wsi is
* created. mbed3_tcp_stream_accept() is also called from
* here to bind the conn and new wsi together
*/
lws_server_socket_service(wsi->protocol->owning_server,
wsi, (struct pollfd *)conn);
ts->setOnSent(Socket::SentHandler_t(conn, &lws_conn::onSent));
ts->setOnReadable(TCPStream::ReadableHandler_t(conn, &lws_conn::onRX));
ts->setOnError(TCPStream::ErrorHandler_t(conn, &lws_conn::onError));
ts->setOnDisconnect(TCPStream::DisconnectHandler_t(conn,
&lws_conn::onDisconnect));
/*
* mbed3 is messed up as of 2015-11-08, data packets may
* appear on the listening socket initially
*/
conn->actual_onRX((Socket *)tl);
conn->actual_onRX((Socket *)conn->ts);
lwsl_debug("%s: exit\n", __func__);
}
示例14: run
void* run()
{
// Remove 1 item at a time and process it. Blocks if no items are
// available to process.
for (int i = 0;; i++)
{
//qDebug("thread %lu, loop %d - waiting for item...", (long unsigned int)self(), i);
WorkItem* item = m_queue.remove();
//qDebug("thread %lu, loop %d - got one item", (long unsigned int)self(), i);
TCPStream* stream = item->getStream();
// Echo messages back the client until the connection is
// closed
char input[256];
for (int i = 0; i < 255; i++)
{
input[i] = '\0';
}
string output;
int len;
while ((len = stream->receive(input, sizeof(input)-1)) > 0 )
{
output = "OK";
stream->send(output.c_str(), (sizeof(output.c_str())-1));
//qDebug("thread %lu, echoed '%s' back to the client", (long unsigned int)self(), input);
std::string cmd(input);
executeCommand(cmd);
}
delete item;
}
// Should never get here
return NULL;
}
示例15: main
int main(int argc, char *argv[])
{
tpport_t port;
int i;
TCPStream tcp;
InetAddress addr;
addr = "255.255.255.255";
cout << "testing addr: " << addr << ":" << 4096 << endl;
addr = "127.0.0.1";
cout << "binding for: " << addr << ":" << 4096 << endl;
Thread::setException(Thread::throwException);
try
{
myTCPSocket server(addr);
while(server.isPendingConnection(30000))
{
tcp.connect(server);
// tcp.unsetf(ios::binary);
tcp << "welcome to " << addr << "; segment size=" << tcp.getSegmentSize() << endl;
tcp << "connected from " << tcp.getPeer(&port) << endl;
if(tcp.isPending(Socket::pendingInput, 2000))
{
tcp >> i;
tcp << "user entered " << i << endl;
}
tcp << "exiting now" << endl;
tcp.disconnect();
}
}
catch(SockException& e)
{
cout << e.getString() << ": " << e.getSystemErrorString() << endl;
exit(-1);
}
cout << "timeout after 30 seconds inactivity, exiting" << endl;
return 0;
}