java中如何使用在一个端口进行同时接受和发送

需求是这样的:客户端每隔10s向服务器发送一个心跳包(本地端口随机),服务器收到后会记下客户端的端口,当有某些业务的时候服务器会使用这个端口发送数据给客户端。问题来了,当... 需求是这样的:客户端每隔10s向服务器发送一个心跳包(本地端口随机),服务器收到后会记下客户端的端口,当有某些业务的时候服务器会使用这个端口发送数据给客户端。
问题来了,当我使用同一个DatagramSocket对象进行recive和send的话,recvie就会阻塞起,从而无法执行send,如果给recive设定超时,然后send之后进行循环,那也只能是一个send一个recive,但是实际上服务器有可能连续发送N个数据;如果使用两个DatagramSocket对象的话,根本没法绑定到同一个端口。
求一个解决方案!
使用UDP
展开
 我来答
grace23450
推荐于2016-09-28 · TA获得超过1.3万个赞
知道大有可为答主
回答量:7081
采纳率:77%
帮助的人:2321万
展开全部
Java NIO DatagramChannel

A Java NIO DatagramChannel is a channel that can send and receive UDP packets. Since UDP is a connection-less network protocol, you cannot just by default read and write to a DatagramChannel like you do from other channels. Instead you send and receive packets of data.

Opening a DatagramChannel
Here is how you open a DatagramChannel:
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));

This example opens a DatagramChannel which can receive packets on UDP port 9999.

1、接收数据-Receiving Data
You receive data from a DatagramChannel by calling its receive() method, like this:
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();

channel.receive(buf);

The receive() method will copy the content of a received packet of data into the given Buffer. If the received packet contains more data than the Buffer can contain, the remaining data is discarded silently.

二、发送数据-Sending Data
You can send data via a DatagramChannel by calling its send() method, like this:
String newData = "New String to write to file..."
+ System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();

int bytesSent = channel.send(buf, new InetSocketAddress("jenkov.com", 80));

This example sends the string to the "jenkov.com" server on UDP port 80. Nothing is listening on that port though, so nothing will happen. You will not be notified of whether the send packet was received or not, since UDP does not make any guarantees about delivery of data.

Connecting to a Specific Address
It is possible to "connect" a DatagramChannel to a specific address on the network. Since UDP is connection-less, this way of connecting to an address does not create a real connection, like with a TCP channel. Rather, it locks your DatagramChannel so you can only send and receive data packets from one specific address.
Here is an example:
channel.connect(new InetSocketAddress("jenkov.com", 80));

When connected you can also use the read() and write() method, as if you were using a traditional channel. You just don't have any guarantees about delivery of the sent data. Here are a few examples:
int bytesRead = channel.read(buf);

int bytesWritten = channel.write(buf);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
11无敌队长
2015-07-21 · TA获得超过167个赞
知道小有建树答主
回答量:380
采纳率:50%
帮助的人:72.3万
展开全部
用DatagramChannel,关键词:nio,非阻塞。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
偷偷地笑呗
2013-10-30 · TA获得超过163个赞
知道小有建树答主
回答量:124
采纳率:0%
帮助的人:87.8万
展开全部
使用NIO。。建议百度下。就是专门解决阻塞问题
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友33dcad5a0
2013-10-29 · TA获得超过164个赞
知道答主
回答量:69
采纳率:100%
帮助的人:81.1万
展开全部
用DatagramChannel,关键词:nio,非阻塞。
追问
哥们,给个demo什么的吧,好歹也是100分啊- -
追答

只是恰好知道有这么个解决办法,还没写过相关代码,一起学习吧。

----

找到一个简单例子

tutorials.jenkov.com/java-nio/datagram-channel.html
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友5a60616
2013-10-29
知道答主
回答量:47
采纳率:0%
帮助的人:6.3万
展开全部
使用多线程就可以了
追问
多线程没用,关键还是DatagramSocket对象,只要你使用了一个DatagramSocket对象进行recive了不管在哪个线程中再使用这个对象进行send都没用,会被阻塞的!
如果这么简单我早弄好了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 4条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式