当前位置: 首页>>代码示例>>C++>>正文


C++ TcpSocket::Send方法代码示例

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


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

示例1: AcceptClients

void Server::AcceptClients()
{
	TcpSocket c;
	Address from;
	if(c.Accept(server, from, false))
	{
		std::wstring name = c.GetPeer().ToString(false);

		// Check password.
		Packet p;
		if(c.Receive(&p, sizeof(p), 1000) == sizeof(p) && (p.Control == C_CONNECT || p.Control == C_RESUME))
		{
			if(password == 0 || password == ntohl(p.Password))
			{
				// Check if a client is being booted by the new client.
				if(client.IsValid())
				{
					std::wstring name = client.GetPeer().ToString(false);

					client.Close();
					if(p.Control != C_RESUME)
						Log(OL_INFO, L"Replacing client %s\r\n", name.c_str());
				}

				if(p.Control == C_CONNECT)
					Log(OL_NOTIFY | OL_INFO, L"Client connected from %s\r\n", name.c_str());
				else
					Log(OL_INFO, L"Client resumed\r\n");
				p.Control = C_CONNECT;
				c.Send(&p, sizeof(p));

				client.Take(c);
			}
			else
			{
				p.Control = C_DISCONNECT;
				p.Reason = 1;
				Log(OL_INFO, L"Rejected client %s: Bad password\r\n", name.c_str());
			}
		}
		else
		{
			p.Control = C_DISCONNECT;
			p.Reason = 0;
			Log(OL_INFO, L"Client failed to connect from %s\r\n", name.c_str());
		}
		if(p.Control == C_DISCONNECT)
		{
			c.Send(&p, sizeof(p));
			c.Close();
		}
	}
}
开发者ID:cub-uanic,项目名称:Touchpad,代码行数:53,代码来源:Server.cpp

示例2: loadOperatorGraph

    // play
    // stop
    // setTime();
    // load( std::string operatorPath, BSONPtr data )
    virtual void loadOperatorGraph( base::bson::BSONPtr operatorGraph )
    {
        m_operatorGraph = operatorGraph;
        if( m_isConnected )
        {
            // send command to client
            std::cout << "send loadOperatorGraph" << std::endl;
            //std::string test = "huhu";
            //m_client.Send(test.c_str(), test.size());

            // todo:
            base::bson::Helper command = base::bson::create();
            command["command"] = "loadOperatorGraph";
            command["operatorGraph"] = m_operatorGraph;
            base::bson::PacketPtr packet = base::bson::pack( command );
            m_client.Send(packet->m_data, packet->m_size);

            // receive response
            //char buffer[1000];
            //std::cout << "sending test message..." << std::endl;
            //int numBytesReceived = client.Receive( buffer, 10000000 );
            //std::cout << numBytesReceived << std::endl;
        }
    }
开发者ID:skydave,项目名称:composer,代码行数:28,代码来源:main.cpp

示例3: main

int main(int argc, char const *argv[])
{
	// signal(SIGPIPE, SIG_IGN);
	//=========== client ===========
	if(argc == 3)
	{
		const char* ip = argv[1];
		unsigned short port = (unsigned short)atoi(argv[2]);

		printf("ip %s\nport %hu\n", ip, port);

		TcpSocket sock;
		assert(sock.Init());
		assert(sock.Connect(ip, port));

		assert(sock.SetNonBlocking());

		getchar();

		char msg[] = "hello server";
		int nsent;
		if(sock.CanSend())
		{
			nsent = sock.Send((unsigned char*)msg, strlen(msg));
			printf("nsent:%d\n", nsent);
		}
		else
		{
			printf("can not send\n");
		}
		getchar();

		if(1/*sock.CanSend()*/)
		{
			nsent = sock.Send((unsigned char*)msg, strlen(msg));
			printf("nsent:%d\n", nsent);
		}
		else
		{
			printf("can not send\n");
		}
		getchar();

		if(sock.CanRecv())
		{
			const int buf_len = 1024;
			char buf[buf_len];
			memset(buf, 0, buf_len);
			int nread = sock.Recv((unsigned char*)buf, buf_len);

			printf("recv(%d): %s\n", nread, buf);
		}
		else
		{
			printf("can not recv\n");
		}
	}
	//=========== server ===========
	else
	{
		printf("tcp echo server\n");
		bool r = false;

		unsigned short port = 9999;
		TcpSocket sock;
		assert(sock.Init());
		r = sock.Listen(port);

		assert(r == true);

		// sock.SetNonBlocking();
		
		while(1)
		{
			printf("waiting for connection...\n");
			TcpSocket client;
			assert(client.Init());
			char ip_buf[32];
			memset(ip_buf, 0, 32);
			bool b = sock.Accept(client, ip_buf);
			printf("client ip: %s\n", ip_buf);

			// client.SetNonBlocking();

			while(b)
			{
				const int buf_len = 1024;
				char buf[buf_len];
				memset(buf, 0, buf_len);

				if(1/*client.CanRecv()*/)
				{
					int nread = client.Recv((unsigned char*)buf, buf_len);
					printf("recv(%d): %s\n", nread, buf);
					toolbox::print_bytes(buf, strlen(buf) + 2);
					if(nread <= 0)
					{
						break;
					}

//.........这里部分代码省略.........
开发者ID:ljoaquin,项目名称:toolbox,代码行数:101,代码来源:c.cpp


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