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
QtWS25 Last Chance

A qt client connect with a java server

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 4.8k Views
  • 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
    #2

    Since you have not provided the initialization part for server nor for client I assume that you checked all possible errors during initialization.

    Have you tried to send anything to the Java server from another application, "netcat":http://netcat.sourceforge.net/ or "ncat":http://nmap.org/ncat/ for example?

    Have you tried to use the same tools to receive anything from Qt client?

    Try to run "wireshark":http://www.wireshark.org/ to see what happens between client and server.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      parkir
      wrote on last edited by
      #3

      thanks for the reply, I 'll try it I will tell you.

      But when I use the Qt client with a server written in Qt, everything is ok and respectively for the java client and server.
      I don't know what more changes to make to have a communication between them!

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

        Another option if you have source code of server.handle(), is to check where is throws an IOException and why its message is null.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          parkir
          wrote on last edited by
          #5

          my server is the following
          chatServer.java
          @import java.net.;
          import java.io.
          ;

          public class ChatServer implements Runnable {
          private ChatServerThread clients[] = new ChatServerThread[50];
          private ServerSocket server = null;
          private Thread thread = null;
          private int clientCount = 0;

          public ChatServer(int port) {
          try {
          System.out.println("Binding to port " + port + ", please wait ...");
          server = new ServerSocket(port);
          System.out.println("Server started: " + server);
          start();
          } catch (IOException ioe) {
          System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
          }
          }

          public void run() {
          while (thread != null) {
          try {
          System.out.println("Waiting for a client ...");
          addThread(server.accept());
          } catch (IOException ioe) {
          System.out.println("Server accept error: " + ioe);
          stop();
          }
          }
          }

          public void start() {
          if (thread == null) {
          thread = new Thread(this);
          thread.start();
          }
          }

          public void stop() {
          if (thread != null) {
          thread.stop();
          thread = null;
          }
          }

          private int findClient(int ID) {
          for (int i = 0; i < clientCount; i++)
          if (clients[i].getID() == ID)
          return i;
          return -1;
          }

          public synchronized void handle(int ID, String input) {
          if (input.equals(".bye")) {
          clients[findClient(ID)].send(".bye");
          remove(ID);
          } else
          for (int i = 0; i < clientCount; i++)
          clients[i].send(ID + ": " + input);
          }

          public synchronized void remove(int ID) {
          int pos = findClient(ID);
          if (pos >= 0) {
          ChatServerThread toTerminate = clients[pos];
          System.out.println("Removing client thread " + ID + " at " + pos);
          if (pos < clientCount - 1)
          for (int i = pos + 1; i < clientCount; i++)
          clients[i - 1] = clients[i];
          clientCount--;
          try {
          toTerminate.close();
          } catch (IOException ioe) {
          System.out.println("Error closing thread: " + ioe);
          }
          toTerminate.stop();
          }
          }

          private void addThread(Socket socket) {
          if (clientCount < clients.length) {
          System.out.println("Client accepted: " + socket);
          clients[clientCount] = new ChatServerThread(this, socket);
          try {
          clients[clientCount].open();
          clients[clientCount].start();
          clientCount++;
          } catch (IOException ioe) {
          System.out.println("Error opening thread: " + ioe);
          }
          } else
          System.out.println("Client refused: maximum " + clients.length + " reached.");
          }

          public static void main(String args[]) {
          ChatServer server = null;
          if (args.length != 1)
          System.out.println("Usage: java ChatServer port");
          else
          server = new ChatServer(Integer.parseInt(args[0]));
          }

          }
          @

          and 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 DataInputStream streamIn = null;
          private DataOutputStream streamOut = null;

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

          public void send(String msg) {
          try {
          streamOut.writeUTF(msg);
          streamOut.flush();
          streamOut.writeUTF("writeUTF");
          streamOut.writeInt(1234);

          } 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 {
          server.handle(ID, streamIn.readUTF());
          } catch (IOException ioe) {
          System.out.println(ID + " ERROR reading: " + ioe.getMessage());
          server.remove(ID);
          stop();
          }
          }
          }

          public void open() throws IOException {
          streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
          streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
          }

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

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

          if (streamOut != null)
          streamOut.close();
          }
          }
          @

          but I cant change the code of server..only the client side which is written in Qt.

          1 Reply Last reply
          0
          • 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
            • P Offline
              P Offline
              parkir
              wrote on last edited by
              #7

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                parkir
                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
                  • P Offline
                    P Offline
                    parkir
                    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
                        • P Offline
                          P Offline
                          parkir
                          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
                          • P Offline
                            P Offline
                            parkir
                            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