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_OBJECT
- public:
- 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_OBJECT
- public:
- 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
(--------------------完---------------------)