TCP连接建立和断开时的序列号

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_20184565/article/details/86500901

 

如下图所示,以TCP流客户端192.168.1.110:52582与服务端23.67.181.145::80为例,说明一下在TCP连接建立和断开时的序列号使用情况。

首先在连接建立时,先看一下客户端的序列号的使用。

客户端第一个SYN数据包,No.1951,Wireshark显示其使用的Seq=0,这是一个相对的序列号,是wireshark自身的显示,数据包中真实的序列号并不为0。使用相对序列号同样可说明序列号使用情况。客户端发出的第二个ACK数据包,No.1630,其Seq=1,说明之前的第一个SYN报文占用了一个序列号。客户端发出的第三个数据报文,No.1631,其Seq=1,没有变化,说明之前的第二个ACK报文未占用序列号。

在连接建立过程中,服务端发送的第一个报文SYN+ACK,No.6529,其相对序列号Seq=0,第二个服务端发出的报文,No.1672,其序列号为Seq=1,说明之前的第一个SYN+ACK使用掉了一个序列号。

 

其次,在连接断开阶段,如下图所示。服务端首先发送FIN+ACK报文,No.1677,其序列号为301。数据包No.1706为服务端发送的下一个ACK数据包,其序列号为Seq=302,可见FIN+ACK占用了一个序列号,另外数据包No.1706的确认序列号Ack=1628,作为对客户端的数据包No.1680(Seq=1627)的回应,表明客户端的FIN+ACK是占用了一个序列号。

综上所述,TCP连接交互过程中,单独的ACK报文是不占用序列号的。包括在数据传输节点,ACK报文也是不占用序列号的,如下所示:

服务端23.67.181.145发往客户端的两个ACK报文(No.1672和No.1673),ACK不同的客户端数据,其序列号相同都是Seq=1。

完。

 

展开阅读全文

多线程,QTcpsocket问题!服务器断开时程序崩溃!

03-30

RT 请高手指教-谢谢帮忙!rn我自己参照网上的一些资料写了个QTcpSocket的线程!当我连接到服务器后,然后让服务器断开连接,程序有时候就正常,但是大多数情况下程序就崩溃!rn错误信息如下图:rn[img=http://www.qtcn.org/bbs/attachment/Mon_1203/62_116608_b5250f5c9029d54.jpg?176][/img]rn就是d这个参数有问题! rn if (d->socketEngine)rn d->socketEngine->setReadNotificationEnabled(false); 执行完这句就挂了!!rnrnrn我尝试过用Designer进行编译,MinGw进行编译,并且今天我还更新了新的QT4.8.1(以前是4.8.0),都是崩溃,一会儿就好了!rn我把链接socket状态信号和自定义槽,注释掉后,就留下发射数据的信号,就没有任何问题!如下:rnrn1.//connect(socketClient, SIGNAL(connected()),this, SLOT(sendRequest())); rn2.//connect(socketClient, SIGNAL(disconnected()), this,SLOT(connectionCloseByServer())); rn3.connect(socketClient, SIGNAL(readyRead()), this, SLOT(recvdatagram()),Qt::DirectConnection); rn4.//connect(socketClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); rnrnrn所以我怀疑我的代码写的有问题,请各位高手指点!!rnSocketThread.h rnrnrn1.#ifndef SOCKETTHREAD rn2.#define SOCKETTHREAD rn3.rn4.#include rn5.#include rn6.rn7.rn8.class SocketThread : public QThread rn9. rn10.Q_OBJECT rn11.rn12.//function rn13.public: rn14.SocketThread(QHostAddress addr, int port); rn15.~SocketThread(); rn16.//通过该函数删除socket rn17.void Stop(); rn18.rn19.// variable rn20.public: rn21.rn22.//variable rn23.private: rn24.rn25.int sum; rn26.bool needStopThread; rn27.QTcpSocket *socketClient; rn28.quint16 readBufferSize; rn29.quint16 nextBlockSize; rn30.QByteArray datagram; rn31.rn32.QHostAddress _hostAddress; rn33.int _portNumber; rn34.rn35.//function rn36.private: rn37.void closeConnection(); rn38.rn39.private slots: rn40.void sendRequest(); rn41.void connectionCloseByServer(); rn42.void error(); rn43.void recvdatagram(); rn44.signals: rn45.//将接收到的数据和状态发射出去! rn46.void RecvDateDisplay(QByteArray str); rn47.void SumDisplay(int s); rn48.void disButtonDisplay(bool b); rn49.void conButtonDisplay(bool a); rn50.void proBarDisplay(bool a); rn51.void staLabeDisplay(QString str); rn52.rn53.protected: rn54.virtual void run(); rn55.; rn56.#endif //SOCKETTHREADrnrnrnSocketThread.cpprnrnrn1.#include "SocketThread.h" rn2.#include rn3.rn4.SocketThread::SocketThread(QHostAddress addr, int port) rn5. rn6._hostAddress =addr; rn7._portNumber = port; rn8.sum = 0; rn9.needStopThread = false; rn10.socketClient=NULL; rn11.readBufferSize = 256; rn12.rn13.qRegisterMetaType("QAbstractSocket::SocketError"); rn14. rn15.SocketThread::~SocketThread() rn16. rn17.needStopThread = false; rn18.closeConnection(); rn19.quit(); rn20.wait(); rn21. rn22.rn23.void SocketThread::sendRequest() rn24. rn25.emit staLabeDisplay(tr("Connected!")); rn26.emit proBarDisplay(false); rn27. rn28.void SocketThread::connectionCloseByServer() rn29. rn30.if (nextBlockSize != 0xFFFF) rn31. rn32.emit staLabeDisplay(tr("Error:connection closed by server")); rn33.emit proBarDisplay(false); rn34. rn35.rn36.if (socketClient) rn37. rn38.socketClient->close(); rn39.exit(); rn40.wait(); rn41. rn42.rn43. rn44.void SocketThread::error() rn45. rn46.emit staLabeDisplay(socketClient->errorString()); rn47.emit proBarDisplay(false); rn48.emit conButtonDisplay(true); rn49.emit disButtonDisplay(false); rn50.rn51.if (socketClient) rn52. rn53.socketClient->close(); rn54.exit(); rn55.wait(); rn56. rn57. rn58.void SocketThread::run() rn59. rn60.rn61.nextBlockSize = 0; rn62.rn63.socketClient = new QTcpSocket; rn64.rn65.if (!_hostAddress.isNull() && _portNumber >0) rn66. rn67.socketClient->abort(); rn68.socketClient->connectToHost(_hostAddress, _portNumber, QIODevice::ReadOnly); rn69.socketClient->setReadBufferSize(readBufferSize); rn70.emit staLabeDisplay(tr("Connect to Server........")); rn71.emit proBarDisplay(true); rn72.emit conButtonDisplay(false); rn73.emit disButtonDisplay(true); rn74. rn75.//slot rn76.rn77.connect(socketClient, SIGNAL(connected()),this, SLOT(sendRequest())); rn78.connect(socketClient, SIGNAL(disconnected()), this,SLOT(connectionCloseByServer())); rn79.connect(socketClient, SIGNAL(readyRead()), this, SLOT(recvdatagram()),Qt::DirectConnection); rn80.connect(socketClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); rn81.rn82.rn83.exec(); rn84.while(needStopThread) rn85. rn86.if (socketClient) rn87. rn88.closeConnection(); rn89. rn90.break; rn91. rn92.rn93. rn94.void SocketThread::closeConnection() rn95. rn96.socketClient->abort(); rn97.socketClient->close(); rn98.delete socketClient; rn99.socketClient = NULL; rn100.rn101.sum =0; rn102.emit proBarDisplay(false); rn103.emit conButtonDisplay(true); rn104.emit disButtonDisplay(false); rn105. rn106.void SocketThread::Stop() rn107. rn108.needStopThread = true; rn109. rn110.void SocketThread::recvdatagram() rn111. rn112.if (socketClient->bytesAvailable() >0) rn113. rn114.datagram.resize(socketClient->bytesAvailable()); rn115.socketClient->read(datagram.data(), datagram.size()); rn116.sum = sum +datagram.size(); rn117.rn118.emit SumDisplay(sum); rn119.emit RecvDateDisplay(datagram); rn120. rn121. 论坛

没有更多推荐了,返回首页