Programación de Android Socket, comunicación continua servidor-cliente (al pulsar un botón)

So, i have a Android-App(Client) and a Java-program(Server), with a One-time socket communication, whenever the android app connects to my server in a special activity (working fine).

Because my server is embedded in a bigger program (with Swing-components, where the server takes its informations from), i have this (reduced) code here:

//somewhere in my Swing-Application
Server myServer = new Server();
myServer.start();
//...

public class Server extends Thread {
@Override
public void run() {
    try {
        ServerSocket serverSocket = new ServerSocket(8090);
        try {
            while (true) {
                System.out.println("Server is waiting for connections...");
                socket = serverSocket.accept();
                startHandler(socket);
            }
        } finally {
            serverSocket.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void startHandler(final Socket socket) throws IOException {
    System.out.println("Client connected to Server");
    Thread thread = new Thread() {
           @Override
        public void run() {
            try {                       
                writer = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");

    //doing something usefull, i am sending a JSON-String, which i´ll parse in my app.

                writer.write(someStringContainingJSONString);
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                closeSocket();
            }
        }
        private void closeSocket() {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    };
    thread.start();
}

In my Android-App i have:

public void onCreate(Bundle savedInstanceState) {
    viewJSON = new Runnable() {
        @Override
        public void run() {
            getJSON();
        }
    };
    Thread thread = new Thread(null, viewJSON, "MagentoBackground");
    thread.start();
    myProgressDialog = ProgressDialog.show(myActivity.this, "Please wait...", "Retrieving data ...", true);
}

private void getJSON() {
    try {
            socket = new Socket(serverIPAddress, SERVER_PORT);
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
            String help = reader.readLine();
            // parse this String according to JSON, is working fine!
            }
// catch and so on...

Now, i want the app, to recieve data, whenever i hit a button "send data" from my Swing-Application, to have the newest data available.

On the other hand, i want the server to recieve data (also a JSON-String) when i make changes in my Android app. The String should also be send when i hit a specific button.

How can i do that? The problem is the threading issue(otherwise my swing application wouldn´t work) combined with networking. If i don´t close the socket, i cannot continue with my program properly (or at least, it seems so with my code right now) Can you help me out here? Thank you very much in advance for your help and thoughts. Best, Andrea

preguntado el 23 de diciembre de 12 a las 15:12

as far as I understand, u want a messenger(or sms etc.) like system with one on computer and one on the phone. and these pairs will communicate with each other as soon as a some event occurs(like pushing the send button on msn ), right? -

use 2 different connection then, one of them is discontinuous momentarily post like request that android sends immediately when an event occurs. the other one, again the android sends a request and waits until response comes or until a duration. if some event occurs in swing side, immediately send response which is already waiting for. so u can communicate instantly. on the other hand, if a timeout occurs(make this smaller than internet timeout) then send response but it shall say 'there is no event' etc. -

Yeah right. or at least the communication base is just a simple String, representing a JSON-Object. But it´s the exchange behind the threads, making me troubles, everything else i´ve solved or will solve so far :) Because, i have to work with this messages in Swing and Android App -

So you mean, i shall implement an Server (just like i allready did, just the other way round) on my Android-App on another port, and let the swing application connect to it? -

your swing application is already a part of server so you dont need another connection between swing component and server. ı only need two connection between android and server(1 continues and the other is not). u might also want to use an observer(and Observable or look at StringProperty which has StringPropertyChangeListener) on your swing application. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.