Results 1 to 10 of 10

Thread: Communication between java and c++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Communication between java and c++

    Hey everyone

    Me and a fellow student are writing a program which has to communicate with each other. His program is written in normal java and mine is written in C++ with Qt. I tried the Fortuneserver example to which he connected. We where able to connect but he didn't recieve any text. His out put was -1.

    Could someone point me in the direction i have to look to make our programs work.

    We are both working on windows (I run xp, he runs Vista) and i use Qt 4.3

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between java and c++

    The problem must be in the way he implemented the client. The fortune server works just fine with the client written with Qt...
    http://java.sun.com/docs/books/tutor...ets/index.html

    Could we see the code for the Java client?

    Regards

  3. #3
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between java and c++

    I will post it here tommorow because he isn't available right now

  4. #4
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between java and c++

    Went wrong here.
    Last edited by eekhoorn12; 24th June 2007 at 21:03.

  5. #5
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between java and c++

    Here is the java code for catching te message:

    Qt Code:
    1. try
    2. {
    3. socket = new Socket("145.92.14.112", 2911);
    4. System.out.println("Socket Created");
    5. out = new PrintWriter(socket.getOutputStream(), true);
    6. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    7. }
    8.  
    9. catch (UnknownHostException e)
    10. {
    11. System.out.println("Unknown host....");
    12. System.exit(1);
    13. }
    14.  
    15. catch (IOException e)
    16. {
    17. System.out.println("No I/O");
    18. System.exit(1);
    19. }
    20.  
    21. while(wii == 1)
    22. {
    23. try
    24. {
    25. if(in.readLine() != null)
    26. {
    27. //line = in.readLine();
    28. System.out.println("Ontvangen: " + in.readLine());
    29. }
    30. }
    31.  
    32. catch(IOException e)
    33. {
    34. System.out.println("Read failed");
    35. System.exit(-1);
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by eekhoorn12; 24th June 2007 at 21:23.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between java and c++

    I don't believe your client implementation is OK.
    Read this first: http://java.sun.com/docs/books/tutor...entServer.html

    EDIT: oh, now I see your code
    Is the port OK? Otherwise it seems OK.
    Have you debugged it? Does it throw any exceptions?

    Regards

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between java and c++

    Does the client computer have a firewall?
    If it has, try turning it off while you test.

    Is the client started with administrator privileges? Maybe this helps a bit.

    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Communication between java and c++

    Don't you think you should wait until something connects and writes something to the socket? Does the Socket constructor wait until a connection is established and does it wait until something actually transmits data over the socket? In other words - are Socket() and getOutputStream() blocking?

  9. #9
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between java and c++

    We have editted de code a bit. We know have communication but the message he gets isn't exactly what i sent.

    He gets a line but between each letter there is some null code.

    The server code is the following:
    Qt Code:
    1. /****************************************************************************
    2. **
    3. ** Copyright (C) 2004-2007 Trolltech ASA. All rights reserved.
    4. **
    5. ** This file is part of the example classes of the Qt Toolkit.
    6. **
    7. ** This file may be used under the terms of the GNU General Public
    8. ** License version 2.0 as published by the Free Software Foundation
    9. ** and appearing in the file LICENSE.GPL included in the packaging of
    10. ** this file. Please review the following information to ensure GNU
    11. ** General Public Licensing requirements will be met:
    12. ** http://www.trolltech.com/products/qt/opensource.html
    13. **
    14. ** If you are unsure which license is appropriate for your use, please
    15. ** review the following information:
    16. ** http://www.trolltech.com/products/qt/licensing.html or contact the
    17. ** sales department at sales@trolltech.com.
    18. **
    19. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    20. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    21. **
    22. ****************************************************************************/
    23.  
    24. #include <QtGui>
    25. #include <QtNetwork>
    26.  
    27. #include <stdlib.h>
    28.  
    29. #include "server.h"
    30.  
    31. Server::Server(QWidget *parent)
    32. : QDialog(parent)
    33. {
    34. statusLabel = new QLabel;
    35. quitButton = new QPushButton(tr("Quit"));
    36. quitButton->setAutoDefault(false);
    37.  
    38. tcpServer = new QTcpServer(this);
    39. tcpServer->listen(QHostAddress::Any,2911);
    40. if (!tcpServer->isListening()) {
    41. QMessageBox::critical(this, tr("Fortune Server"),
    42. tr("Unable to start the server: %1.")
    43. .arg(tcpServer->errorString()));
    44. close();
    45. return;
    46. }
    47.  
    48. statusLabel->setText(tr("The server is running on port %1.\n"
    49. "Run the Fortune Client example now.")
    50. .arg(tcpServer->serverPort()));
    51.  
    52. fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
    53. << tr("You've got to think about tomorrow.")
    54. << tr("You will be surprised by a loud noise.")
    55. << tr("You will feel hungry again in another hour.")
    56. << tr("You might have mail.")
    57. << tr("You cannot kill time without injuring eternity.")
    58. << tr("Computers are not intelligent. They only think they are.");
    59.  
    60. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    61. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
    62.  
    63. QHBoxLayout *buttonLayout = new QHBoxLayout;
    64. buttonLayout->addStretch(1);
    65. buttonLayout->addWidget(quitButton);
    66. buttonLayout->addStretch(1);
    67.  
    68. QVBoxLayout *mainLayout = new QVBoxLayout;
    69. mainLayout->addWidget(statusLabel);
    70. mainLayout->addLayout(buttonLayout);
    71. setLayout(mainLayout);
    72.  
    73. setWindowTitle(tr("Fortune Server"));
    74. }
    75.  
    76. void Server::sendFortune()
    77. {
    78. QByteArray block;
    79. statusLabel->setText("Bla");
    80. QDataStream out(&block, QIODevice::WriteOnly);
    81. //out.setVersion(QDataStream::Qt_4_0);
    82. //out << (quint16)0;
    83. out << fortunes.at(qrand() % fortunes.size());
    84. //out.device()->seek(0);
    85. //out << (quint16)(block.size() - sizeof(quint16));
    86.  
    87. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    88. connect(clientConnection, SIGNAL(disconnected()),
    89. clientConnection, SLOT(deleteLater()));
    90.  
    91. clientConnection->write(block);
    92. clientConnection->disconnectFromHost();
    93. }
    To copy to clipboard, switch view to plain text mode 

    and the java code is the following:
    Qt Code:
    1. try
    2. {
    3. while ((fromServer = in.readLine()) != null)
    4. {
    5. System.out.println("Server: " + fromServer);
    6. if (fromServer.equals("Bye."))
    7. break;
    8.  
    9. fromUser = stdIn.readLine();
    10.  
    11. if (fromUser != null)
    12. {
    13. System.out.println("Client: " + fromUser);
    14. out.println(fromUser);
    15. }
    16. }
    17. }
    18.  
    19. catch (IOException e)
    20. {
    21. System.err.println("Couldn't get I/O for the connection to: taranis.");
    22. System.exit(1);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by eekhoorn12; 25th June 2007 at 11:40.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Communication between java and c++

    You are experiencing differences in data encoding. Make sure you write and read data that is encoded the same way. Especially if you use QDataStream.

Similar Threads

  1. Qt <-> Java - Best Practices?
    By mentat in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2006, 02:32
  2. UICTable listview subitems with java
    By cqtkitty in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 08:59
  3. Embedding Java in Qt
    By learnerplts in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 15:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.