SocketClient TcpServer::accept() throw (SocketError) { int cfd; struct sockaddr_in remoteaddr; socklen_t sin_size = sizeof(struct sockaddr_in); if ((cfd = ::accept(sfd, (struct sockaddr *)&remoteaddr, &sin_size)) < 0) { throw SocketError(sfd, errno, strerror(errno)); } return SocketClient(cfd, remoteaddr, is_nonblock); }
int _tmain(int argc, _TCHAR* argv[]) { SocketClient so = SocketClient("192.168.0.179"); so.sendTo("Hello from C++ 111"); so.sendTo("Hello from C++ 222"); so.~SocketClient(); /*WSAData data; SOCKET sock; struct sockaddr_in si_other; const std::string address = "192.168.0.179"; int slen = sizeof(si_other); char message[512] = "Hello World Java, I am a C++ Client Socket"; int ret = WSAStartup(MAKEWORD(2, 2), &data); if (ret != 0) { printf("Error WSAStartup failed: " + WSAGetLastError()); WSACleanup(); } sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == INVALID_SOCKET) { printf("Error opening socket: " + WSAGetLastError()); } memset((char *)&si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(8881); si_other.sin_addr.S_un.S_addr = inet_addr(address.c_str()); std::stringstream sstm; for (int i = 0; i < 10; i++) { sstm << "Attemp #" << i << " - Sending:" << message << "\n"; printf(sstm.str().c_str()); ret = sendto(sock, message, strlen(message), 0, (struct sockaddr *) &si_other, slen); if (ret < 0) { sstm << "Failed Sending:" << WSAGetLastError(); } sstm.str(""); } printf("\n *** End ***"); std::getchar(); exit(0); closesocket(sock); WSACleanup(); */ return 0; }
int main(int argc, char** argv) { comPort=openPort(portS);//***PUT YOUR COM PORT NUMBER HERE!*** // location of the machine running the 3D motion cube std::string receiverHost = "localhost"; if (argc > 2) { std::cout << "Usage: " << argv[0] << " <hostname>" << std::endl; std::cout << "The arguments specify the host of the motion cube (Default: localhost)" << std::endl; return 1; } if (argc > 1) { receiverHost = std::string(argv[1]); } EmoEngineEventHandle eEvent = EE_EmoEngineEventCreate(); EmoStateHandle eState = EE_EmoStateCreate(); unsigned int userID = 0; try { // if (EE_EngineConnect() != EDK_OK) { if (EE_EngineRemoteConnect("127.0.0.1", 3008) != EDK_OK) { throw std::exception("Emotiv Engine start up failed."); } else { std::cout << "Emotiv Engine started!" << std::endl; neutral();//send all servos to their neutral positions } int startSendPort = 6868; std::map<unsigned int, SocketClient> socketMap; promptUser(); while (true) { // Handle the user input if (_kbhit()) { if (!handleUserInput()) { break; } } if(paused == true){ int state = EE_EngineGetNextEvent(eEvent); // New event needs to be handled if (state == EDK_OK) { EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent); EE_EmoEngineEventGetUserId(eEvent, &userID); switch (eventType) { // New headset connected, create a new socket to send the animation case EE_UserAdded: { std::cout << std::endl << "New user " << userID << " added, sending Cognitiv animation to "; std::cout << receiverHost << ":" << startSendPort << "..." << std::endl; promptUser(); socketMap.insert(std::pair<unsigned int, SocketClient>( userID, SocketClient(receiverHost, startSendPort, UDP))); startSendPort++; break; } // Headset disconnected, remove the existing socket case EE_UserRemoved: { std::cout << std::endl << "User " << userID << " has been removed." << std::endl; promptUser(); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { socketMap.erase(iter); } break; } // Send the Cognitiv animation if EmoState has been updated case EE_EmoStateUpdated: { //std::cout << "New EmoState from user " << userID << "..." << std::endl; EE_EmoEngineEventGetEmoState(eEvent, eState); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { sendCognitiv(eState); } break; } // Handle Cognitiv training related event case EE_CognitivEvent: { handleCognitivEvent(std::cout, eEvent); break; } default: break; } } else if (state != EDK_NO_EVENT) { std::cout << "Internal error in Emotiv Engine!" << std::endl; break; } } Sleep(1); } } catch (const std::exception& e) { std::cerr << e.what() << std::endl; std::cout << "Press any keys to exit..." << std::endl; getchar(); } EE_EngineDisconnect(); EE_EmoStateFree(eState); EE_EmoEngineEventFree(eEvent); return 0; }
int main(int argc, char **argv) { // location of the machine running the "BlueGirl" head model std::string receiverHost = "localhost"; if (argc > 2) { std::cout << "Usage: " << argv[0] << " <hostname>" << std::endl; std::cout << "The arguments specify the host of the head model " "(Default: localhost)" << std::endl; return 1; } if (argc > 1) { receiverHost = std::string(argv[1]); } EmoEngineEventHandle eEvent = IEE_EmoEngineEventCreate(); EmoStateHandle eState = IEE_EmoStateCreate(); unsigned int userID = 0; const int CONTROL_PANEL_PORT = 3008; try { // Connect to EmoEngine if (IEE_EngineConnect() != EDK_OK) { throw std::runtime_error("EmoEngine start up failed."); } else { std::cout << "EmoEngine started!" << std::endl; } int startSendPort = 30000; std::map<unsigned int, SocketClient> socketMap; std::cout << "Type \"exit\" to quit, \"help\" to list available commands..." << std::endl; promptUser(); while (true) { // Handle the user input if (_kbhit()) { if (!handleUserInput()) { break; } } int state = IEE_EngineGetNextEvent(eEvent); // New event needs to be handled if (state == EDK_OK) { IEE_Event_t eventType = IEE_EmoEngineEventGetType(eEvent); IEE_EmoEngineEventGetUserId(eEvent, &userID); switch (eventType) { // New headset connected // Create a new socket to send the animation case IEE_UserAdded: { std::cout << std::endl << "New user " << userID << " added, sending FacialExpression animation to "; std::cout << receiverHost << ":" << startSendPort << "..." << std::endl; promptUser(); socketMap.insert(std::pair<unsigned int, SocketClient>( userID, SocketClient(receiverHost, startSendPort, UDP))); startSendPort++; break; } // Headset disconnected, remove the existing socket case IEE_UserRemoved: { std::cout << std::endl << "User " << userID << " has been removed." << std::endl; promptUser(); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { socketMap.erase(iter); } break; } // Send the FacialExpression animation // if EmoState has been updated case IEE_EmoStateUpdated: { IEE_EmoEngineEventGetEmoState(eEvent, eState); std::map<unsigned int, SocketClient>::iterator iter; iter = socketMap.find(userID); if (iter != socketMap.end()) { sendFacialExpressionAnimation(iter->second, eState); } break; } // Handle FacialExpression training event case IEE_FacialExpressionEvent: { handleFacialExpressionEvent(std::cout, eEvent); } default: break; } } else if (state != EDK_NO_EVENT) { std::cout << std::endl << "Internal error in Emotiv Engine!" << std::endl; break; } #ifdef _WIN32 Sleep(15); #endif #ifdef __linux__ sleep(1); #endif } } catch (const std::runtime_error& e) { std::cerr << e.what() << std::endl; std::cout << "Press 'Enter' to exit..." << std::endl; getchar(); } // Clean up IEE_EngineDisconnect(); IEE_EmoStateFree(eState); IEE_EmoEngineEventFree(eEvent); return 0; }