QT and Java implement TCP network communication and send and receive simple strings.

Source: Internet
Author: User

Qt implements simple network communication with Java. the Java program is located on a computer with the IP address 172.23.33.30, And the QT program is located on a computer with the IP address 172.23.33.16.

1. Java receives the string sent by QT.

Java code:

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 code:

/*********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! Right? "; 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();}

Lab results:

Java end

Qt end

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

2. QT accepts Strings sent by Java.

Java code:

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! Right? "); 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 code:

/************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();}

Effect:

Qt end

Note: To compile a program related to the QT network, add the following to the XX. Pro file generated by qmake-Project:

Qt + = Core
Gui Network

---------------------)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.