PDA

View Full Version : How do connect to the QT on Server and Android Client Program?



dooil8817
28th July 2011, 08:15
I tried to connect QT Server with Android client.
Sending data from Android to QT was good,
but Android program can't receive data from QT on server.

================================================== ====================
QT code on server
================================================== ====================

void TcpServer::readyRead()
{
QTcpSocket *client = (QTcpSocket*)sender();
while(client->canReadLine())
{
QString line = QString::fromUtf8(client->readLine()).trimmed();
qDebug() << "Read line:" << line;

client->write("Here from server-Write ");

//
//QTextStream out(client);
//out << "Here from server-TextStream ";
//out.flush();
}
}

================================================== ====================
Android client program code
================================================== ====================
public class TCPClient implements Runnable {
public static final String SERVERIP = "xx.xx..";
public static final int SERVERPORT = 8080;
private BufferedReader networkReader;

public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.d("TCP", "C: Connecting...");
Socket socket = new Socket(serverAddr, SERVERPORT);
String message = "Hello... from Client";
String line;

try {
Log.d("TCP", "C: Sending: '" + message + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true );
out.println(message);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");

networkReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Thread.sleep(1000);
line = networkReader.readLine(); <== ( on debugging, line is on Holding or Null )
Log.d("TCP", "C: Receieved..." + line);

} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
}

I don't know hardly why android program can't receive a stream data from QT on server..

mvuori
28th July 2011, 14:56
Just one idea: Your server socket is a local variable. It is supposed to buffer any writes, but perhaps it drops that when it it gets out of scope -- and the client doesn't get a chance to read anything. I'd make it a static variable or make a test into the loop that the write buffer is empty before exiting the function.

dooil8817
29th July 2011, 06:03
Thanks to your reply.

the client program receives data stream from Qt Server..after i killed the Qt Process on Server.
the Lock on client is unfasten when the Qt server process is disconnected from client

now.. it works normally ( some code on Sever was added )


void TcpServer::readyRead()
{

QString line;

//QTcpSocket *client = (QTcpSocket*)sender(); <== this was moved to a member Variable
while(true)
{

if (client->canReadLine()) {
line = QString::fromUtf8(client->readLine()).trimmed();
qDebug() << "Read line:" << line;
break;
}
}
qDebug() << "Send line:" << line << " from Server";

QTextStream out(client);
out << "Here from server-TextStream ";
client->flush();

//client->disconnectFromHost();
client->setSocketDescriptor(m_iSocketDesc); <= the client program works normally after this line was added.. i don't know why it works

qDebug() << "server-TextStream";
}

iitmrp
29th January 2014, 13:03
Hello,

I am trying to perform the same functionality.
However, I am not able to connect to the server due to some bug in my code.
Could you please share your QT and Android codes just to get me started?

Thanks in advance..