• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ socket类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中socket的典型用法代码示例。如果您正苦于以下问题:C++ socket类的具体用法?C++ socket怎么用?C++ socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了socket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1:

bool net::socket::accept(socket& s, socket_address* addr, socklen_t* addrlen, int timeout)
{
	if (timeout != 0) {
		if (!wait_readable(timeout)) {
			return false;
		}
	}

	int fd;

#if HAVE_ACCEPT4
	if ((fd = accept4(_M_fd, reinterpret_cast<struct sockaddr*>(addr), addrlen, SOCK_NONBLOCK)) < 0) {
		return false;
	}

	s._M_fd = fd;
#else
	if ((fd = ::accept(_M_fd, reinterpret_cast<struct sockaddr*>(addr), addrlen)) < 0) {
		return false;
	}

	s._M_fd = fd;

	if (!s.set_non_blocking()) {
		s.close();
		return false;
	}
#endif

	return true;
}
开发者ID:guidoreina,项目名称:gwebs-plusplus,代码行数:31,代码来源:socket.cpp


示例2: extras

void Packet::read(socket& socket) {
  std::uint32_t bytes_read = 0;

  if (ext_len > 0) {
    std::vector<char> extras(ext_len);
    while (bytes_read < static_cast<std::uint32_t>(ext_len)) {
      bytes_read += socket.receive(boost::asio::buffer(extras));
    }
    bytes_read = 0;

    if (extras.size() > 4) {
      char flags_buf[4] = { extras[0], extras[1], extras[2], extras[3] };
      flags = readUInt32LE(flags_buf, 0);
    }
  }

  if (key_len > 0) {
    key.resize(key_len);
    while (bytes_read < key_len) {
      bytes_read += socket.receive(boost::asio::buffer(key));
    }
    bytes_read = 0;
  }

  if (bod_len > key_len) {
    std::uint32_t val_len = bod_len - key_len - ext_len;
    val.resize(val_len);
    while (bytes_read < val_len) {
      bytes_read += socket.receive(boost::asio::buffer(val));
    }
  }
}
开发者ID:nickdesaulniers,项目名称:cpp11-memcached,代码行数:32,代码来源:packet.cpp


示例3:

bool net::tcp_server::on_new_connection(socket& client, const socket_address& addr, struct listener* listener)
{
	if (!allow_connection(addr, listener)) {
		return false;
	}

	if (!client.set_tcp_no_delay(true)) {
		return false;
	}

	tcp_connection* conn = _M_connections[client.fd()];

	// Add timer.
	if (!timer::timers::add(_M_current_msec + (tcp_connection::_M_max_idle_time * 1000), conn, 0, conn->_M_timer)) {
		return false;
	}

	if (!selector::add(client.fd(), fdset::FD_SOCKET, conn, _M_client_writes_first ? READ : WRITE)) {
		// Delete timer.
		del(conn->_M_timer);

		return false;
	}

	conn->fd(client.fd());

	conn->_M_listener = listener;

	conn->_M_timer_set = 1;

	return true;
}
开发者ID:guidoreina,项目名称:gwebs-plusplus,代码行数:32,代码来源:tcp_server.cpp


示例4: add

void socket_acceptor::add(const socket& s) const {
	auto hdl = s.native_handle();
#if defined(NG_WINDOWS)
#else
	pimpl->max = std::max(pimpl->max, hdl);
#endif

	FD_SET(hdl, &pimpl->all);
}
开发者ID:giranath,项目名称:ngsocket,代码行数:9,代码来源:socket_acceptor.cpp


示例5: relay

// Call from work to establish a proxy between two sockets.
void worker::relay(socket& left, socket& right)
{
    // Blocks until the context is terminated, always returns -1.
    zmq_proxy_steerable(left.self(), right.self(), nullptr, nullptr);

    // Equivalent implementation:
    ////zmq::poller poller;
    ////poller.add(left);
    ////poller.add(right);
    ////
    ////while (!poller.terminated())
    ////{
    ////    const auto signaled = poller.wait();
    ////
    ////    if (signaled.contains(left.id()))
    ////        forward(left, right);
    ////
    ////    if (signaled.contains(right.id()))
    ////        forward(right, left);
    ////}
}
开发者ID:evoskuil,项目名称:libbitcoin-protocol,代码行数:22,代码来源:worker.cpp


示例6: apply

void certificate::apply(socket& sock)
{
    zcert_apply(self_, sock.self());
}
开发者ID:Lucky7Studio,项目名称:czmqpp,代码行数:4,代码来源:certificate.cpp


示例7:

bool operator==(const socket& sock_a, const socket& sock_b)
{
    return sock_a.self() == sock_b.self();
}
开发者ID:BWallet,项目名称:czmqpp,代码行数:4,代码来源:socket.cpp


示例8: forward

// TODO: use non-copying private zmq implementation of forward.
// Call from work to forward a message from one socket to another.
bool worker::forward(socket& from, socket& to)
{
    message packet;
    return !from.receive(packet) && !to.send(packet);
}
开发者ID:evoskuil,项目名称:libbitcoin-protocol,代码行数:7,代码来源:worker.cpp


示例9: set_resend_interval

 void set_resend_interval(socket &s, milliseconds i)
 { s.setopt(REQ, REQ_RESEND_IVL, static_cast<int>(i.count())); }
开发者ID:Bigpet,项目名称:nanomsgxx,代码行数:2,代码来源:reqrep.cpp


示例10: set

 void poll_entry::set(socket &s, int ev) noexcept
 { set(s.fd(), ev); }
开发者ID:Bigpet,项目名称:nanomsgxx,代码行数:2,代码来源:poll.cpp


示例11: is

 bool poll_entry::is(const socket &s) const noexcept
 { return this->fd == s.fd(); }
开发者ID:Bigpet,项目名称:nanomsgxx,代码行数:2,代码来源:poll.cpp


示例12: swap

 void swap(socket &s1, socket &s2) noexcept
 { s1.swap(s2); }
开发者ID:Bigpet,项目名称:nanomsgxx,代码行数:2,代码来源:socket.cpp


示例13: add

void poller::add(socket& sock)
{
    zpoller_add(self_, sock.self());
}
开发者ID:BWallet,项目名称:czmqpp,代码行数:4,代码来源:poller.cpp


示例14: remove

void socket_acceptor::remove(const socket& s) const {
	auto hdl = s.native_handle();

	FD_CLR(hdl, &pimpl->all);
	FD_CLR(hdl, &pimpl->ready);
}
开发者ID:giranath,项目名称:ngsocket,代码行数:6,代码来源:socket_acceptor.cpp


示例15: handle_command

void auth::handle_command(socket& pipe) {
    // Get the whole message off the pipe in one go
	message msg;
	pipe.receive(msg);

    if(0 == msg.parts())
        return;     // Interrupted

    // authentication command
	std::string command = msg.get(0);
	if (verbose) {
    	std::cout <<"auth: API command=" << command << std::endl;
    }

    if("ALLOW" == command) {
    	std::string address = msg.get(1);
    	if(verbose) {
    		std::cout << "auth: whitelisting ipaddress=" << address << std::endl;
    	}

    	whitelist.insert(address);
    	pipe.send(signal::ok); 

    } else if("DENY" == command) {
    	std::string address = msg.get(1);
    	if(verbose) {
    		std::cout << "auth: blacklisting ipaddress=" << address << std::endl;
    	}

    	blacklist.insert(address);
    	pipe.send(signal::ok); 
    	
    } else if("DOMAIN" == command) {
    	std::string domain = msg.get(1);
    	if(verbose) {
    		std::cout << "auth: domain=" << domain << std::endl;
    	}

    	this->domain = domain;
    	pipe.send(signal::ok); 
    	
    } else if("PLAIN" == command) {
    	std::string user = msg.get(1);
    	std::string pass = msg.get(2);

        if (verbose) {
            std::cout << "auth: configured PLAIN - user:" << user << std::endl; 
        }

    	passwords.insert(std::make_pair(user, pass));
    	pipe.send(signal::ok); 

    } else if("CURVE" == command) {
    	// If client_public_key is CURVE_ALLOW_ANY, allow all clients. Otherwise
    	// treat client_public_key as client public key certificate.
    	std::string client_public_key = msg.get(1);

    	if("CURVE_ALLOW_ANY" == client_public_key) {
    		curve_allow_any = true;
            if(verbose) {
    		  std::cout << "auth: configured CURVE - allow ALL clients" << std::endl;
            }
    	} else {
    		curve_allow_any = false;
    		client_keys.insert(client_public_key);
            if(verbose) {
    		  std::cout << "auth: configured CURVE - allow client with public key:" << client_public_key << std::endl;
            }
    	}
		pipe.send(signal::ok); 

    } else if("GSSAPI" == command) {
    	// GSSAPI authentication is not yet implemented here
        if(verbose) {
    	   std::cout << "auth: configure GSSAPI authentication is not yet implemented here" << std::endl;
        }
    	pipe.send(signal::ok); 

    } else if("VERBOSE" == command) {
    	std::string verbose_string = msg.get(1);

    	verbose = ("true" == verbose_string)? true : false;
    	pipe.send(signal::ok); 

    } else if("TERMINATE" == command) {
    	std::cout << "auth: Shutdown ZAP Authentication Server" << std::endl;
        
    	terminated = true;
        pipe.send(signal::ok); 

    } else {
        if(verbose) {
    	   std::cout << "auth: Invalid command=" << command << std::endl;
        }
    	assert(false);
    }
}
开发者ID:jbreams,项目名称:mqexec-tng,代码行数:97,代码来源:auth.cpp


示例16:

socket::socket(const socket& sock) {
	_socket = sock.get_rawsocket();
//	sock.set_rawsocket(SOCKET_ERROR);
}
开发者ID:rabbit1989,项目名称:rabbit_server,代码行数:4,代码来源:socket.cpp


示例17: handleIncomingMessage

 void handleIncomingMessage(socket& sock, const std::string& msg)
 {
     typedef std::pair<std::string, std::string> NameAndArgs;
     auto nameAndArgs = deserialize<NameAndArgs>(msg);
     sock.write(functions_[nameAndArgs.first](nameAndArgs.second));
 }
开发者ID:CCJY,项目名称:coliru,代码行数:6,代码来源:main.cpp


示例18: device

 void device(socket &s1, socket &s2)
 {
   nn_device(s1.fd(), s2.fd());
   throw_error();
 }
开发者ID:Bigpet,项目名称:nanomsgxx,代码行数:5,代码来源:nn.cpp


示例19: is_ready

bool socket_acceptor::is_ready(const socket& s) const {
	return FD_ISSET(s.native_handle(), &pimpl->ready) != 0;
}
开发者ID:giranath,项目名称:ngsocket,代码行数:3,代码来源:socket_acceptor.cpp


示例20: write

 virtual void write(const byte *buf, size_t len) {
   s.write_some(boost::asio::buffer(buf,len));
 }
开发者ID:MartinMuzatko,项目名称:code,代码行数:3,代码来源:MCSocket.hpp



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ socket_ptr类代码示例发布时间:2022-05-31
下一篇:
C++ smart_ptr类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap