简体   繁体   English

我的Java服务器和本地客户端之间的通信

[英]Communication between my Java Server and Local Client

I've been writing easy Java Server. 我一直在编写简单的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? 也许REST是个好主意? So, how can I 'teach' my server to answer on REST queries? 那么,我如何“教”我的服务器来回答REST查询?

I've got piece of code which send data to my GAE server: 我有一段代码将数据发送到我的GAE服务器:

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. 如果您尝试从服务器提供RESTFul服务,这不是一件容易的事。 What you might want to do is user something like Restlet for bootstrapping your RESTFul server and client. 您可能想要做的是使用像Restlet这样的东西来引导您的RESTFul服务器和客户端。

For more information refer to http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet/318-restlet.html 有关更多信息,请参阅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是一种在客户端和服务器之间进行通信的简单方法。 REST basically says, use HTTP the way it was meant to be used, even when communicating between computer programs. REST基本上说,即使在计算机程序之间进行通信时,也要按照它的使用方式使用HTTP。

Read up on HTTP in case you do not have enough knowledge. 如果您没有足够的知识,请阅读HTTP。 Here is one good document: http://www.jmarshall.com/easy/http/ 这是一份很好的文件: 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. 一旦了解了如何在客户端和服务器上发送和接收HTTP消息,就可以开发RESTful服务器API了。

What you need to know about REST is that it is mostly a way of thinking when you design your API. 您需要了解的REST是,在设计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). 确保充分利用HTTP并以任何格式发送/接收数据(通常是JSON,XML或UrlEncoded键/值对)。

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的同时学习Restlet或其他一些大型图书馆好多了。 REST and HTTP are both easy stuff - once you get down to the "it's just some text going back and fourth". REST和HTTP都是简单的东西 - 一旦你开始“它只是一些文本返回和第四”。 When you understand these things fully, then you could look at some frameworks. 当你完全理解这些东西时,你可以看一些框架。

Here is some information about REST: http://rest.elkstein.org/ 以下是有关REST的一些信息: http//rest.elkstein.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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