简体   繁体   中英

Communication between my Java Server and Local Client

I've been writing easy Java Server. I'm gonna deploy this code to my student's server and run it there.

public class Demo {

    public static void main(String[] args) {

        String port = "50000";

        ServerAttributes attr = new ServerAttributes();
        attr.setPort(Integer.parseInt(port));

        Socket socket = null;
        ServerSocket serverSocket= null;

        try {
            serverSocket = new ServerSocket(attr.getPort());
            System.out.println("Waiting for accept...");

            while(true) {
                socket = serverSocket.accept();
                // TODO

                socket.close();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

I wanna create easy Client code which will be 'talking' with my server. Communication Client->Server is easy. My server is visible for client. But what should I do to provide communication in another way?

Maybe REST is good idea? So, how can I 'teach' my server to answer on REST queries?

I've got piece of code which send data to my GAE server:

package enceladus.server.trash.rest;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class RESTGAEServer {
    static String httpAddress = "http://*********.appspot.com/sign";

    public static void main(String[] args) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(httpAddress);

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


            nameValuePairs.add(new BasicNameValuePair("guestbookName", "default"));                     
            nameValuePairs.add(new BasicNameValuePair("content", "TEST"));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            @SuppressWarnings("unused")
            HttpResponse response = client.execute(post);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Thanks in advance

If you are trying to provide RESTFul service from the server, its not an easy task. What you might want to do is user something like Restlet for bootstrapping your RESTFul server and client.

For more information refer to http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet/318-restlet.html

REST is a very simple an easy way of communicating between a client and a server. REST basically says, use HTTP the way it was meant to be used, even when communicating between computer programs.

Read up on HTTP in case you do not have enough knowledge. Here is one good document: http://www.jmarshall.com/easy/http/

Once you understand how to send and receive HTTP messages on the client and on the server, you are ready to develop RESTful server API:s.

What you need to know about REST is that it is mostly a way of thinking when you design your API. Make sure to utilize HTTP to its full extent and send/receive data in whatever format (usually JSON, XML or UrlEncoded key/value pairs).

I would say you are MUCH better off doing this yourself than to try to learn Restlet or some other huge library at the same time you learn REST. REST and HTTP are both easy stuff - once you get down to the "it's just some text going back and fourth". When you understand these things fully, then you could look at some frameworks.

Here is some information about REST: http://rest.elkstein.org/

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