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


C++ NetConnection::GetWorldAddress方法代码示例

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


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

示例1: Connect

bool WorldServer::Connect(bool FromInit)
{
	MStateLock.lock();
	
	if (!FromInit && connection_state != WSCS_Ready)
	{
		cout << "Error: WorldServer::Connect() while not ready" << endl;
		MStateLock.unlock();
		return false;
	}
	
	connection_state = WSCS_Connecting;
	MStateLock.unlock();
	//	unsigned long nonblocking = 1;
	struct sockaddr_in	server_sin;
	struct in_addr	in;
	WORD version = MAKEWORD (1,1);
	WSADATA wsadata;
	PHOSTENT phostent = NULL;

	WSAStartup (version, &wsadata);

	if ((send_socket = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)	
	{
		cout << "WorldServer connect: Allocating socket failed. Error: " << EQC::Common::GetLastSocketError();
		SetState(WSCS_Ready);
		return false;
	}


	server_sin.sin_family = AF_INET;

	if ((phostent = gethostbyname(net.GetWorldAddress())) == NULL) 
	{
		cout << "Unable to get the host name. Error: " << EQC::Common::GetLastSocketError();
		EQC::Common::CloseSocket(send_socket);
		SetState(WSCS_Ready);
		return false;
	}
	
	memcpy ((char FAR *)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
	server_sin.sin_port = htons(WORLD_PORT);

	// Establish a connection to the server socket.
	if (connect (send_socket, (PSOCKADDR) &server_sin, sizeof (server_sin)) == SOCKET_ERROR)
	{

		cout << "WorldServer connect: Connecting to the server failed. Error: " << EQC::Common::GetLastSocketError() << endl;
		EQC::Common::CloseSocket(send_socket);
		SetState(WSCS_Ready);
		return false;
	}	
	/*#ifdef WIN32
	ioctlsocket (send_socket, FIONBIO, &nonblocking);
	#else
	fcntl(send_socket, F_SETFL, O_NONBLOCK);
	#endif*/

	// had to put a mini-receivedata here to handel switching the TCP
	// connection to Zoneserver mode when enabled console commands
	int status = 0;
	uchar buffer[1024];
	memset(buffer, 0, sizeof(buffer));
	Timer con_timeout(15000);
	con_timeout.Start();
	SetState(WSCS_Authenticating);
	
	while (1)
	{
		if (!RunLoops)
		{
			EQC::Common::CloseSocket(send_socket);
			SetState(WSCS_Ready);
			return false;
		}
		if (FromInit)
		{
			Timer::SetCurrentTime();
		}

		status = recv(send_socket, (char*) buffer, sizeof(buffer), 0);
		
		if (status >= 1)
		{
			char Bf[64]; memset(Bf, 0, sizeof(Bf));
			char Bf2[64]; memset(Bf2, 0, sizeof(Bf2));
			char Bf3[64]; memset(Bf3, 0, sizeof(Bf3));
			sprintf(Bf, "**ZONESERVER*%s*\r\n", EQC_VERSION);
			sprintf(Bf2, "**ZONESERVER*%s*\r\nZoneServer Mode", EQC_VERSION);
			sprintf(Bf3, "**ZONESERVER*%s*\r\nNot Authorized.", EQC_VERSION);

			if (strncmp((char*) buffer, "Username: ", 10) == 0)
			{
				send(send_socket, Bf, strlen(Bf), 0);
			}
			else if (strncmp((char*) buffer, "ZoneServer Mode", 15) == 0 || strncmp((char*) buffer, Bf2, strlen(Bf2)) == 0)
			{
				// Connection successful
				break;
			}
//.........这里部分代码省略.........
开发者ID:aceoyame,项目名称:OpenEQC,代码行数:101,代码来源:worldserver.cpp


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