qt与java实现简单的网络通信

 qt与java实现简单的网络通信,java程序位于ip为172.23.33.30的计算机,Qt程序位于ip为172.23.33.16的计算机上。

1.java接受Qt发送的字符串。

java代码:

[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.net.Socket;  
  4. import java.net.UnknownHostException;  
  5.   
  6. public class ConnectTest {  
  7.      public ConnectTest(){  
  8.          try {  
  9.              InputStream is=new Socket("172.23.33.16",8888).getInputStream();  
  10.              byte[] by=new byte[1024];  
  11.              is.read(by);  
  12.              String str=new String(by);  
  13.              System.out.println(str);  
  14.              is.close();  
  15.         } catch (UnknownHostException e) {  
  16.             e.printStackTrace();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.      }  
  21.        
  22.      public static void main(String[] args){  
  23.          new ConnectTest();  
  24.      }  
  25. }  

Qt代码:

[cpp]  view plain copy
  1. /*********TestNet.h***********/  
  2.   
  3. #ifndef _TESTNET_H_  
  4. #define _TESTNET_H_  
  5.   
  6. #include <iostream>  
  7. #include <QtNetwork/QTcpServer>  
  8. #include <QtNetwork/QHostAddress>  
  9. #include <QtNetwork/QTcpSocket>  
  10.   
  11. class TestNet : public QObject  
  12. {  
  13.   Q_OBJECT  
  14. public:  
  15.    TestNet();  
  16.    ~TestNet();  
  17. public slots:  
  18.    void getConnect();  
  19.    private:  
  20.    QTcpServer *server;  
  21.    QTcpSocket *socket;  
  22. };  
  23.   
  24. #endif  

[cpp]  view plain copy
  1. /**********TestNet.cpp**********/  
  2.   
  3. #include "TestNet.h"  
  4.   
  5. TestNet::TestNet()  
  6. {  
  7.     server=new QTcpServer(this);  
  8.     server->listen(QHostAddress::Any,8888);  
  9.     QObject::connect(server,SIGNAL(newConnection()),this,SLOT(getConnect()));  
  10. }  
  11.   
  12. TestNet::~TestNet()  
  13. {  
  14. }  
  15.   
  16. void TestNet::getConnect()  
  17. {  
  18.     std::cout<<"here_1"<<std::endl;  
  19.     socket=server->nextPendingConnection();  
  20.     std::cout<<"here_2"<<std::endl;  
  21.     std::cout<<"here_3"<<std::endl;  
  22.     QString strMesg="Hello,World!是不是?";   
  23.     socket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str()));  
  24. }  

[cpp]  view plain copy
  1. /*********Main.cpp**********/  
  2.   
  3. #include "TestNet.h"  
  4.   
  5. #include <QtGui/QApplication>  
  6.   
  7. int main(int argc,char *argv[])  
  8. {  
  9.   QApplication a(argc,argv);  
  10.   TestNet *test=new TestNet();  
  11.   return a.exec();  
  12. }  

实验效果截图:

java端

Qt端

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

2.Qt接受java发送的字符串。

java代码:

[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.io.OutputStream;  
  3. import java.net.Socket;  
  4. import java.net.UnknownHostException;  
  5.   
  6. public class ConnectTest2 {  
  7.      public ConnectTest2(){  
  8.          try {  
  9.              OutputStream os=new Socket("172.23.33.16",8888).getOutputStream();  
  10.              String temp=new String("Hello,I am that boy! 是不是?");  
  11.              os.write(temp.getBytes("UTF-8"));  
  12.              os.close();  
  13.         } catch (UnknownHostException e) {  
  14.             e.printStackTrace();  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.      }  
  19.        
  20.      public static void main(String[] args){  
  21.          new ConnectTest2();  
  22.      }  
  23. }  

Qt代码:

[cpp]  view plain copy
  1. /************TestNet.h***************/  
  2.   
  3. #ifndef _TESTNET_H_  
  4. #define _TESTNET_H_  
  5.   
  6. #include <iostream>  
  7. #include <QtNetwork/QTcpServer>  
  8. #include <QtNetwork/QHostAddress>  
  9. #include <QtNetwork/QTcpSocket>  
  10. #include <QtGui/QMessageBox>  
  11.   
  12. class TestNet : public QObject  
  13. {  
  14.   Q_OBJECT  
  15. public:  
  16.    TestNet();  
  17.    ~TestNet();  
  18. public slots:  
  19.    void getConnect();  
  20.    void readMessage();  
  21.    private:  
  22.    QTcpServer *server;  
  23.    QTcpSocket *socket;  
  24. };  
  25.   
  26. #endif  

[cpp]  view plain copy
  1. /**********TestNet.cpp************/  
  2.   
  3. #include "TestNet.h"  
  4.   
  5. TestNet::TestNet()  
  6. {  
  7.     server=new QTcpServer(this);  
  8.     server->listen(QHostAddress::Any,8888);  
  9.     QObject::connect(server,SIGNAL(newConnection()),this,SLOT(getConnect()));  
  10. }  
  11.   
  12. TestNet::~TestNet()  
  13. {  
  14. }  
  15.   
  16. void TestNet::getConnect()  
  17. {  
  18.     socket=server->nextPendingConnection();  
  19.     QObject::connect(socket,SIGNAL(readyRead()),this,SLOT(readMessage()));  
  20. }  
  21.   
  22. void TestNet::readMessage()  
  23. {  
  24.     QByteArray qba=socket->readAll();  
  25.     std::cout<<qba.data()<<std::endl;  
  26.     QString ss=QVariant(qba).toString();  
  27.     QMessageBox::information(NULL,ss,ss);  
  28. }  

[cpp]  view plain copy
  1. /***********Main.cpp************/  
  2.   
  3. #include "TestNet.h"  
  4. #include <QtGui/QApplication>  
  5. #include <QtCore/QTextCodec>   
  6.   
  7. int main(int argc,char *argv[])  
  8. {  
  9.   QApplication a(argc,argv);  
  10.   QTextCodec *codec = QTextCodec::codecForLocale();  
  11.   QTextCodec::setCodecForCStrings(codec);  
  12.   TestNet *test=new TestNet();  
  13.   return a.exec();  
  14. }  

效果截图:

Qt端


注意:编译Qt网络相关的程序时,需要在qmake -project生成的xx.pro文件中加入:

QT +=core gui network


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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值