// Сервер#include "myserver.h"MyServer::MyServer(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags){ ui.setupUi(this); h_addr=new QHostAddress("192.168.13.9"); m_ptcpServer=new QTcpServer(this); if(m_ptcpServer->listen(*h_addr, 2323)); { QMessageBox::critical(0,"Server Error","Unable to start the server:"+m_ptcpServer->serverError()); m_ptcpServer->close(); close(); } connect(m_ptcpServer,SIGNAL(newConnection()),SLOT(slotNewConnection()));}MyServer::~MyServer(){}void MyServer::slotNewConnection(){ QTcpSocket * pClientSocket=m_ptcpServer->nextPendingConnection(); connect(pClientSocket,SIGNAL(disconnected()),SLOT(deleteLater())); connect(pClientSocket,SIGNAL(readyRead()),SLOT(slotReadClient())); sendToClient(pClientSocket,"Server Response: Connected!");}void MyServer::slotReadClient(){ QTcpSocket* pClientSocket=(QTcpSocket*)sender(); QDataStream in(pClientSocket); in.setVersion(QDataStream::Qt_4_3); for(;;) { if(!m_bNextBlockSize) { if(pClientSocket->bytesAvailable()< sizeof(qint16)) break; in>> m_bNextBlockSize; } if(pClientSocket->bytesAvailable()< m_bNextBlockSize) break; QTime time; QString str; in>>time>>str; QString strMessage; strMessage=time.toString()+" "+"Client has send - "+str; ui.textEdit->setText(strMessage); m_bNextBlockSize=0; }}void MyServer::sendToClient(QTcpSocket* pSock, const QString& str){ QByteArray arrBlock; QDataStream out(&arrBlock,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_3); out<< qint16(0)<<QTime::currentTime()<<str; out.device()->seek(0); out<<qint16(arrBlock.size()-sizeof(qint16)); pSock->write(arrBlock);}
//Клиент#include "myclient.h"myClient::myClient(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags){ ui.setupUi(this); m_pTcpSocket=new QTcpSocket(this); m_pTcpSocket->connectToHost("192.168.13.12",2323); connect(m_pTcpSocket,SIGNAL(connected()),SLOT(slotConnected())); connect(m_pTcpSocket,SIGNAL(readyRead()),SLOT(slotConnected())); connect(m_pTcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotError(QAbstractSocket::SocketError))); connect(ui.textEdit,SIGNAL(returnPressed()),this,SLOT(slotSendToServer()));}myClient::~myClient(){}void myClient::on_pushButton_clicked(){ slotSendToServer();}void myClient::slotReadyRead(){ QDataStream in(m_pTcpSocket); in.setVersion(QDataStream::Qt_4_3); for(;;) { if(!m_nNextBlockSize) { if(m_pTcpSocket->bytesAvailable()< sizeof(qint16)) break; in>> m_nNextBlockSize; } if(m_pTcpSocket->bytesAvailable()< m_nNextBlockSize) break; QTime time; QString str; in>>time>>str; QString strMessage; strMessage=time.toString()+" "/*+"Server has send - "*/+str; ui.textEdit->setText(strMessage); m_nNextBlockSize=0; }}void myClient::slotError(QAbstractSocket::SocketError err){ QString strError= "Error:"+(err==QAbstractSocket::HostNotFoundError? "The host was not found." : err==QAbstractSocket::RemoteHostClosedError ? "The remote host is closed." : err==QAbstractSocket::ConnectionRefusedError ? "The connection was refused." : QString(m_pTcpSocket->errorString()) ); ui.textEdit->setText(strError);}void myClient::slotConnected(){ ui.textEdit->setText("Received the connected() signal");}void myClient::slotSendToServer(){ QByteArray arrBlock; QDataStream out(&arrBlock,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_3); out<< qint16(0)<<QTime::currentTime()<<ui.lineEdit->text(); out.device()->seek(0); out<<qint16(arrBlock.size()-sizeof(qint16)); m_pTcpSocket->write(arrBlock); ui.lineEdit->setText("");}
C++ (Qt)if(m_ptcpServer->listen(*h_addr, 2323));{ QMessageBox::critical(0,"Server Error","Unable to start the server:"+m_ptcpServer->serverError()); m_ptcpServer->close(); close();}
C++ (Qt)if(m_ptcpServer->listen(*h_addr, 2323)){ QMessageBox::critical(0,"Server Error","Unable to start the server:"+m_ptcpServer->serverError()); m_ptcpServer->close(); close();}
C++ (Qt)if(!m_ptcpServer->listen(*h_addr, 2323)){ QMessageBox::critical(0,"Server Error","Unable to start the server:"+m_ptcpServer->serverError()); m_ptcpServer->close(); close();}