Results 1 to 7 of 7

Thread: Sending a file while chatting QTcpSocket and QTcpServer

  1. #1

    Default Sending a file while chatting QTcpSocket and QTcpServer

    Hello to all, this is my first thread on this site. I am a newbie and i am studying c++ using Qt. I am asked to create a sort of chat client/server where two users can (of course) talk each other and also send a file. For the talking itself is not a problem, i've already done it. I am wondering how to send a file at the same time. I mean, i should use two different sockets, one for the chat and one for the file transfer or it can be done on the same socket? If can be done on the same socket, how can the client or server understands if it's a file or simply a text message?. I hope i was not too confusing. Thank you for your help

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    One simple way, have tags in the chatting message

    Example 1:
    [CHAT]Hi friend, did you see the Qt Dev days movie[/CHAT]
    [CHAT]I will send you the file wait[/CHAT]
    [FILE]27349720840924972084973849734075082789789249043093 892[/FILE]

    you could also use some binary tags (instead of text) to keep the parsing simple.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    Thank you for the fast reply
    However, i believe i should work in a different way. I mean, this is the code of my server, (which is also very similar to the client). The readyread() function manages the message sent between the two users. Shouldn't i edit that function?
    Consider that i know the code to send and receive the file (which i can even post you later) but i dunno how to "filter" the two different things.


    #include "myserver.h"
    #include "std_lib_facilities.h"
    MyServer::MyServer(QObject *parent) :
    QObject(parent)
    {

    //initial server
    server = new QTcpServer();
    //connect signal and slots
    //if a new connection is triggered.
    //The server will execute slot newConnection();
    connect(server,SIGNAL(newConnection()),this,SLOT(n ewConnection()));
    }

    void MyServer::startServer(){

    //server starts listen port
    server->listen(QHostAddress::Any,port);
    }
    void MyServer::setPort(int port)
    {
    this->port=port;
    }

    void MyServer::stopServer(){
    //close server
    server->close();
    }

    void MyServer::newConnection(){
    //assign socket to new connection
    socket = server->nextPendingConnection();
    //connect signal and slots, if the client send a message the readyRead();
    //will trigger slot readyRead().
    connect(socket,SIGNAL(readyRead()),this,SLOT(ready Read()));
    }

    void MyServer::readyRead(){
    while(socket->canReadLine()){
    //socket read a line with a UTF8 format
    QString line = QString::fromUtf8(socket->readLine());
    //socket return the line that socket just read.

    socket->write(line.toAscii());

    //signal ui to update
    emit updateUI(line);

    }
    }
    void MyServer::sendText(QString text){


    QString output="server [";
    output.append(QString::fromStdString(this->ltime.timeSystem()));
    output.append("]:");

    text = text + "\r\n";
    output.append(text);
    socket->write(output.toAscii());
    emit updateUI(output);
    }

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    You need to add a mechanism (in readyRead()) to differentiate between a chat message, and a file message. One way I suggested earlier
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    yes yes, don't misunderstand me, i get your suggestion and i did something similar for other purposes. But still i am confused now.
    Let clarify my thought: for example, when i send the file, i could send a message with the tag [FILE] and in readyRead() when QString line = QString::fromUtf8(socket->readLine()); get that line i could use "find("[FILE]"); and if positive the server/client knows that the next date will be a file. So with a if who compares the return of find() i can choose what to do?

    and sorry for my grammar -.-'

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    I would go with your initial idea instead and use a separate connection for the file transfer.

    Cheers,
    _

  7. #7

    Default Re: Sending a file while chatting QTcpSocket and QTcpServer

    Thank you, i will try to do it in both one and two sockets using a chosen port (for the chat) and a fixed one for the "data socket".

Similar Threads

  1. sending a file(txt,docs,exe,rar,zip...) through QTcpSocket
    By toufic.dbouk in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2012, 17:03
  2. QTcpSocket & QTcpServer are mishappen.
    By Fallen_ in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 19:06
  3. Sending mp3 files over QTcpSocket
    By ada10 in forum Newbie
    Replies: 5
    Last Post: 17th August 2010, 08:05
  4. File Transfer with QTcpServer and QTcpSocket
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 17:12
  5. Replies: 3
    Last Post: 29th June 2007, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.