Home > Mobile >  Novice consult a question about QUdpSocket foundation
Novice consult a question about QUdpSocket foundation

Time:09-22

Below is I on the server to receive and return the message of a piece of code:
 
QHostAddress clientAddress;
While (receiver - & gt; HasPendingDatagrams ()) {
Quint16 recPort=0;
QByteArray datagram.
Datagram. Resize (receiver - & gt; PendingDatagramSize ());
//receive datagrams
The receiver - & gt; ReadDatagram (datagram data (), datagram. The size (), & amp; ClientAddress, & amp; RecPort);
QString strData=https://bbs.csdn.net/topics/datagram;

//add data to the interface
QStringList items={datagram, clientAddress. ToString (), a QString: : number (recPort)};
Int col=UI - & gt; TableWidget - & gt; ColumnCount ();
The UI - & gt; TableWidget - & gt; InsertRow (0);
For (int c=0; CQTableWidgetItem * item=new QTableWidgetItem (items) [c];
The UI - & gt; TableWidget - & gt; SetItem (0, c, item);
}

QByteArray datagback=getIp (.) toLatin1 ();
QHostAddress addressBack=QHostAddress: : Broadcast;
The receiver - & gt; WriteDatagram (datagback, datagback. The size (), addressBack, recPort);
}

Receive clientAddress with the client's address, I use addressBack saved local address, it is found that when writeDatagram no matter use which one, the client can receive return message properly,
Is it because I'm server and client are on the same machine? If you must need the clientAddress with different machine?
Now trapped in the home, there is no test environment, strives for the bosses to give directions,

There is a problem, I use two client bindings for the same port, service received the first one is the binding, I a second is a random
This is because the port is occupied so randomly assigned to another? Is there any need to pay attention to in practical use?
  •  Tags:  
  • Qt
  • Related