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
(--------------------完---------------------)