// Demonstrates a client that can receive and play received data #include #include #include #include using namespace std; // Returns true if specified key is down bool KeyDown(int vKey) { bool bReturn; unsigned short iResult; iResult = GetAsyncKeyState(vKey); iResult = iResult & 0xFF00; if(iResult > 0) { bReturn = true; } else { bReturn = false; } return(bReturn); } void main() { int iResult; long long int InputData = mnCreatePacket(); long long int RecvPacket = mnCreatePacket(); // Connect to server char IP[1024]; unsigned short Port; int PushToTalkKey = 0; cout << "Enter an IP to connect to: "; cin >> IP; cout << "Enter a port to connect to: "; cin >> Port; cout << "Press a key to be your 'push to talk' button\n"; cout << "Waiting for key press...\n"; Sleep(500); while(PushToTalkKey == 0) { Sleep(1); for(int n = 0;n<200;n++) { if(KeyDown(n) == true) { PushToTalkKey = n; cout << "Key press detected\n\n"; break; } } } cout << "Connecting...\n"; mnStart(1,0); mnSetBufferSizes(0,15000,1024,STATE_CLIENT); iResult = mnConnect(0,IP,Port,IP,Port,5,true); switch(iResult) { case(1): cout << "Connected to server\n"; cout << "Local TCP Port: " << mnGetLocalIPTCP(0) << '\n'; cout << "Local TCP Port: " << mnGetLocalPortTCP(0) << '\n'; cout << "Local UDP IP: " << mnGetLocalIPUDP(0) << '\n'; cout << "Local UDP Port: " << mnGetLocalPortUDP(0) << '\n'; break; case(0): cout << "Failed to connect: timed out\n"; system("PAUSE"); return; break; case(-1): cout << "Failed to connect: an unknown error occurred\n"; system("PAUSE"); return; break; case(-2): cout << "Failed to connect: the server is full\n"; system("PAUSE"); return; break; } // Setup sound mnStartSound(1,mnGetMaxClients(0)+1); mnStartInput(0,-1); for(int n = 1;n<=mnGetMaxClients(0);n++) { mnStartOutput(n,-1); } // Main loop while(mnClientConnected(0,0) == 1) { Sleep(1); // Toggle sound input if(KeyDown(PushToTalkKey) == true) { if(mnGetInputUnpaused(0) == 0) { mnUnpauseInput(0); cout << "Input unpaused\n"; } } else { if(mnGetInputPaused(0) == 0) { mnPauseInput(0); cout << "Input paused\n"; } } // Input data iResult = mnGetInputData(0,InputData); if(iResult > 0) { // Send data to server mnSendUDP(0,InputData,0,false,true); } // Packets from server iResult = mnRecvUDP(0,RecvPacket,0,0); if(iResult > 0) { // Play data int iClient = mnGetInt(RecvPacket); mnErase(RecvPacket,0,sizeof(int)); mnPlayData(1,RecvPacket); } } cout << "Disconnected from server\n"; mnFinishSound(); mnFinish(-1); system("PAUSE"); return; }