Exemple #1
0
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);
}
Exemple #2
0
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;
}
Exemple #3
0
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;
}
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;
}
Exemple #5
0
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');
}