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


C++ TCPSocket::Connect方法代码示例

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


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

示例1: start

void SocketThread::start()
{

    char* json = SocketManager::getInstance()->msgVector.front();
    
    TCPSocket *ts = SocketManager::getInstance()->ts;
    
    ts->Create(AF_INET,SOCK_STREAM,0);
    bool iscon=ts->Connect(SocketManager::getInstance()->getAddr(),SocketManager::getInstance()->getPort());
    
    if (iscon)
    {
        int len = strlen(json);
        len = len + 5;
        ByteArray *ba = new ByteArray(len);
        ba->writeInt(strlen(json));
        ba->writeByte(0);
        ba->writeString(json);
        ts->Send(ba->getByteArray(), len);
    } else {
        printf("TCPSOCKET ERROR : connect failed!");
    }
    
    char* buff;
    ts->Recv(buff);

    resposeMsg(buff);
}
开发者ID:yue19870813,项目名称:src,代码行数:28,代码来源:SocketThread.cpp

示例2: main

int main(int argc, char * *argv)
{
	console = new Console();

	/* Begin your application here. */
	int cc_err;

	TCPSocket * sock = new TCPSocket();
	console->WriteLine("Connecting...");
	cc_err = sock->Connect("www.example.com", 80);
	if (cc_err == CC_ERR_WOULD_BLOCK)
		console->WriteLine("Non-blocking sockets are enabled. Waiting for connection...");
	while (sock->State() == SOCKET_STATE_CONNECTING) ;
	if (sock->State() != SOCKET_STATE_CONNECTED) {
		console->WriteLine("Connection failed.");
		return 1;
	} else
		console->WriteLine("Connection seems OK.");
	console->Write("Requesting http://www.example.com/... ");
	cc_err = sock->Send("GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
	if (cc_err < 1)
		console->WriteLine("Failed.");
	else
		console->WriteLine("OK");

	while (sock->State() == SOCKET_STATE_CONNECTED) {
		string in;
		cc_err = sock->Read(in);
		if (cc_err == 0) {
			console->WriteLine(in);
			break;
		} else {
			if (cc_err != CC_ERR_WOULD_BLOCK)
				console->WriteLine("Read error %d (%s)", cc_err, GetErrorDescription(cc_err));
			System::ThreadSleep(10);
		}
	}

	sock->Close();

	delete sock;

	/* End your application here. */

#ifdef TARGET_OS_WINDOWS
	system("pause");
#endif

	delete console;
	return 0;
}
开发者ID:DanLinder,项目名称:crisscross,代码行数:51,代码来源:main.cpp

示例3: main

int main()
{

	TCPSocket* sock = new TCPSocket();
	sock->setHomeAddr( "127.0.0.1", 8023);
	sock->setRemoteAddr( "127.0.0.1", 8026);
	sock->Connect();

	cout << "accepted" << endl;
	cout << sock->getRemoteAddr()->getIp() << endl;

	while(1);

	delete sock;

	return 0;
}
开发者ID:lumpyh,项目名称:TCPSocket,代码行数:17,代码来源:main2.cpp

示例4: ConnHttpHost

bool RTHttpSender::ConnHttpHost(const std::string& addr, const unsigned short port, const std::string& host)
{
    SetHttpHost(host);
    TCPSocket* socket = this->GetSocket();
    if (socket->Open() != OS_NoErr) {
        LE("%s socket->Open failed!!!\n", __FUNCTION__);
        return false;
    }
    socket->InitNonBlocking(socket->GetSocketFD());
    socket->NoDelay();
    socket->SetSocketBufSize(96L * 1024L);
    socket->SetTask(this);
    this->SetTimer(5*1000);
    OS_Error err;
    do{
        sleep(1);
        err = socket->Connect(SocketUtils::ConvertStringToAddr(addr.c_str()), port);
        LI("Connect to http server %s:%u, waiting...\n", addr.c_str(), port);
    }while(!(err==OS_NoErr || err==EISCONN));
        
    //socket->RequestEvent(EV_RE);
    LI("%s addr:%s, port:%u\n", __FUNCTION__, addr.c_str(), port);
    return true;
}
开发者ID:DyncEric,项目名称:Teameeting-MsgServer,代码行数:24,代码来源:RTHttpSender.cpp

示例5: main

int main()
{
	TCPSocket tcpsock;
	GlobalData *gData = new GlobalData("NULL", &tcpsock);
	GeneralHandler ghandler;

	tcpsock.Connect("mmavipc.dyndns.org", 6667);
	std::cout << tcpsock.GetError() << std::endl;
	std::string str;
	std::stringstream cTime;
	cTime << (long long)time(NULL);
	if(!tcpsock.SendData("PASS :test\r\nSERVER services.mmavipc.dyndns.org 0 :NoName Services\r\nEOS\r\n"))
	{
		std::cout << "Fatal error: " << tcpsock.GetError();
	}
	GenericBot lolbot("lolbot");
	lolbot.JoinChannel("#lol");
	
	//Never do this, for testing purposes only
	while(true)
	{
		bool result = tcpsock.RecvLine(str);
		if(!result)
		{
			std::cout << "Fatal error: " << tcpsock.GetError();
			break;
		}
		std::cout << result << " " << str << std::endl;
		int spc = FindSpaceCount(str);
		std::string *split = new std::string[spc+1];
		int pos = 0;
		int oldpos = 0;
		for(int i = 0; i < spc+1; i++)
		{
			pos = str.find(' ', pos+1);
			if(i != 0)
			{
				split[i] = str.substr(oldpos+1, pos-oldpos-1);
			}
			else
			{
				split[i] = str.substr(oldpos, pos-oldpos);
			}
			oldpos = pos;
		}
		int params = 0;
		for(int i = 1; i < spc+1; i++)
		{
			if(split[i].substr(0, 1) == ":")
			{
				params = i+1;
				split[i] = split[i].substr(1, split[i].length()-1);
				for(int j = i+1; j < spc+1; j++)
				{
					split[i] += " " + split[j];
				}
				break;
			}
		}
		ghandler.Handle(split, params);
		delete [] split;
		str = "";
	}
	std::cin.ignore(10000, '\n');
}
开发者ID:mmavipc,项目名称:NoName,代码行数:65,代码来源:main.cpp


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