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


C++ StreamSocket::connect方法代码示例

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


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

示例1: 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();
}
开发者ID:Alcibiades586,项目名称:roadrunner,代码行数:13,代码来源:SocketTest.cpp

示例2: testAddress

void LocalSocketTest::testAddress()
{
	SocketAddress sas("/tmp/poco.server.tcp.sock");
	ServerSocket serv;
	serv.bind(sas);
	serv.listen();
	StreamSocket ss;
	SocketAddress sac("/tmp/poco.client.tcp.sock");
	ss.connect(sas, &sac);
	StreamSocket css = serv.acceptConnection();
	assert (css.peerAddress().host() == ss.address().host());
	assert (css.peerAddress().port() == ss.address().port());
}
开发者ID:RobertoMalatesta,项目名称:sandbox,代码行数:13,代码来源:LocalSocketTest.cpp

示例3: regTest6

void regTest6()
{
	tracef("reg test 6 begin: reg connect timeout.");
	StreamSocket ss;
	SocketAddress sa("127.0.0.1", 13333);
	ss.connect(sa, Timespan(3, 0));
	for(int i = 0; i < 25; i++)
	{
		tracef("%d seconds...", i + 1);
		Thread::sleep(1000);
	}
	ss.close();
	tracef("register test 6 finished.");
}
开发者ID:shuchuangtech,项目名称:Shuchuang,代码行数:14,代码来源:main.cpp

示例4: 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();
}
开发者ID:Alcibiades586,项目名称:roadrunner,代码行数:16,代码来源:SocketTest.cpp

示例5: testConnectRefused

void LocalSocketTest::testConnectRefused()
{
	SocketAddress sas("/tmp/poco.server.tcp.sock");
	ServerSocket serv(sas);
	serv.close();
	StreamSocket ss;
	Timespan timeout(250000);
	SocketAddress sac("/tmp/poco.client.tcp.sock");
	try
	{
		ss.connect(sas, timeout, &sac);
		fail("connection refused - must throw");
	}
	catch (ConnectionRefusedException&)
	{
	}
}
开发者ID:RobertoMalatesta,项目名称:sandbox,代码行数:17,代码来源:LocalSocketTest.cpp

示例6: 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);
		
	
}
开发者ID:Dinesh-Ramakrishnan,项目名称:workspace,代码行数:17,代码来源:sock.C

示例7: testConnectRefused

void SocketTest::testConnectRefused()
{
	ServerSocket serv;
	serv.bind(SocketAddress());
	serv.listen();
	Poco::UInt16 port = serv.address().port();
	serv.close();
	StreamSocket ss;
	Timespan timeout(250000);
	try
	{
		ss.connect(SocketAddress("localhost", port));
		fail("connection refused - must throw");
	}
	catch (ConnectionRefusedException&)
	{
	}
}
开发者ID:Alcibiades586,项目名称:roadrunner,代码行数:18,代码来源:SocketTest.cpp

示例8: 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;
        }
    }
}
开发者ID:zhuifeng1017,项目名称:PocoTest,代码行数:44,代码来源:CSimpleSocket.cpp

示例9: regTest2

void regTest2()
{
	tracef("reg test 2 begin: reg send data.");
	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());
	ss.close();
	tracef("socket closed.");
	tracef("register test 2 finished.\n");
}
开发者ID:shuchuangtech,项目名称:Shuchuang,代码行数:20,代码来源:main.cpp

示例10: testStreamEcho

void SocketStreamTest::testStreamEcho()
{
	EchoServer echoServer;
	StreamSocket ss;
	ss.connect(SocketAddress("localhost", echoServer.port()));
	SocketStream str(ss);
	str << "hello";
	assert (str.good());
	str.flush();
	assert (str.good());
	ss.shutdownSend();

	char buffer[5];
	str.read(buffer, sizeof(buffer));
	assert (str.good());
	assert (str.gcount() == 5);
	assert (std::string(buffer, 5) == "hello");

	ss.close();
}
开发者ID:RobertoMalatesta,项目名称:sandbox,代码行数:20,代码来源:SocketStreamTest.cpp

示例11: testBufferSize

void SocketTest::testBufferSize()
{
	EchoServer echoServer;
	StreamSocket ss;
	ss.connect(SocketAddress("localhost", echoServer.port()));
	
	int osz = ss.getSendBufferSize();
	int rsz = 32000;
	ss.setSendBufferSize(rsz);
	int asz = ss.getSendBufferSize();
	std::cout << "original send buffer size:  " << osz << std::endl;
	std::cout << "requested send buffer size: " << rsz << std::endl;
	std::cout << "actual send buffer size:    " << asz << std::endl;
	
	osz = ss.getReceiveBufferSize();
	ss.setReceiveBufferSize(rsz);
	asz = ss.getReceiveBufferSize();
	std::cout << "original recv buffer size:  " << osz << std::endl;
	std::cout << "requested recv buffer size: " << rsz << std::endl;
	std::cout << "actual recv buffer size:    " << asz << std::endl;
}
开发者ID:,项目名称:,代码行数:21,代码来源:

示例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");
}
开发者ID:shuchuangtech,项目名称:Shuchuang,代码行数:22,代码来源:main.cpp

示例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();
}
开发者ID:Alcibiades586,项目名称:roadrunner,代码行数:23,代码来源:SocketTest.cpp

示例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);
}
开发者ID:Alcibiades586,项目名称:roadrunner,代码行数:37,代码来源:SocketTest.cpp

示例15: main

int main(int argc, char** argv)
{
	if (argc > 1)
		serverAddr = string(argv[1]);
	if (argc > 2)
		port = atoi(argv[2]);
	if (argc > 3)
		filename = string(argv[3]);

	try
	{
		ss.connect(SocketAddress(serverAddr, port));
	}
	catch (Poco::Exception& exc)
	{
		cout << "Connect server failed, err: " << exc.displayText() << endl;
		return -1;
	}
	testOption(addUser, "1011", "[email protected]#$%^&*[email protected]#$%^&*[email protected]#$%^&*~abc", filename);
	testOption(addUser, "2011", "2011b", filename);
	testOption(queryUserInfo, "[email protected]#$%^&*[email protected]#$%^&*~abcdefghijklmnopqrstHIHIHHKJDHSKJHDQOPMMXZSD", "", filename);
	testOption(queryUserInfo, "1011", "", filename);
	testOption(queryAllUserInfo);
	testOption(updateUser, "1011", "[email protected]#$%^&*~abcdefghijklmnopqrstHIHIHHKJDHSKJHDQOPMMXZSD", filename);
	testOption(delUser, "1011");

	char c(' ');

    /*
	while (c != 'q' && c != 'Q')
	{
	cout << "Press q then enter to quit: \n";
	cin >> c;
	}
	*/
	return 0;
}
开发者ID:xubingyue,项目名称:finger_server,代码行数:37,代码来源:RegistClient.cpp


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