设计模式:客户端-服务器
我将用java实现一个客户端-服务器应用程序。客户端和服务器通过 TCP 相互通信。只有一台中央服务器,负责处理所有客户端消息。
我不使用 RMI 进行通信,它们与 tcp 套接字进行通信,并且客户端可以通过服务器与另一个客户端进行通信。这意味着服务器将消息从客户端 A 转发到客户端 B。
现在我坐在这里尝试弄清楚如何设计服务器应用程序。
所以服务器一直在监听传入的连接。没关系。对于连接到服务器(拨号)的每个新客户端,服务器都会创建一个新的套接字,我将使用线程让它们同时工作。
每个用户都使用一个客户端,并且每个客户端都必须使用用户名和密码进行身份验证。
我正在寻找一种在服务器上实现的设计模式,该模式存储/处理与客户端的套接字连接。
我的第一个想法是创建一个名为 ClientManager 的大单例对象,其中包含一个 Map。因此,服务器可以调用类似 ClientManger.getSocketBy(username) 的方法来获取所需用户(客户端)的 Socket。
这样就可以解决客户端A到客户端B的消息“转发”问题
。例如: 用户A(客户端A)想要向用户B(客户端B)发送消息。这样服务器端接收到用户A(客户端A)发来的消息,调用ClientManager.getSocketBy(User B)获取到用户B的通信socket,然后通过这个Socket发送消息。
但是服务器端的这种“设计”在我看来很简单,因为它可能是这样的事情的一个很好的设计,并且我缺少诸如事件,观察者等之类的东西。
另一种可能性可能是将事件抛出到处理该事件的线程用户B的socket连接,而不是直接调用Socket.send()方法。
但一定有更好的方法......
是否有一种通用的方法来实现这样的设计模式,您可以推荐?我想知道即时通讯服务器如何实现这一点?
顺便说一句:我没有实现即时通讯程序,可扩展性并不是最重要的,因为没有 1000 个客户端同时与服务器通信。干净的软件设计对我来说更重要。
我正在实现一个自己的通信协议(基于 xml)。有没有一种方法/设计模式如何将协议包装在对象中,或者除了字符串/xml解析之外我无能为力(从软件架构师的角度来看)?
i am going to implement a client - server application in java. Client and server communicate with each other via TCP. There is only one central server, who handles all Client messages.
I don't use RMI for the communication, they communicate with tcp-sockets and a client can communicate with another client through the server. That means that the server forwards messages from client A to client B.
Now im sitting here and try to figure out how to design the server application.
So the server is listen for incomming connections all the time. Thats fine. For every new client, that get connected to the server (dial-up), the server create a new Socket and i would use Threads to let them work concurrently.
Every User uses a client, and every client must be authenticated with a username and password.
Im searching for a design pattern to implement on the Server, that stores/handles the Socket connection to a client.
My first idea was to create a big Singleton object called ClientManager that contains a Map. So the Server could call something like ClientManger.getSocketBy(username) to get the Socket of the desired User (Client).
This could solve the "forwarding" of messages from Client A to Client B.
For expample:
User A (Client A) would like to send a message to User B (Client B). So the server receives the message from User A (Client A) and call ClientManager.getSocketBy(User B) to get the communication socket to the User B, and then send the message through this Socket.
But this "design" on the server side seems to me to simple as it could be a good design for such a thing, and im missing something like Events, Observer etc.
Another possibility could be to throw an Event to the Thread that handles the User B socket connection instad of calling the Socket.send() method directly.
But there must be a better way ...
Is there a common way to implement something like this a design pattern that you can recommend? Im wondering how a instant messanger server would implement this?
By the way: im not implementing a instant messanger and the scalability is not the most important thing, since there are not 1000 of clients at the same time communicating with the server. A clean software design is more important for me.
Im implementing a own Protocol (xml based) for the communication. Is there a way/design pattern how to wrap a protocol in a object or is there nothing i could do (from the sofware architects view) excepted string / xml parsing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你有两个问题。
第一个是关于路由使用什么“设计模式”。您指出了您正在做什么,但没有指出您正在遇到(或预计遇到)的问题,只是说它太简单了。模式是解决常见问题的常见方法,但是太简单的程序不是我见过的模式:)
第二个问题是将协议的实现与使用它的客户端分开。现在这是一个很好的问题,也是你绝对应该做的事情。您将需要创建一个公开字段(如“收件人”、“发件人”、“内容”等)的 Message 对象。您的服务器需要将原始 xml 数据传递到某种类型的解析器中,该解析器充当消息对象的工厂。
您可以通过为每个 TCP/IP 端口分配不同的协议或从默认协议开始然后让客户端和服务器协商不同的协议来支持其他协议。
EDIT1:
说实话,我不会将套接字映射到用户。我会将您与用户通信的方式抽象为一个对象,可能是连接或通道。然后,您可以使用 TcpConnection 或 CorbaConnection 等对 Connection 进行子类化。理想情况下,您会执行以下操作:
请注意,没有提及连接或协议实现细节。
You have two questions I think.
The first is in regard to what "design pattern" to use for your routing. You indicated what you were doing, but you didn't indicate what problem you are experiencing (or anticipate experiencing) other than to say it was too simple. Patterns are common ways to solve common problems, but a program being too simple is not one I've seen a pattern for :)
The second question is about separating the implementation of your protocol from the clients that use it. Now that is a good question and something you should definitely do. You are going to want to create a Message object that exposes the fields (like To, From, Content, etc). Your server needs to pass the raw xml data into some type of parser that acts as a Factory for the message objects.
You can support additional protocols by assigning a different one to each TCP/IP Port or by starting with a default protocol and then having the client and server negotiate a different protocol.
EDIT1:
I would not map a socket to a User, to be honest. I would abstract the way you communicate with a User into an object, perhaps Connection or Channel. You would then subclass the Connection with TcpConnection or CorbaConnection or whatever. Ideally you are doing something like:
Note that there is no mention of the connection or the protocol implementation details.
您尝试过服务定位器模式吗?但Java已经实现了你在其核心所做的事情,为什么要“重新发明轮子”。您可以使用 CORBA 或基于 TCP/UDP 的框架之一,例如ICE 如果您不喜欢 RMI,它将真正帮助您构建您的分布式应用程序。
did you try the Service Locator pattern. But Java already implements what you are doing in its core, why "re-invent the wheel". You can use CORBA or one of the TCP/UDP based frameworks, like ICE if you don't prefer RMI, and which will really help you build this distributed application of yours.