2

I have an application in android that communicate to a server which is python throw socket connection. This is my doInBackground method in android:

protected String doInBackground(String... arg0) {
    String username = arg0[0];
    String result;

    try {
        ///Socket Connection
        InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
        socket = new Socket(serverAddr, SERVERPORT);
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF(username);
        out.flush();
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line=br.readLine()) != null){
            sb.append(line);
        }
        result = sb.toString();
        socketResult = result;
        isr.close();
        br.close();
        //socket.close();

        return result;
    } catch (Exception e) {
        return new String("Exception: " + e.getMessage());
    }
}

and this is a slice of my server side code which i send & recieve message and its Python:

    def run(self):
    i = True
    while i:
        strRecieved = self.sock.recv(self.recieveBuffer).decode('utf-8')
        if strRecieved != "":
            print('Client sent: ' + strRecieved)
            msg = "yes"
            self.sock.send(msg.encode('utf-8'))
            print('Message sended to client: ' + msg)
            strRecieved = ""

    self.sock.close()

I defined a button and when i click on that button my AsyncTask class will execute. My application work fine if i just send one message to server and recieve one message from server but when i click on that button for second time, nothing will happen either on server and client. I don't know what should i do to send & recieve multiple messages with one socket connection. I will appriciate if you help me. Thanks.

Mostafa Fahimi
  • 510
  • 1
  • 8
  • 23
  • What the hell? Just read my question. Is this similar to simple tcp client?!!!!! how? – Mostafa Fahimi Jan 13 '17 at 00:00
  • I did not read the other question, but with over a decade of network programming experience, you do have a simple TCP client (and server). You'll need to debug both of your programs at the same time. Wireshark is a useful tool to see what packets are actually sent and received between them. One common problem people have is not setting the TCP_NODELAY flag on the socket, and not flushing the buffers when sending. This leaves their data in a buffer and is not actually sent. – dsh Jan 13 '17 at 14:29
  • 1
    Just looking at the code you posted, your client opens a new socket connection each time it wants to do something. Your server program looks like it may not be ready to accept a new connection. Until the socket's recv returns no data, your server program will remain in the loop handling the one socket. You haven't shown if your server is multi-threaded (which is very complex), or multi-process or can otherwise handle multiple connections at the same time. – dsh Jan 13 '17 at 14:34
  • I suggest developing one side at a time and use an existing and known-to-work program for the other. For example, unix systems have a built-in "echo" server you could use for the server in order to develop your Android client and debug it. You can use the 'telnet' program on unix or putty on windows to connect to your server program to test and debug its operation. That way you won't need to learn both sides at the same time and be dealing with and fighting bugs in both programs at the same time. – dsh Jan 13 '17 at 14:36

0 Answers0