'Client1: Used to test all simple Xserver demosOption ExplicitPrivate Sub CmdClear_Click() List1.ClearEnd SubPrivate Sub cmdClose_Click() cmdConnect.Enabled = True cmdClose.Enabled = False cmdSend.Enabled = False Winsock1.Close StatusBar1.Panels(1).Text = "Status: Disconnect."End SubPrivate Sub cmdConnect_Click() Winsock1.RemoteHost = ServeripText.Text Winsock1.RemotePort = ServerPortText.Text Winsock1.ConnectEnd SubPrivate Sub CmdEXIT_Click() Winsock1.Close Unload MeEnd SubPrivate Sub cmdSend_Click() If Winsock1.State = sckConnected Then Winsock1.SendData txtsend.Text Else MsgBox "Not currently connected" End IfEnd SubPrivate Sub Form_Load() Caption = "Client side host name: " & Winsock1.LocalHostName & ", IP: " & Winsock1.LocalIP cmdConnect.Enabled = True cmdClose.Enabled = False cmdSend.Enabled = False StatusBar1.Panels(1).Text = "Status: Disconnect."End SubPrivate Sub Winsock1_Close() MsgBox "Winsock1_Close"End Sub' Connect event fired when the client connectsPrivate Sub Winsock1_Connect() Dim vtdata As Variant If Winsock1.State = sckConnected Then StatusBar1.Panels(1).Text = "Status: Connection to " & Winsock1.RemoteHostIP & " successful." cmdConnect.Enabled = False cmdSend.Enabled = True cmdClose.Enabled = True End IfEnd Sub' DataArrival event is fired when the Winsock1 receive dataPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim vtdata As Variant Winsock1.GetData vtdata, vbString List1.AddItem vtdataEnd Sub
#ifndef _MyClient_h_#define _MyClient_h_#include <QWidget>#include <QTcpSocket>class QTextEdit;class QLineEdit;// ======================================================================class MyClient : public QWidget {Q_OBJECTprivate: QTcpSocket* m_pTcpSocket; QTextEdit* m_ptxtInfo; QLineEdit* m_ptxtInput; quint16 m_nNextBlockSize;public: MyClient(const QString& strHost, int nPort, QWidget* pwgt = 0) ;private slots: void slotReadyRead ( ); void slotError (QAbstractSocket::SocketError); void slotSendToServer( ); void slotConnected ( );};#endif
#include <QApplication>#include "MyClient.h"// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); MyClient client("192.168.0.41", 10000); client.show(); return app.exec();}
#include <QtNetwork>#include <QtGui>#include "MyClient.h"// ----------------------------------------------------------------------MyClient::MyClient(const QString& strHost, int nPort, QWidget* pwgt /*=0*/ ) : QWidget(pwgt) , m_nNextBlockSize(0){ m_pTcpSocket = new QTcpSocket(this); m_pTcpSocket->connectToHost(strHost, nPort); connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected())); connect(m_pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead())); connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)) ); m_ptxtInfo = new QTextEdit; m_ptxtInput = new QLineEdit; connect(m_ptxtInput, SIGNAL(returnPressed()), this, SLOT(slotSendToServer()) ); m_ptxtInfo->setReadOnly(true); QPushButton* pcmd = new QPushButton("&Send"); connect(pcmd, SIGNAL(clicked()), SLOT(slotSendToServer())); //Layout setup QVBoxLayout* pvbxLayout = new QVBoxLayout; pvbxLayout->addWidget(new QLabel("<H1>Client</H1>")); pvbxLayout->addWidget(m_ptxtInfo); pvbxLayout->addWidget(m_ptxtInput); pvbxLayout->addWidget(pcmd); setLayout(pvbxLayout);}// ----------------------------------------------------------------------void MyClient::slotReadyRead(){ QDataStream in(m_pTcpSocket); in.setVersion(QDataStream::Qt_4_2); for (;;) { if (!m_nNextBlockSize) { if (m_pTcpSocket->bytesAvailable() < sizeof(quint16)) { break; } in >> m_nNextBlockSize; } if (m_pTcpSocket->bytesAvailable() < m_nNextBlockSize) { break; } QTime time; QString str; in >> time >> str; m_ptxtInfo->append(time.toString() + " " + str); 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()) ); m_ptxtInfo->append(strError);}// ----------------------------------------------------------------------void MyClient::slotSendToServer(){ QByteArray arrBlock; QDataStream out(&arrBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_2); out << quint16(0) << QTime::currentTime() << m_ptxtInput->text(); out.device()->seek(0); out << quint16(arrBlock.size() - sizeof(quint16)); m_pTcpSocket->write(arrBlock); // m_ptxtInput->setText("");}// ------------------------------------------------------------------void MyClient::slotConnected(){ m_ptxtInfo->append("Received the connected() signal");}
void MyClient::slotReadyRead(){QByteArray qwe = m_pTcpSocket->readAll(); m_ptxtInfo->append(QString(qwe));}void MyClient::slotSendToServer(){ m_pTcpSocket->write("22"); }