コード例 #1
0
ファイル: Net.cpp プロジェクト: wyrover/boids
void tcpUpdate(float dt)
{
	accumulated_time += dt;
	if (accumulated_time >= time_to_send_heart_beat)
	{
		//发送心跳包
		uint32_t empty = 4;
		tcp_client.send((unsigned char*)&empty, 4);

		accumulated_time -= time_to_send_heart_beat;
	}

	if (stage == to_receive_size)
	{
		if (tcp_client.receive((unsigned char*)&data_size, 4) == 0)
		{
			return;
		}
		data_size -= 4; //后面数据的具体长度是要减去头的4的
		receive_data = new unsigned char[data_size];
		stage = to_receive_data;
	}

	if (stage == to_receive_data)
	{
		if (tcp_client.receive(receive_data, data_size) == 0)
		{
			return;
		}

		boids::BoidsMessageHeader __msg;
		if (__msg.ParseFromArray(receive_data, data_size))
		{
			switch (__msg.type())
			{
				HANDLE_MSG(boids::AUTO_MATCH_RESPONSE, boids::MatchResponse, autoMatchResponse);
			default:
				cocos2d::log("[ERROR] unrecognized msg type: %d", __msg.type());
				break;
			}
		}
		else
		{
			cocos2d::log("[ERROR] receive a packet but not a BoidsMessageHeader. data_len: %d", data_size);
		}

		delete receive_data;
		stage = to_receive_size;
	}
}
コード例 #2
0
int main(int argc, char** argv){
  if (argc < 2) {
    printf ("ERROR: Chamada incorreta.\nEscreva '<nome do programa> <endereço IP> <porta>'.\n");
    return 0;
  }

  receivingBuffer = sendingBuffer = new char[512];
  matrix clientMatrix;

  TcpClient client;
  client.connect (argv[1], atoi(argv[2]));

  while (client.receive (receivingBuffer, textLength)) {
    cout << receivingBuffer;
    if (receivingBuffer[textLength-1]  == ':'){
      cin >> sendingBuffer;
      client.send (sendingBuffer);
    }
  }