PDA

View Full Version : Communication between java and c++



eekhoorn12
22nd June 2007, 21:48
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

marcel
22nd June 2007, 21:53
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/tutorial/networking/sockets/index.html

Could we see the code for the Java client?

Regards

eekhoorn12
22nd June 2007, 22:15
I will post it here tommorow because he isn't available right now

eekhoorn12
24th June 2007, 20:58
Went wrong here.

eekhoorn12
24th June 2007, 21:01
Here is the java code for catching te message:


try
{
socket = new Socket("145.92.14.112", 2911);
System.out.println("Socket Created");
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}

catch (UnknownHostException e)
{
System.out.println("Unknown host....");
System.exit(1);
}

catch (IOException e)
{
System.out.println("No I/O");
System.exit(1);
}

while(wii == 1)
{
try
{
if(in.readLine() != null)
{
//line = in.readLine();
System.out.println("Ontvangen: " + in.readLine());
}
}

catch(IOException e)
{
System.out.println("Read failed");
System.exit(-1);
}
}

marcel
24th June 2007, 21:41
I don't believe your client implementation is OK.
Read this first: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.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

marcel
24th June 2007, 21:54
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

wysota
24th June 2007, 22:39
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?

eekhoorn12
25th June 2007, 11:34
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:

/************************************************** **************************
**
** Copyright (C) 2004-2007 Trolltech ASA. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
************************************************** **************************/

#include <QtGui>
#include <QtNetwork>

#include <stdlib.h>

#include "server.h"

Server::Server(QWidget *parent)
: QDialog(parent)
{
statusLabel = new QLabel;
quitButton = new QPushButton(tr("Quit"));
quitButton->setAutoDefault(false);

tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,2911);
if (!tcpServer->isListening()) {
QMessageBox::critical(this, tr("Fortune Server"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}

statusLabel->setText(tr("The server is running on port %1.\n"
"Run the Fortune Client example now.")
.arg(tcpServer->serverPort()));

fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
<< tr("You've got to think about tomorrow.")
<< tr("You will be surprised by a loud noise.")
<< tr("You will feel hungry again in another hour.")
<< tr("You might have mail.")
<< tr("You cannot kill time without injuring eternity.")
<< tr("Computers are not intelligent. They only think they are.");

connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));

QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);

setWindowTitle(tr("Fortune Server"));
}

void Server::sendFortune()
{
QByteArray block;
statusLabel->setText("Bla");
QDataStream out(&block, QIODevice::WriteOnly);
//out.setVersion(QDataStream::Qt_4_0);
//out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
//out.device()->seek(0);
//out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));

clientConnection->write(block);
clientConnection->disconnectFromHost();
}



and the java code is the following:

try
{
while ((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;

fromUser = stdIn.readLine();

if (fromUser != null)
{
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
}

catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}

wysota
25th June 2007, 14:26
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.