简体   繁体   English

Java客户端/服务器实现

[英]Java Client/Server Implementation

I have to make an university project that involves a client/server architecture. 我必须创建一个涉及客户端/服务器架构的大学项目。

There should be a server where a client can login and search or save some stuff. 应该有一个服务器,客户端可以登录并搜索或保存一些东西。

What's the best way to implement a stuff like that? 实现这样的东西的最佳方法是什么?

I think it can be done using RMI or ServerSockets or even WebServices, but what's the easiest way to implement this project? 我认为可以使用RMI或ServerSockets甚至WebServices来完成,但是实现这个项目最简单的方法是什么?

Using Web Sevrvices i think it can be troublesome the authentication/session handling, using ServerSockets i have done some tests where i pass some custom serialized objects, but It doesnt seem to me a good way to go. 使用Web Sevrvices我认为认证/会话处理很麻烦,使用ServerSockets我已经做了一些测试,我传递了一些自定义的序列化对象,但它似乎不是一个好方法。

Any help is appreciated 任何帮助表示赞赏

Since this is a project for university I will not post an solution, but give you an good direction. 由于这是一个大学项目,我不会发布解决方案,但给你一个好的方向。

The most basic Way (what may be a good thing for a university project, and for understanding th whole matter...) would be with the Server listening in his Mainthread on a ServerSocket for Requests to connect to the Server and then for every (correct) Request (you need to specify somehow what is correct in this case) starting a new Thread with a Socket connected to the Client. 最基本的方式(对于大学项目来说可能是一件好事,对于理解整个问题......)将是服务器在服务器上监听他的Mainthread上的请求连接到服务器,然后是每个(正确)请求(您需要以某种方式指定在这种情况下是正确的)启动一个连接到客户端的套接字的新线程。 This Threads should be hosted in some sort of List or whatever in the Mainthread of the Server... 这个线程应该托管在某种List或服务器的Mainthread中的任何东西......

Update: 更新:

So if this Server provides different functionalities to its clients, which are of course methods in our Server Code, you can specify the Objects which are crated when a new Client connects (I'm calling these "ClientServerConnection" from now on, and which run in its own Thread) in the Way that the Server Object is passed to it, so if one of the "ClientServerConnection"s get a Request for whatever they can call the matching method on the Server-Object and give an according response to the client... 因此,如果此服务器为其客户端提供不同的功能,这当然是我们的服务器代码中的方法,您可以指定新客户端连接时创建的对象(我从现在开始称这些为“ClientServerConnection”,哪个运行在它自己的Thread中以服务器对象传递给它的方式,所以如果其中一个“ClientServerConnection”得到一个Request,它可以调用Server-Object上的匹配方法,并给客户端一个相应的响应...

Here some pseudo-code: 这里有一些伪代码:

in Server: 在服务器中:

//request for Connection came in 
ClientServerConnection csc = new ClientServerConnection(this, "and everything you need, at least client IP for connecting the socket");
csc.run(); //running in its own thread, of cause ClientServerConnection should extend Thread
connectionList.add(csc); //a list of the connections the Server holds

in ClientServerConnection: 在ClientServerConnection中:

//A request to the use a functionality of the Server come in, in the easiest way you are sending a String, and than trying to match it here
if(recievedString=="doWhatever"){
Server server.doWhatever(); //calling the according method on the Server Object you passed by creation of the ClientServerConnection Object
//now return something to the client, according to whatever the Method did 
}else if(recievedString=="doSomethingElse"){
//same again, according to whatever the now requested method does
}else{
//the client requested something you do not provide, need some sort of handling here
}

Hope I got you right and this helps... 希望我帮到你,这有助于......

I think RMI is the easiest solution since you define all your interfaces and don't have to care about the protocol used to communicate. 我认为RMI是最简单的解决方案,因为您定义了所有接口,而不必关心用于通信的协议。

You can also use web service with SOAP which is also a RPC (remote procedure call ) interface. 您还可以将Web服务与SOAP一起使用,SOAP也是RPC(远程过程调用)接口。

But by using Socket and ServerSocket you will learn how to build a server / client software from scratch, which is very important to know (because this is the basics). 但是通过使用Socket和ServerSocket,您将学习如何从头开始构建服务器/客户端软件,这一点非常重要(因为这是基础知识)。

'Easy' is a subjective thing, depending on what you already have experience on. “简单”是一种主观的东西,取决于你已经拥有的经验。 If you have experience in Java related technologies, you could pick a tech stack like Servlets, JSP and JQuery, and use GAE to keep things simple from the 'troublesome' aspects you mentioned. 如果您有Java相关技术方面的经验,那么您可以选择像Servlets,JSP和JQuery这样的技术堆栈,并使用GAE来简化您提到的“麻烦”方面。 GAE is a platform as a service so you woudnt have to worry about those things, as google takes care of the authentication, scaling etc. You can use GAE with PHP too, if you are into that. GAE是一个平台即服务,所以你不必担心这些事情,因为谷歌负责身份验证,扩展等。如果你是这样的话,你也可以使用GAE和PHP。

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

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