Tcp/IP
TCP/IP是一套用于网络通信的协议集合或者系统
TCP/IP体系:
TCP报文格式:
三次握手连接TCP
四次挥手断开TCP
QHostAddress
枚举类型 | 含义 |
---|---|
QHostAddress::Null | 空地址对象,相当于QHostAddress() |
QHostAddress::Broadcast | IPv4广播地址,相当于QHostAddress(“255.255.255.255”) |
QHostAddress::LocalHost | IPv4本地主机地址,相当于QHostAddress(“127.0.0.1”) |
QHostAddress::LocalHostIPv6 | IPv6本地主机地址,相当于 QHostAddress(“::1”) |
QHostAddress::Any | 双any-address栈,与该地址绑定的socket将侦听IPv4和IPv6接口 |
QHostAddress::AnyIPv6 | IPv6 any-address,相当于QHostAddress(“::”)。与该地址绑定的socket将只监听IPv4接口 |
QHostAddress::AnyIPv4 | IPv4 any-address,相当于QHostAddress(“0.0.0.0”)。与该地址绑定的socket将只监听IPv4接 |
注:Qt4我没有发现AnyIPv4。
UDP使用
发送:
QByteArray datagram;QString str = QDateTime::currentDateTime().toString("yyyy/MM/dd") + QHostInfo::fromName(QHostInfo::localHostName()).addresses().last().toString();
#ifdef WIN32datagram = str.toLocal8Bit();
#elsedatagram = str.toLatin1();
#endif //发送主机可以用QHostAddress("127.0.0.1")替换udpSocketServer.writeDatagram(datagram, QHostAddress::Broadcast, 13999); // UDP 发送数据
两种方法都可以,但是Linux下无验证。
接受:
QUdpSocket *udpSocketClient = new QUdpSocket(this);
udpSocketClient.bind(QHostAdress::Any, 13999); //建立监听
connect(&udpSocketClient, SIGNAL(readyRead()), this, SLOT(onPendingDatagrams()));
//函数实现
void ceshi::onPendingDatagrams()
{while( udpSocketClient.hasPendingDatagrams() );{QByteArray datagram;datagram.resize(udpSocketClient.pendingDatagramSize());udpSocketClient.readDatagram(datagram.data(), datagram.size()); //接收数据QString str1 = QString(datagram);}
}