A qt client connect with a java server



  • I have a problem when I am trying to send a msg from my qt client to java server.

    this is the java code, the error that i take is " ID ERROR reading: null "
    @ public void run() {
    System.out.println("Server Thread " + ID + " running.");
    while (true) {
    try {
    server.handle(ID, DataInputStream.readUTF());
    }
    catch (IOException ioe) {
    System.out.println(ID + " ERROR reading: " + ioe.getMessage());
    server.remove(ID);
    stop();
    }
    }
    }
    @

    my void in Qt client for send a msg is
    @void MainWindow::sendTheMsg()
    {
    QByteArray buffer;
    buffer.append(nickName);
    buffer.append(": ");
    buffer.append(ui->lineEdit->text());
    socket->write("<i>"+buffer+"</i>");
    qDebug() << buffer;

    }
    @

    also I have tried to change the encode to UTF8 with this code
    @ QString qtext = "Hello";
    socket->write(qtext.toUtf8());
    @

    but nothing!

    any idea plz!



  • 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.



  • 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!



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



  • 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.



  • 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?



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



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

    The error display when I close the client!



  • 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.



  • 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..



  • 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.



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

    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();
    

    }
    }
    @



  • 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



  • Did you find a solution andreyc?



  • No, unfortunately nothing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.