简体   繁体   中英

Java server that waits for message from client

I am trying to send a string from an Android app (client) to a Java server which waits for such messages from many android clients and stores them in a file. The client side coding is good, I wrote with help of tutorials and blogs and here it is:

HttpURLConnection conn = null;
    try {
        Log.e("URL", "> " + url);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
          throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }

What I want is a server in Java which can handle these messages from different android apps and store the message strings into a text file. I just want to know how to handle those incoming messages and get them into an empty string.

So far I have done some small trials with the following server side coding:

int port = 8080;
    ServerSocket ss = new ServerSocket(port);
    Socket s = ss.accept();
    while(true) {
       if (s == null) {
            /* nothing to do */
            try {
                wait();
            } catch (InterruptedException e) {
                continue;
            }
        }
        try {
            InputStream is = new BufferedInputStream(s.getInputStream());
            PrintStream ps = new PrintStream(s.getOutputStream());
            ANDROID_DEVICE = ps.flush();       
        }catch(IOException e){}
    }

There are many ways to approach this problem. Following are a few. 1) Use an application Server and install a webservice which accepts requests from android clients. 2) Create your own server. Register a remote object which would accept requests from android clients. By using RMI (Remote method invocation), you can achieve a client server architecture. 3) Use JMS , if it is supported on Android.

I am not sure if you have done some web development before. It will make more sense to use a web server since your client code is using HTTP protocol. In fact, it is much easier to use a web container such as Tomcat than writing your own server. You then will have to write a Servlet to handle the HTTP request.

Please note that you are not limited to java when it comes to web development. You can have the server side written in php, .net,...etc.

However, if you are only looking for a simple client-server (one client only). Then you can refer to this zerioh.tripod.com/ressources/sockets.html but this means you have to use the new client code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM