void RacomClient::onUdpSocketReadyRead() { if(status() != Status::Running) return; QUdpSocket *sock = udpSocket(); QNetworkDatagram dg = sock->receiveDatagram(); QByteArray data = dg.data(); qfInfo() << data.toHex(); // remove possible initial garbage int i = 0; for(i=0; i<data.size(); i++) if(data.at(i) == 0x02) break; data = data.mid(i); // remove possible multiple 0x02 for(i=1; i<data.size(); i++) if(data.at(i) != 0x02) break; data = data.mid(i-1); qfInfo() << "stripped data:" << data.toHex(); if(!m_siDriver) { m_siDriver = new siut::DeviceDriver(this); CardReader::CardReaderPlugin *plugin = cardReaderPlugin(); if(plugin) { connect(m_siDriver, &siut::DeviceDriver::messageReady, plugin, &CardReader::CardReaderPlugin::emitSiMessagereceived); } } m_siDriver->processSiPacket(data); }
/* * Add an output channel to <logger> that sends messages to a UDP socket on port * <port> on host <host>. If the host could not be found, -1 is returned and the * channel is not created. */ int logToUDP(Logger *logger, const char *host, int port) { int fd; if ((fd = udpSocket()) < 0 || netConnect(fd, host, port) != 0) return -1; else { pthread_mutex_lock(&logger->access); LOG_Output *out = log_create_output(logger, LOG_OT_UDP); out->u.fd = fd; pthread_mutex_unlock(&logger->access); return 0; } }
void* procPerMessage::seeUdp(void* arg) { procPerMessage* perMessage = (procPerMessage*) arg; char* udpPortNumber = perMessage->mutRecvPort; int udpSock = udpSocket(udpPortNumber); struct sockaddr_in clientAddress; socklen_t len; int mess; while (1) { char* msg_buf = new char[1024]; memset(msg_buf, 0, 1024); len = sizeof(clientAddress); mess = recvfrom(udpSock, msg_buf, 1024, 0, (struct sockaddr *)&clientAddress, &len); Message* msg = new Message(msg_buf, mess); perMessage->threadPool->dispatch_thread(procPerMessage::ethernetIn, (void*) msg); // delete[] msg_buf; } }
main() { // Local variables used to hold user-inputted components of a XPCTransactionClass // instance. char sUser[50]; // User name char sTicker[10]; // Ticker symbol char cBS; // Buy/Sell indicator long int iAmount; // Number of shares bought or sold // Before using sockets on the Windows-NT operating system, the Winsock library // must be initialized. WSADATA wsaData; if (WSAStartup(0x101, &wsaData) != 0) { cout << "Error initializing WinSock library: " << WSAGetLastError() << endl; return 0; } // The name of the user is retrieved from the console and is stored in the local // sUser variable. cout << "Enter your User Name: " << flush; cin >> sUser; try { // An instance of the XPCUdpSocket is created. At this point a UDP // socket has been created and a port has been defined. XPCUdpSocket udpSocket(WINDOWS_NT_PORT_NUM); // Loop forever prompting the user for transaction record data. while(1) { cout << "Enter Ticker Symbol: " << flush; cin >> sTicker; // If the user enters "QUIT", exit the loop. if (strcmp(sTicker, "QUIT") == 0) break; cout << "Enter Buy(B) or Sell(S): " << flush; cin >> cBS; cout << "Enter Quantity: " << flush; cin >> iAmount; // Create a transaction record class instance XPCTransaction newTransaction(sUser, sTicker, cBS, iAmount); // Send the transaction record to the UNIX server udpSocket.iSendMessage((void *)&newTransaction, sizeof(newTransaction), UNIX_SERVER, UNIX_PORT_NUM); // Suspend process execution until the UNIX server replies with the // updated position. udpSocket.iReceiveMessage((void *)&newTransaction, sizeof(newTransaction)); // Display the newly updated position cout << "Ticker: " << newTransaction.sGetTicker() << " New Position: " << newTransaction.iGetAmount() << endl; } } catch(XPCException &exceptObject) { cout << exceptObject.sGetException() << endl; } // Disconnect from the Windows NT socket library. The socket is already closed // since the CUdpSocket instance got destructed after it left the // try-catch block. WSACleanup(); }
void RacomClient::run() { udpSocket(); Super::run(); }
void onReceivedDatagram(const void *data, int size, const struct sockaddr_in *sin) { printf("onReadFrom emitted.. %d from %s:%d\n", size, inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); udpSocket()->writeTo(data, size, sin); }