Android : Android client does not update the data when connect with linux socket

on Saturday, March 28, 2015


I have my android app connect to a linux HAC server (or HPC) implemented on C++ using sockets , but the problem that the android does not update the received message from the server! I have closed the process on the HAC server and every time I still receive the same text from the server! I don't even know how the application still fetching the data even if the process that runs the server is closed! Small scenario to what happens with me: I run the server I fetch any string from the server I close the server process (ctrl+c) I change the server to send the time instead of that old string I recompile it and run the server and still getting the old string instead of the time, even if there server is closed or open!


This is the android client :



withdraw.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
MyClientTask myClientTask = new MyClientTask(
"1.1.1.1.", // removed for security reasons!
0000
);
myClientTask.execute();

} // end of OnClick
}); // end of setOnClickListener
}



public class MyClientTask extends AsyncTask<Void, Void, Void> {

String dstAddress;
int dstPort;
String response = "";

MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}

@Override
protected Void doInBackground(Void... arg0) {

Socket socket = null;

try {
socket = new Socket(dstAddress, dstPort);

ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];

int bytesRead;
inputStream = socket.getInputStream();

/*
* notice:
* inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}

@Override
protected void onPostExecute(Void result) {
tv.setText(response);
super.onPostExecute(result);
}

}


and this is the server



#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;

char sendBuff[1025];
time_t ticks;

listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);

bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

listen(listenfd, 10);

while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));

close(connfd);
sleep(1);
}
}

0 comments:

Post a Comment