virtual void createUDPSockets(string _url, bool _testSocketWO) { ServiceAddress serviceAddress(_url, _testSocketWO? ServiceAddress::stUDPRO : ServiceAddress::stUDPWO); m_bfSocket = BFSocketUPtr(new BFSocket(serviceAddress)); bzero(&m_streamAddr, sizeof(m_streamAddr)); m_streamAddr.sin_family = AF_INET; m_streamAddr.sin_addr.s_addr = serviceAddress.hostAddress(); m_streamAddr.sin_port = htons(m_port); m_sockFd = socket(AF_INET, SOCK_DGRAM, 0); if(m_sockFd == -1) THROW_SOCKET_EXCEPTION("Could not create socket - " << strerror(errno)); if(!_testSocketWO) { if(bind(m_sockFd, (struct sockaddr *) &m_streamAddr, sizeof(m_streamAddr))) THROW_SOCKET_EXCEPTION("Could not bind UDP Socket - " << strerror(errno)); if(serviceAddress.isMulticast()) { struct ip_mreq mreq; zero_init(mreq); mreq.imr_multiaddr.s_addr = serviceAddress.hostAddress(); mreq.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(m_sockFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) THROW_SOCKET_EXCEPTION("Could not add membership do socket - " << strerror(errno)); } } }
int main(int argc, char **argv) { QCoreApplication app(argc, argv); QStringList args = app.arguments(); QString appName = args.takeFirst(); bool notification = args.contains("-n"); if (notification) args.removeAll("-n"); if (args.size() < 2) { qDebug("usage: %s [-n] <service> <method> <arguments>", appName.toLocal8Bit().data()); return -1; } // try to process socket QIODevice *device = 0; QScopedPointer<QIODevice> devicePtr(device); QString service = args.takeFirst(); QUrl serviceUrl = QUrl::fromUserInput(service); QHostAddress serviceAddress(serviceUrl.host()); if (serviceAddress.isNull()) { QLocalSocket *localSocket = new QLocalSocket; device = localSocket; localSocket->connectToServer(service); if (!localSocket->waitForConnected(5000)) { qDebug("could not connect to service: %s", service.toLocal8Bit().data()); return -1; } } else { QTcpSocket *tcpSocket = new QTcpSocket; device = tcpSocket; int servicePort = serviceUrl.port() ? serviceUrl.port() : 5555; tcpSocket->connectToHost(serviceAddress, servicePort); if (!tcpSocket->waitForConnected(5000)) { qDebug("could not connect to host at %s:%d", serviceUrl.host().toLocal8Bit().data(), servicePort); return -1; } } QJsonRpcSocket socket(device); QString method = args.takeFirst(); QVariantList arguments; foreach (QString arg, args) arguments.append(arg); QJsonRpcMessage request = notification ? QJsonRpcMessage::createNotification(method, arguments) : QJsonRpcMessage::createRequest(method, arguments); QJsonRpcMessage response = socket.sendMessageBlocking(request, 5000); if (response.type() == QJsonRpcMessage::Error) { qDebug("error(%d): %s", response.errorCode(), response.errorMessage().toLocal8Bit().data()); return -1; } qDebug() << response.result(); }
bool DlvClient::Connect(const QString &service) { QUrl serviceUrl = QUrl::fromUserInput(service); QHostAddress serviceAddress(serviceUrl.host()); QTcpSocket *tcpSocket = new QTcpSocket(this); int servicePort = serviceUrl.port() ? serviceUrl.port() : 5555; tcpSocket->connectToHost(serviceAddress, servicePort); if (!tcpSocket->waitForConnected(5000)) { qDebug("could not connect to host at %s:%d", serviceUrl.host().toLocal8Bit().data(), servicePort); return false; } m_addr = service; m_isCommandBlock = false; m_dlv.reset(new QJsonRpcSocket(tcpSocket,this)); return true; }