简体   繁体   中英

QT - JAVA socket loss data

i've a QT/C++ server and a Java client. The client asks a file to the server, and the server send to the client the stream. The problem is that in the TCP transmission (also in localhost) i lost some packets. Sometimes, the client receive 280705 bytes of 288890.

There is the server :

MyTcpServer::MyTcpServer(QObject *parent) :
    QTcpServer(parent)
{
}

void MyTcpServer::startServer(int port)
{
    if(!this->listen(QHostAddress::Any, serverPort))
    {
        qDebug() << "Could not start server";
    }
    else
    {
        qDebug() << "Listening to port " << serverPort << "...";
    }
}

void MyTcpServer::incomingConnection(qintptr socketDescriptor)
{
    SocketThread *thread = new SocketThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

This is the SocketThread:

SocketThread::SocketThread(qintptr ID, QObject *parent) :
QThread(parent)
{
    this->socketDescriptor = ID;
}

void SocketThread::run()
{
    socket = new QTcpSocket();
    if(!socket->setSocketDescriptor(this->socketDescriptor))
    {
        emit error(socket->error());
        return;
    }
    connect(socket, SIGNAL(readyRead()),    this, SLOT(readyRead()), Qt::DirectConnection);
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));

    socket->write("Welcome to the Server\r\n"); //SEND AN HELLO MESSAGE
    socket->waitForBytesWritten();
    exec();
}

void SocketThread::readyRead()
{       
    QByteArray socketByteArray = socket->readAll();

    int number = 0;
    QDataStream socketDataStream(socketByteArray);
    socketDataStream >> number; //RECEIVE A NUMBER (I WANT 1)
    if (number == 1)
    {
       QFile file("C:\\temp\\test.txt");
       file.open(QIODevice::ReadWrite);

        socket->write(QString("%1\n").arg(file.size()).toStdString().c_str()); //SEND THE FILESIZE AS STRING
        socket->waitForBytesWritten();
        QByteArray buffer = file.readAll();
        long byteSent = socket->write(buffer); //SEND THE FILE

        socket->flush();
        file.close();
    }
    socket->close();
}

void SocketThread::disconnected()
{
    socket->deleteLater();
    exit(0);
}

And this is the JAVA client :

Socket MyClient = null;

boolean connect()
{
  try
  {
    MyClient = new Socket(remoteIP, remotePort);
    MyClient.setSoTimeout(60000);
    if (MyClient != null) {
        inFromServer = new BufferedReader(new InputStreamReader(MyClient.getInputStream()));

        serverWelcomeMessage = inFromServer.readLine(); //RECEIVE THE WELCOME MESSAGE
    }
  }
  catch (IOException e) {
    ...
  }
}

void requestFile()
{
    try {

        FileOutputStream fos = null;

        BufferedOutputStream bos = null;

        DataOutputStream outToServer = new DataOutputStream(MyClient.getOutputStream());

        outToServer.write(encodeIntToByteArray(1)); //SEND THE 1

        outToServer.flush();

        InputStream is = MyClient.getInputStream();

        int remoteFileSize = Integer.parseInt(inFromServer.readLine()); //RECEIVE THE FILESIZE AS STRING

        fos = new FileOutputStream(output);

        bos = new BufferedOutputStream(fos);

        int byteCount = 0;

        int totalByteCount = 0;

        byte[] bytes = new byte[1400];

        while ((byteCount = is.read(bytes)) > 0) {  //RECEIVE THE FILE

            bos.write(bytes, 0, byteCount);

            totalByteCount += byteCount;

        }

        System.out.println("Byte Received "+totalByteCount+" of "+remoteFileSize);

        bos.close();

        fos.close();

        is.close();
    }
catch(...) {

} }

The file test.txt is a file with a number on each line:

0
1
2
3
4
...much numbers...
50000

Sometimes, the client receive the entire file, sometimes receive the file without the first part like this:

60
1860
1861
1862
...much numbers...
50000

Starts from 60, jump to 1860 and ends to 50000.

I try to iterate the request 1000 times, and 90% of times the code works, transfer all data.

Can someone help me to understand ?

The problem is in usage of IO streams. You cannot use different instances without proper understanding of side-effects: inFromServer & is. Your exact problem is java.io.BufferedReader#defaultCharBufferSize.

I would recommend you to initialize streams & readers on connection. And use exactly them everywhere across your class.

private Socket socket;
private OutputStream outputStream;
private Writer outputWriter;
private InputStream inputStream;
private Reader inputReader;

public void connect() throws IOException {
    socket = new Socket(..., ...);
    socket.setSoTimeout(60000);
    outputStream = new BufferedOutputStream(socket.getOutputStream()); // Buffered 
    outputWriter = new OutputStreamWriter(outputStream);               // Non-buffered - !important
    inputStream = new BufferedInputStream(socket.getInputStream());    // Buffered
    inputReader = new InputStreamReader(inputStream);                  // Non-buffered - !important
}

And it would be better to use Java naming conventions for Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM