Im trying to transfer a simple message from my raspberry pi which is the client to my computer which is the server. Im using cpp for tcpClient and java for tcpServer. This is the my TCPServer.java:
import java.net.*;
import java.io.*;
class TCPServer extends Thread {
TCPClass() {
}
public void connect(){
try {
ServerSocket welcomeSocket = new ServerSocket();
welcomeSocket.setReuseAddress(true);
welcomeSocket.bind(new InetSocketAddress(8080));
System.out.println("server start listening... ... ...");
Socket connectionSocket = welcomeSocket.accept();
System.out.print("Server has connected!\n");
Connection c = new Connection(connectionSocket);
c.start();
connectionSocket.close();
welcomeSocket.close();
System.out.println("connection terminated");
}
catch(IOException e) {
System.out.println("Listen :"+e.getMessage());
}
}
class Connection extends Thread{
Socket connectionSocket;
Connection(Socket _connectionSocket){
connectionSocket = _connectionSocket;
}
public void run(){
try{
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
String clientSentence = inFromClient.readLine();
System.out.println("Client sent: "+clientSentence);
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
System.out.println("Client sent: "+capitalizedSentence);
outToClient.writeBytes(capitalizedSentence);
outToClient.flush();
inFromClient.close();
}catch(Exception e){}
}
}
This is my TCPClient.cpp:
#include "Client.h"
#include <QHostAddress>
Client::Client(QObject* parent): QObject(parent)
{
connect(&client, SIGNAL(connected()),
this, SLOT(startTransfer()));
}
Client::~Client()
{
client.close();
}
void Client::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void Client::startTransfer()
{
client.write("Hello, world", 13);
}
and finally, this is the program which executes my TCPClient.cpp:
#include "Client.h"
#include <QApplication>
#include <iostream>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Client client;
std::cout << argv[1] << " " << argv[2] << std::endl;
client.start( argv[1], atoi( argv[2] ) );
// client.start("127.0.0.1", 8888);
return app.exec();
}
so I execute my TCPServer.java, I have a message to prove that the server has connected, so I execute my TCPCLient.cpp , and there is a problem. My server doesn't show any message sent by the client, but when I end the execution of my TCPClient.cpp , the message is shown on my screen which means the server has received the message. Weird no!! Thank you for the help.
EDIT: This is my Client.h, sorry for the delay
// client.h
#include <QtNetwork>
#include <QObject>
#include <QString>
#include <QTcpSocket>
class Client: public QObject
{
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
public slots:
void startTransfer();
private:
QTcpSocket client;
};