#include <QtGui>
#include <QImage>
#include "kuvaclient.h"
// including <QtGui> saves us to include every class user, <QString>, <QFileDialog>,...
KuvaClient
::KuvaClient(QDialog *parent
){
setupUi(this); // this sets up GUI
file = new QFile("testi.bmp");
if(file->exists())
file->remove();
numberFrame=0;
tcpSocket->abort();
tcpSocket->connectToHost("masa",1200);
connect(vasen, SIGNAL( clicked() ), this, SLOT( Vasenpaina()));
connect(oikea, SIGNAL( clicked() ), this, SLOT( Oikeapaina()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readImage()));
}
void KuvaClient::Vasenpaina()
{
tcpSocket->abort();
tcpSocket->connectToHost("localhost",1200);
numberFrame=numberFrame+1;
QString number
=number.
setNum(numberFrame
);
//koe->setText(number);
tcpSocket->write(number);
}
void KuvaClient::readImage()
{
//tcpSocket->seek(0);
char *buffer = new char[65536];
//int count = tcpSocket->bytesAvailable();
//sockStream->resetStatus();
while(!sockStream->atEnd())
{
int src = sockStream->readRawData(buffer, 65536);
int rcv = fileStream->writeRawData(buffer, src);
int xxx = 0;
xxx++;
}
delete[] buffer;
file->flush();
QImage someimage
("testi.bmp");
preview = preview.fromImage(someimage);
label->setPixmap(preview);
tcpSocket->waitForBytesWritten();
}
void KuvaClient::Oikeapaina()
{
tcpSocket->abort();
tcpSocket->connectToHost("locahost",1200);
numberFrame++;
/*QString number=number.setNum(numberFrame);
tcpSocket->write(number.toLatin1() );
tcpSocket->deleteLater();
*/
}
{
switch (socketError) {
break;
tr("The host was not found. Please check the "
"host name and port settings."));
break;
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
}
#include <QtGui>
#include <QImage>
#include "kuvaclient.h"
// including <QtGui> saves us to include every class user, <QString>, <QFileDialog>,...
KuvaClient::KuvaClient(QDialog *parent)
{
setupUi(this); // this sets up GUI
tcpSocket = new QTcpSocket(this);
sockStream = new QDataStream(tcpSocket);
file = new QFile("testi.bmp");
if(file->exists())
file->remove();
file->open(QIODevice::ReadWrite);
numberFrame=0;
fileStream = new QDataStream(file);
tcpSocket->abort();
tcpSocket->connectToHost("masa",1200);
connect(vasen, SIGNAL( clicked() ), this, SLOT( Vasenpaina()));
connect(oikea, SIGNAL( clicked() ), this, SLOT( Oikeapaina()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readImage()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
void KuvaClient::Vasenpaina()
{
tcpSocket->abort();
tcpSocket->connectToHost("localhost",1200);
numberFrame=numberFrame+1;
QString number=number.setNum(numberFrame);
//koe->setText(number);
tcpSocket->write(number);
}
void KuvaClient::readImage()
{
//tcpSocket->seek(0);
char *buffer = new char[65536];
//int count = tcpSocket->bytesAvailable();
//sockStream->resetStatus();
while(!sockStream->atEnd())
{
int src = sockStream->readRawData(buffer, 65536);
int rcv = fileStream->writeRawData(buffer, src);
int xxx = 0;
xxx++;
}
delete[] buffer;
file->flush();
QImage someimage("testi.bmp");
QPixmap preview;
preview = preview.fromImage(someimage);
label->setPixmap(preview);
tcpSocket->waitForBytesWritten();
}
void KuvaClient::Oikeapaina()
{
tcpSocket->abort();
tcpSocket->connectToHost("locahost",1200);
numberFrame++;
/*QString number=number.setNum(numberFrame);
tcpSocket->write(number.toLatin1() );
tcpSocket->deleteLater();
*/
}
void KuvaClient::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
}
To copy to clipboard, switch view to plain text mode
This is my client code.It' writes message to server,but i can't read message from socket in the server side.
Bookmarks