Ejemplo n.º 1
0
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();
		}
	}
}
Ejemplo n.º 2
0
static void Server(String r)
{
	TcpSocket   server;
	if(server.Listen(4000, 10)) {

		TcpSocket socket;
		LOG("Waiting...");
		bool b = socket.Accept(server);
		if(b) {
			LOG("Connection accepted");
			HttpHeader http;
			http.Read(socket);
			socket.Put(r);
			socket.Close();
		}
	}
}
Ejemplo n.º 3
0
void SkylarkApp::RunThread()
{
	if(SQL.IsOpen()) {
		SQL.ClearError();
		SQLR.ClearError();
		SQL.GetSession().ThrowOnError();
		SQLR.GetSession().ThrowOnError();
	}
	
	for(;;) {
		TcpSocket request;
		accept_mutex.Enter();
		if(quit) {
			accept_mutex.Leave();
			break;
		}
		SKYLARKLOG("Waiting for accept ");
		bool b = request.Accept(server);
		accept_mutex.Leave();
		if(quit)
			break;
		if(b) {
			SKYLARKLOG("Accepted ");
		#ifdef PLATFORM_POSIX
			if(prefork)
				alarm(timeout);
		#endif
			Http http(*this);
			http.Dispatch(request);
		#ifdef PLATFORM_POSIX
			if(prefork)
				alarm(0);
		#endif
			SKYLARKLOG("Finished ");
		}
		else {
			SKYLARKLOG("Accept failed: " << request.GetErrorDesc());
		#ifdef _DEBUG 
			break;
		#endif
		}
	}
}
Ejemplo n.º 4
0
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;
					}

					if(client.CanSend())
					{
						int nsent = client.Send((unsigned char*)buf, strlen(buf));
						printf("send(%d): %s\n", nsent, buf);
						if(nsent <= 0)
						{
							break;
						}
					}
					else
					{
						printf("can not send\n");
					}
				}
				else
				{
					printf("can not recv\n");
				}

				sleep(1);
			}
			sleep(1);
		}

	}

	return 0;
}