#ifndef TCP_client_H#define TCP_client_H#include <QDialog>#include <QWidget>#include <QVBoxLayout>#include <QTcpSocket>#include <QTime>#include <QDataStream>#include <QTextEdit>#include <QLineEdit>#include <QLabel>#include <QPushButton>class TCP_client : public QDialog{ Q_OBJECTprivate: QTcpSocket* m_pTcpSocket; quint16 m_nNextBlockSize; QTextEdit* m_ptxtInfo; QLineEdit* m_ptxtInput;public: TCP_client (QWidget* pobj = 0);private slots: void slotReadyRead(); void slotError(QAbstractSocket::SocketError); void slotConnnected(); void slotSendToServer();};#endif // TCP_H
#include "tcp_client.h"TCP_client::TCP_client(QWidget *pwgt) : QDialog(pwgt), m_nNextBlockSize(0){ m_pTcpSocket = new QTcpSocket(this); m_pTcpSocket ->connectToHost("10.28.17.164", 1000); connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnnected())); 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; m_ptxtInfo->setReadOnly(true); QPushButton* pcmd = new QPushButton("&send"); connect(pcmd, SIGNAL(clicked()), SLOT(slotSendToServer())); connect(m_ptxtInput, SIGNAL(returnPressed()), this, SLOT(slotSendToServer())); //-------------отоброжение---------------..// 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 TCP_client::slotSendToServer(){ QByteArray arrBlock; QDataStream out(&arrBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_5); out<<qint16(0) << m_ptxtInput->text(); out.device()->seek(0); out<<qint16(arrBlock.size() - sizeof(qint16)); m_pTcpSocket->write(arrBlock); m_ptxtInput ->setText("");}//--------------------считывание собщения-------------------------//void TCP_client::slotReadyRead(){ QDataStream in(m_pTcpSocket); in.setVersion(QDataStream::Qt_5_5); 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>>str; m_ptxtInfo ->append(str); m_nNextBlockSize = 0; }}//-----------------------------ошибки-----------------------------//void TCP_client::slotError(QAbstractSocket::SocketError err){ QString strError = "Error: " +(err == QAbstractSocket::HostNotFoundError ? "The host is not font." : err == QAbstractSocket::RemoteHostClosedError ? "The remote host is closed" : err == QAbstractSocket::ConnectionRefusedError ? "The conection was refused" : QString (m_pTcpSocket->errorString()) ); m_ptxtInfo ->append( strError);}void TCP_client::slotConnnected(){ m_ptxtInfo ->append("Соеденение устанволенно!");}
#!/usr/bin/perl#tcpclient.pluse IO::Socket::INET;# flush after every write$| = 1;my ($socket,$client_socket);# creating object interface of IO::Socket::INET modules which internally creates# socket, binds and connects to the TCP server running on the specific port.$socket = new IO::Socket::INET (PeerHost => '192.168.0.6',PeerPort => '1000',Proto => 'tcp',) or die "ERROR in Socket Creation : $!\n";print "TCP Connection Success.\n";# write on the socket to server.$data = "DATA from Client";print $socket "$data\n";# we can also send the data through IO::Socket::INET module,# $socket->send($data);# read the socket data sent by server.$data = <$socket>;# we can also read from socket through recv() in IO::Socket::INET# $socket->recv($data,1024);print "Received from Server : $data\n";sleep (10);$socket->close();
void TCP_client::slotSendToServer(){ QByteArray arrBlock; QDataStream out(&arrBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_5); out<<qint16(0) << m_ptxtInput->text(); out.device()->seek(0); out<<qint16(arrBlock.size() - sizeof(qint16)); QTextCodec* data; m_pTcpSocket->write(data->toUnicode(arrBlock)); m_ptxtInput ->setText("");}
C++ (Qt)QTextCodec *codec = QTextCodec::codecForName( "UTF-8" );out<<qint16(0) << codec->fromUnicode( m_ptxtInput->text() );
void TCP_client::slotSendToServer(){ QByteArray arrBlock; QDataStream out(&arrBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_5); QTextCodec *codec = QTextCodec::codecForName( "UTF-8" ); out<<qint16(0) << codec->fromUnicode( m_ptxtInput->text() ); out.device()->seek(0); out<<qint16(arrBlock.size() - sizeof(qint16)); m_pTcpSocket->write(arrBlock); m_ptxtInput ->setText("");}
out<<qint16(arrBlock.size() - sizeof(qint16));
C++ (Qt)void TCP_client::slotSendToServer(){ QTextCodec *codec = QTextCodec::codecForName( "UTF-8" ); m_pTcpSocket->write( codec->fromUnicode( m_ptxtInput->text() ) ); m_ptxtInput ->setText("");}
QByteArrey data;QTextCodec* codec = QTextCodec::codecForName("UTF-8");m_pTcpSocket->read(data);m_ptxtInfo->append(codec->fromUnicode(data.toStdString());
C++ (Qt)QTextCodec* codec = QTextCodec::codecForName("UTF-8");QByteArrey data = m_pTcpSocket->readAll();m_ptxtInfo->append(codec->fromUnicode(data.toStdString());
conversion from 'std::string(aka std::basic_string<char>) to non scalar type QString reguested