Sunday, July 6th 2008, 7:35pm UTC+1

You are not logged in.

  • Login
  • Register

1

Tuesday, May 6th 2008, 10:45am

sending a fil in QTcpSocke programming

dear friends, i have written two applications.
One is client application using QTcpSocket class.
Another is server application using QTcpServer class.
I am now able to send the string from client to server and viceversa as well.
i have used QDatastream object, normally what has been given in fortune client example.

Now i want to send a file from client to server. But i am not getting how should i start the coding for the file transfer from client to server.....

Plz help me yaar..
  • Go to the top of the page

2

Tuesday, May 6th 2008, 3:30pm

RE: sending a fil in QTcpSocke programming

define a communication protocol, such as:
- command to execute
- name of the file
- size of the file
- file
what seems you necessary
- perhaps server states like WaitCommand, WaitName, WaitSize, WaitFile ... to manage reading on the socket

client: write according to this protocol
server: read according to this protocol in a slot connected to readyRead() signal
keeping in mind all data will not be received at one time, so you probably need to bufferize
Nicolas

This post has been edited 1 times, last edit by "Nicolas SOUCHON" (May 6th 2008, 3:33pm)

  • Go to the top of the page

3

Tuesday, May 6th 2008, 7:00pm

RE: sending a fil in QTcpSocke programming

dear friend,
i am using the following like code for sending strings to other aplication.

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << "Welcome"
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));

clientConnection->write(block);
clientConnection->disconnectFromHost();


So i dont know how to use this type of code for sending a file..
If anybody having sample code for file transfer plz share it..
plz help me..
  • Go to the top of the page

4

Tuesday, May 6th 2008, 9:53pm

RE: sending a fil in QTcpSocke programming

What OS are you on? If non windows, using the native sockets are so much easier IMO.

IF windows, winsock is ok. if non windows, consider looking up Sockets.h. I use it all of the time and it is fine
  • Go to the top of the page

5

Wednesday, May 7th 2008, 7:38am

RE: sending a fil in QTcpSocke programming

i am in windows only..
But i want to transfer the file using QTcpSocket class only.
I dont want to use any native OS related classes..
If anybody is having sample code of transfering file using QTcpSocket, the plz share with me...
  • Go to the top of the page

6

Wednesday, May 7th 2008, 12:24pm

RE: sending a fil in QTcpSocke programming

try something such as

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
QByteArray  block;
QDataStream out( &block, QIODevice::WriteOnly );
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0
    << fileName
    << fileSize;
QFile file( "someFile" );
file.open( QIODevice::ReadOnly );
while( ! file.atEnd() )
{
    QByteArray arr = file.read( 0x1000 );
    block.append( arr );
}
file.close();out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
Nicolas
  • Go to the top of the page

Rate this thread