简体   繁体   English

Java套接字发送和接收

[英]java socket sending and receiving

since hours I am thinking about how I should use Java Sockets to send and receive data. 几个小时以来,我一直在思考如何使用Java套接字发送和接收数据。 I hope you can help me finding a good way to do it. 希望您能帮助我找到一个好的方法。

I have build a Server/Client structure where the client connects to the server and gets his own thread handling a request-response method. 我已经建立了一个服务器/客户端结构,其中客户端连接到服务器并获得了自己的线程来处理请求-响应方法。 The information are capsuled in XML. 该信息以XML封装。

At the beginning the Server starts a blocking socket read. 在开始时,服务器开始读取阻止套接字。 First I use writeInt() where the lenght of the XML message is transmitted. 首先,我使用writeInt()来传输XML消息的长度。 After that the Server reads the amount of lenght bytes and parses the message. 之后,服务器读取长度字节的数量并解析消息。 After the transmission the client goes in the receive state and waits for an response. 传输后,客户端进入接收状态并等待响应。

That is fine but after the client authenticates the server waits for the things that will come and blocks. 很好,但是在客户端进行身份验证之后,服务器会等待即将发生的事情并阻止。

But what should I do when the server has no information which needs to be transmitted. 但是,如果服务器没有需要传输的信息,我该怎么办。 I could try to destroy the read blocking and send the message to the client. 我可以尝试破坏读取阻止并将消息发送给客户端。 But what happens if the client comens in mind that he has also a message and also begins to send. 但是如果客户想到他也有一条消息并且也开始发送该怎么办。 In that moment no one will listen. 在那一刻,没人会听。

For that maybe I could use some sort of buffer but I have the feeling that this is not the way I should go. 为此,也许我可以使用某种缓冲区,但是我感觉这不是我应该走的路。

I have been searching for a while and found some interesting information but I didn't understand them well. 我已经搜索了一段时间,发现了一些有趣的信息,但是我不太了解它们。 Sadly my book is only about this simple socket model. 可悲的是,我的书只是关于这个简单的套接字模型的。

Maybe I should use two threads. 也许我应该使用两个线程。 One sending and one receiving. 一发送一接收。 The server has a database where the messages are stored and waiting for transmission. 服务器具有一个数据库,在该数据库中存储消息并等待传输。 So when the server receives a message he stores that message in the database and also the answer like "message received" would be stored in the database. 因此,当服务器接收到一条消息时,他将该消息存储在数据库中,并且还将诸如“收到的消息”之类的答案存储在数据库中。 The sender thread would look if there are new messages and would send "message received" to the client. 发件人线程将查看是否有新消息,并将“已收到消息”发送给客户端。 That approach would not send the answer in millisenconds but I could image that it would work well. 这种方法不会在千里之外给出答案,但是我可以想象它会很好地工作。

I hope that I gave you enough information about what I am trying. 我希望我能给您有关我正在尝试的信息。 What would you recommend me how to implement that communication? 您会如何建议我实现这种沟通?

Thanks 谢谢

But what should I do when the server has now information which needs to be transmitted. 但是,当服务器现在具有需要传输的信息时,我该怎么办。

I would make writes from the server synchronized. 我将从服务器同步写入。 This will allow you to respond to requests in one thread, but also send other messages as required by another thread. 这将使您可以在一个线程中响应请求,还可以根据另一个线程的要求发送其他消息。 For the client you can have a separate thread doing the receiving. 对于客户端,您可以有一个单独的线程来进行接收。

A simpler model may be to have two sockets. 一个更简单的模型可能是具有两个插槽。 One socket works as you do now and when you "login" a unique id is sent to the client. 一个套接字就像您现在所做的那样工作,当您“登录”时,唯一的ID将发送到客户端。 At this point the client opens a second connection with a dedicated thread for listening to asynchronous events. 此时,客户端使用专用线程打开第二个连接,以监听异步事件。 It passes the unique id so the server know which client the asynchronous messages are for. 它传递唯一的ID,以便服务器知道异步消息用于哪个客户端。

This will give a you a simple synchronous pattern as you have now and a simple asynchronous pattern. 这将为您提供一个像现在一样的简单同步模式,以及一个简单的异步模式。 The only downside is you have two socket connections and you have co-ordinate them. 唯一的缺点是您有两个套接字连接,并且已协调它们。

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

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