Qt與Java實現Tcp網路通訊,收發簡單字串。

來源:互聯網
上載者:User

     qt與java實現簡單的網路通訊,java程式位於ip為172.23.33.30的電腦,Qt程式位於ip為172.23.33.16的電腦上。

1.java接受Qt發送的字串。

java代碼:

import java.io.IOException;import java.io.InputStream;import java.net.Socket;import java.net.UnknownHostException;public class ConnectTest {     public ConnectTest(){     try {     InputStream is=new Socket("172.23.33.16",8888).getInputStream();     byte[] by=new byte[1024];     is.read(by);     String str=new String(by);     System.out.println(str);     is.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}     }          public static void main(String[] args){     new ConnectTest();     }}

Qt代碼:

/*********TestNet.h***********/#ifndef _TESTNET_H_#define _TESTNET_H_#include <iostream>#include <QtNetwork/QTcpServer>#include <QtNetwork/QHostAddress>#include <QtNetwork/QTcpSocket>class TestNet : public QObject{  Q_OBJECTpublic:   TestNet();   ~TestNet();public slots:   void getConnect();   private:   QTcpServer *server;   QTcpSocket *socket;};#endif

/**********TestNet.cpp**********/#include "TestNet.h"TestNet::TestNet(){    server=new QTcpServer(this);    server->listen(QHostAddress::Any,8888);    QObject::connect(server,SIGNAL(newConnection()),this,SLOT(getConnect()));}TestNet::~TestNet(){}void TestNet::getConnect(){    std::cout<<"here_1"<<std::endl;    socket=server->nextPendingConnection();    std::cout<<"here_2"<<std::endl;    std::cout<<"here_3"<<std::endl;    QString strMesg="Hello,World!是不是?";     socket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str()));}

/*********Main.cpp**********/#include "TestNet.h"#include <QtGui/QApplication>int main(int argc,char *argv[]){  QApplication a(argc,argv);  TestNet *test=new TestNet();  return a.exec();}

實驗效果:

java端

Qt端

-----------------------------------------------------------------------------------------------------------------------------------------------

2.Qt接受java發送的字串。

java代碼:

import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;public class ConnectTest2 {     public ConnectTest2(){     try {     OutputStream os=new Socket("172.23.33.16",8888).getOutputStream();     String temp=new String("Hello,I am that boy! 是不是?");     os.write(temp.getBytes("UTF-8"));     os.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}     }          public static void main(String[] args){     new ConnectTest2();     }}

Qt代碼:

/************TestNet.h***************/#ifndef _TESTNET_H_#define _TESTNET_H_#include <iostream>#include <QtNetwork/QTcpServer>#include <QtNetwork/QHostAddress>#include <QtNetwork/QTcpSocket>#include <QtGui/QMessageBox>class TestNet : public QObject{  Q_OBJECTpublic:   TestNet();   ~TestNet();public slots:   void getConnect();   void readMessage();   private:   QTcpServer *server;   QTcpSocket *socket;};#endif

/**********TestNet.cpp************/#include "TestNet.h"TestNet::TestNet(){    server=new QTcpServer(this);    server->listen(QHostAddress::Any,8888);    QObject::connect(server,SIGNAL(newConnection()),this,SLOT(getConnect()));}TestNet::~TestNet(){}void TestNet::getConnect(){    socket=server->nextPendingConnection();    QObject::connect(socket,SIGNAL(readyRead()),this,SLOT(readMessage()));}void TestNet::readMessage(){    QByteArray qba=socket->readAll();    std::cout<<qba.data()<<std::endl;    QString ss=QVariant(qba).toString();    QMessageBox::information(NULL,ss,ss);}

/***********Main.cpp************/#include "TestNet.h"#include <QtGui/QApplication>#include <QtCore/QTextCodec> int main(int argc,char *argv[]){  QApplication a(argc,argv);  QTextCodec *codec = QTextCodec::codecForLocale();  QTextCodec::setCodecForCStrings(codec);  TestNet *test=new TestNet();  return a.exec();}

效果:

Qt端

注意:編譯Qt網路相關的程式時,需要在qmake -project產生的xx.pro檔案中加入:

QT +=core
gui network

(--------------------完---------------------)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.