实时应用程序(服务器/客户端)的设计(类、方法、接口)
我一直在寻找一本关于这个主题的好书或文章,但没有找到太多。我没有找到适合特定场景的好示例(一段代码)。就像客户端/服务器对话一样。 在我的应用程序协议中,它们必须发送/接收消息。喜欢: 服务器想要向客户端发送文件 客户可以接受也可以不接受, 如果他接受,服务器将通过同一连接/套接字发送字节。 我的应用程序的其余部分都使用阻塞方法,服务器有一个方法
继承人我所做的:
服务器方法:
public synchronized void sendFile(File file)
{
//send messsage asking if I can send a file
//block on read, waiting for client responde
//if client answers yes, start sending the bytes
//else return
}
客户端方法:
public void reciveCommand()
{
//read/listen for a command from socket
//if is a send file command handleSendFileCommand();
//after the return of handleSendFileCommand() listen for another command
}
public void handleSendFileCommand()
{
//get the file server want to send
//check if it already has the file
//if it already has, then send a command to the socket saying it already has and return
//else send a command saying server can send the file
//create a FileInputStream, recive bytes and then return method
}
我100%确定这是错误的,因为,服务器和客户端不可能双向对话,我的意思是,当服务器想要向服务器发送命令,它们必须遵循命令的顺序,直到会话完成,然后才能发送/接收另一个命令序列。这就是为什么我同步发送请求的所有方法,
我并没有花很多时间就意识到我需要研究此类应用程序的设计模式...... 我读过有关责任链设计模式的内容,但我不明白在这种情况下如何使用它或其他好的设计模式。
我希望有人可以帮助我一些代码示例。 提前致谢
I´ve been looking for a good book or article about this topic but didnt find much. I didnt find a good example - piece of code - for a specific scenario. Like clients/server conversation.
In my application´s protocol they have to send/recieve messages. Like:
Server want to send a file to a client
Client can accpet or no,
if he accepts, server will send bytes over the same connection/socket.
The rest of my application all uses blocking methods, server has a method
Heres what I did:
Server method:
public synchronized void sendFile(File file)
{
//send messsage asking if I can send a file
//block on read, waiting for client responde
//if client answers yes, start sending the bytes
//else return
}
Client methods:
public void reciveCommand()
{
//read/listen for a command from socket
//if is a send file command handleSendFileCommand();
//after the return of handleSendFileCommand() listen for another command
}
public void handleSendFileCommand()
{
//get the file server want to send
//check if it already has the file
//if it already has, then send a command to the socket saying it already has and return
//else send a command saying server can send the file
//create a FileInputStream, recive bytes and then return method
}
I am 100% sure this is wrong because, there is no way server and clients would talk bidirecional, I mean, when server wants to send a command to a server, they have to follow an order of commands until that conversation is finished, only then, they can send/recive another sequence of commands. Thats why I made all methods that send requests synchronized
It didnt took me a lot of time to realize I need to study about design patterns for that kind of application...
I read about Chain of Responsibility design pattern but I dont get it how can I use it or another good design pattern in that situation.
I hope someone can help me with some code example-like.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 中的
synchronized
关键字意味着完全不同的东西 - 它将方法或代码块标记为 临界区一次只能执行一个线程。你在这里不需要它。那么,TCP 连接在字节流级别上是双向的。服务器和客户端之间的同步由交换的消息驱动。将客户端(几乎同样适用于服务器)视为
状态机
。有些类型的消息在当前状态下是可接受的,有些则不可接受,有些会将节点切换到不同的状态。
由于您正在研究设计模式,因此
状态模式
在这里非常适用。synchronized
keyword in Java means something completely different - it marks a method or a code block as a critical section that only single thread can execute at a time. You don't need it here.Then, a TCP connection is bi-directional on the byte-stream level. The synchronization between the server and a client is driven by the messages exchanged. Think of a client (same pretty much applies to the server) as a
state machine
. Some types of messages are acceptable in the current state, some are not, some switch the node into different state.Since you are looking into design patterns, the
State pattern
is very applicable here.