由 muddy » 週一 12月 11, 2006 4:37 pm
Server
void Open()
{
FileName = QFileDialog::getOpenFileName(this,tr("Open File"),"/",tr("Image(*.png *.jpg *.bmp)"));
pixmap.load(FileName);
MyServer = new QTcpServer(this);
MyServer->listen(QHostAddress::LocalHost,12345);
connect(MyServer, SIGNAL(newConnection()), this, SLOT(SendData()));
}
void SendData()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << FileName;
out << pixmap;
QTcpSocket *clientConnection = MyServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
clientConnection->disconnectFromHost();
MyServer->close();
}
Client
void Connect()
{
MySocket = new QTcpSocket(this);
MySocket->connectToHost(QHostAddress::LocalHost,12345,QIODevice::ReadOnly);
connect(MySocket, SIGNAL(readyRead()), this,SLOT(ReadData()));
}
void ReadData()
{
QDataStream in(MySocket);
in.setVersion(QDataStream::Qt_4_0);
in >> FileName;
in >> pixmap;
}