Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A qt client connect with a java server
Qt 6.11 is out! See what's new in the release blog

A qt client connect with a java server

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 9.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andreyc
    wrote on last edited by
    #6

    What you will get if you try to read input without processing it.
    @
    public void run() {
    System.out.println("Server Thread " + ID + " running.");
    while (true) {
    try {
    System.out.println(streamIn.readUTF());
    //server.handle(ID, streamIn.readUTF());
    } catch (IOException ioe) {
    System.out.println(ID + " ERROR reading: " + ioe.getMessage());
    server.remove(ID);
    stop();
    }
    }
    }
    @

    [EDIT] Did you try to use wireshark and, or netcat?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #7

      sr but I dont understand how to use both of them.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #8

        I tried your changes but nothing!
        It goes to catch.

        The error display when I close the client!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #9

          Have you tried another client like ncat or netcat ?
          Have you tried wireshark to see what is sent ?

          [quote author="parkir" date="1407169299"]But when I use the Qt client with a server written in Qt, everything is ok and respectively for the java client and server.[/quote]

          You can compare what is sent between a Java client and Java Server with Qt client and Java server using wireshark.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #10

            With the wireshark i can't understand whta happen and how to run but I am running the Zenmap, tell me what parameters to insert to give you the results..

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #11

              IIRC(If I Memeber Correctly) Zenmap is for scanning the computer ports not for sniffing a traffic.

              But anyway. I tested your java code and simple client. My client connects to server, sends text and disconnect.

              I see that client connect to java server and sends text and disconnect.
              The error message 55130 ERROR reading: null appears when a client is disconnected, which is ok.

              The same happens if I use ncat.

              I don't see that ChatServer::handle() is called. I guess something should be done in the server to process the input.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on last edited by
                #12

                Here is modified ChatServerThread.java that works.
                At least as I think it should work.

                • ChatServerThread.java
                  @
                  import java.net.;
                  import java.io.
                  ;

                public class ChatServerThread extends Thread
                {
                private ChatServer server = null;
                private Socket socket = null;
                private int ID = -1;
                private BufferedReader streamIn = null;
                private BufferedWriter streamOut = null;

                public ChatServerThread(ChatServer _server, Socket _socket)
                {
                super();
                server = _server;
                socket = _socket;
                ID = socket.getPort();
                }

                public void send(String msg)
                {
                try {
                streamOut.write(msg + "\n");
                streamOut.flush();
                }
                catch (IOException ioe) {
                System.out.println(ID + " ERROR sending: " + ioe.getMessage());
                server.remove(ID);
                stop();
                }
                }

                public int getID()
                {
                return ID;
                }

                public void run()
                {
                System.out.println("Server Thread " + ID + " running.");
                while (true) {
                try {
                String input = streamIn.readLine();
                if (input != null) {
                server.handle(ID, input);
                } else {
                System.out.println(ID + " we lost the client :-(.\n");
                server.remove(ID);
                stop();
                }
                }
                catch (IOException ioe) {
                System.out.println(ID + " ERROR reading: " + ioe.getMessage());
                server.remove(ID);
                stop();
                }
                }
                }

                public void open() throws IOException
                {
                streamIn =
                new BufferedReader(new InputStreamReader(socket.getInputStream()));
                streamOut =
                new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                }

                public void close() throws IOException
                {
                if (socket != null)
                socket.close();

                if (streamIn != null)
                  streamIn.close();
                
                if (streamOut != null)
                  streamOut.close();
                

                }
                }
                @

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #13

                  I think so the problem is the client..

                  I post you the mainwindow.cpp

                  @#include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QStackedWidget>
                  #include "about.h"
                  #include <QMessageBox>

                  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
                  ui(new Ui::MainWindow)
                  {
                  ui->setupUi(this);

                  //set background color
                  setStyleSheet("background-color: rgb(87,87,87)");
                  
                  //the login group
                  ui->loginGroup->setStyleSheet("background-color: rgb(112,128,144); color:white");
                  
                  //button
                  ui->btn_login->setStyleSheet("QPushButton { background-color: rgb(176,196,222); color:black }");
                  ui->btnSend->setStyleSheet("QPushButton { background-color: rgb(176,196,222); color:black }");
                  
                  //label
                  ui->label_welcome->setStyleSheet("QLabel {font-family:Pristina; font-size: 30px; color:white;}");
                  
                  //QlineEdit
                  ui->txt_nickname->setStyleSheet("QLineEdit {background-color: white; color:black}");
                  ui->txt_serverIP->setStyleSheet("QLineEdit {background-color: white; color:black}");
                  ui->msgArea->setStyleSheet("QLineEdit {background-color: white; color:black}");
                  
                  //the place who display everything from the chat room
                  ui->roomTextMsg->setStyleSheet("background-color: rgb(112,128,144); color:white");
                  
                  //set the IP of my server
                  ui->txt_serverIP->setText("localhost");
                  
                  //menu bar
                  ui->menuAbout->setStyleSheet("background-color: rgb(112,128,144); color:white");
                  
                  socket = new QTcpSocket(this);
                  
                  ui->stackedWidget->setCurrentWidget(ui->loginPage);
                  
                  //socket->write(QString("\n"+nickName ).toUtf8());
                  
                  connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
                  connect(socket, SIGNAL(connected()), this, SLOT(connected()));
                  

                  }

                  MainWindow::~MainWindow()
                  {
                  delete ui;
                  thread->deleteLater();
                  }

                  void MainWindow::displayMsg(QByteArray sentMsg)
                  {
                  QString msg = QString(sentMsg.data());
                  ui->roomTextMsg->append(msg);
                  }

                  void MainWindow::sendTheMsg()
                  {
                  QByteArray buffer;
                  buffer.append(nickName);
                  buffer.append(": ");
                  buffer.append(ui->msgArea->text());
                  socket->write("<i>"+ buffer +"</i>");
                  qDebug() << buffer;

                  ui->msgArea->clear();
                  

                  }

                  void MainWindow::on_btnSend_clicked()
                  {
                  if( !(ui->msgArea->text() == ""))
                  {
                  sendTheMsg();
                  }
                  }

                  void MainWindow::readyRead()
                  {
                  thread = new QThread();
                  ui->roomTextMsg->append(socket->readAll());
                  socket->flush();
                  socket->waitForBytesWritten(3000);

                  socket->moveToThread(thread);
                  thread->start();
                  

                  }

                  void MainWindow::on_btn_login_clicked()
                  {
                  nickName = ui->txt_nickname->text().trimmed();

                  socket->connectToHost(ui->txt_serverIP->text(), 1234);
                  socket->waitForConnected(3000);
                  

                  }

                  void MainWindow::connected()
                  {
                  // Flip over to the chat page:
                  ui->stackedWidget->setCurrentWidget(ui->chatRoom);

                  // And send our username to the chat server.
                  socket->write(QString("\n"+nickName + " ..connected\n").toUtf8());
                  

                  --}

                  void MainWindow::on_msgArea_returnPressed()
                  {
                  if( !(ui->msgArea->text() == ""))
                  {
                  sendTheMsg();
                  }
                  }
                  @
                  this is the client code..this code I have to change and me it to communicate with the server..but I cant find why cannot send and recieve a msg

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #14

                    Did you find a solution andreyc?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #15

                      No, unfortunately nothing.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved