PDA

View Full Version : Sending a file while chatting QTcpSocket and QTcpServer



samuele89
7th January 2013, 13:55
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 :D

Santosh Reddy
7th January 2013, 14:01
One simple way, have tags in the chatting message

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

you could also use some binary tags (instead of text) to keep the parsing simple.

samuele89
7th January 2013, 14:37
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);
}

Santosh Reddy
7th January 2013, 14:56
You need to add a mechanism (in readyRead()) to differentiate between a chat message, and a file message. One way I suggested earlier

samuele89
7th January 2013, 15:24
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 -.-'

anda_skoa
8th January 2013, 11:44
I would go with your initial idea instead and use a separate connection for the file transfer.

Cheers,
_

samuele89
8th January 2013, 14:51
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".