Setting UDP output port?
-
New to Qt and enjoying the experience, however....
I'm having problems with UDP datagrams??? How do I bind to a known outgoing port?
First I setup a 'discovery receive socket' on port 30303.....
@
//setup Discovery UDP socket on port 30303
discovery_udpSocket = new QUdpSocket(this);
discovery_udpSocket->bind(QHostAddress::Broadcast,30303);
connect(discovery_udpSocket, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));
@Within the event handler for sending datagram I bind socket again
@
// Bind udp socket to Artemis discovery port 30303
discovery_udpSocket = new QUdpSocket(this);
//discovery_udpSocket->bind(QHostAddress::Broadcast,30303); // I tried this as well
discovery_udpSocket->bind(30303);// Create test data packet // QByteArray datagram = "Message number : " + QByteArray::number(messageNo); discovery_udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast,30303);
@
When I run the application and run WireShark to analyse the datagrams, the outgoing port is being randomly set each time I send a datagram?
Any help appreciated :)
Kevin
[EDIT: code formatting, Volker]
-
The reason for needing to know the source & destination ports is due to the fact
I'm using the MicroChip TCP/IP stack were the UDPOpen API Syntax is:UDP_SOCKET UDPOpen(UDP_PORT localport, NODE_INFO *remoteNode, TCP_PORT remotePort)
where:
localPort [in] = Local UDP port number on which data transfer will occure
remoteNode [in] = Remote host that contains remotePort
RemoteNode [in] = UDP port number on remote host to transfer the data from.
-
Have a look at "this forum thread":http://developer.qt.nokia.com/forums/viewthread/4145, they discussed the same topic over there.
-
bind binds a name to a socket. You're simply binding the wrong name, QHostAddress::Broadcast not being an address of any local interface (are you checking the return value of bind? It's probably failing with EADDRNOTAVAIL). Get a copy of TCP/IP Illustrated and/or Unix Network Programming for the gory details.