NIO-based UDP send-receiver
enter:
【】indicates optional functions and can not be used
Output:
Add the following code to your project app/build.gradle
dependencies {
...
compile 'com.jwkj:udpsender:v2.0.2'
}
UDPSender . getInstance ()
. setInstructions ( byteAraary )
. setTargetPort ( port )
. start ( new UDPResultCallback () {
@ Override
public void onNext ( UDPResult result ) {
//do something
}
});
UDPSender . getInstance ()
. setInstructions ( byteAraary ) //设置发送的指令[可为空]
. setReceiveTimeOut ( 70 * 1000 ) //设置接收超时时间[可不写,默认为60s]--超时70s就停止任务
. setTargetPort ( port ) //设置发送的端口[必写]
. setLocalReceivePort ( port ) //设置本机接收的端口[可不写,默认为目标端口]
. setTargetIp ( "192.168.1.150" ) //设置目标ip地址,[可不写,默认广播]
. schedule ( 2 , 3000 ) //执行2次,间隔三秒执行(上一次结束到下一次开始的时间)
. start ( new UDPResultCallback () {
@ Override
public void onStart () {
//请求开始的时候回调
}
@ Override
public void onNext ( UDPResult result ) {
//每收到一个结果的时候就回调
}
@ Override
public void onCompleted () {
//请求结束的时候回调
}
@ Override
public void onError ( Throwable throwable ) {
//当发生错误的时候回调
}
});
UDPSender . getInstance ()
. setLocalReceivePort ( 9988 ) //接收端口
. start ( new UDPResultCallback () {
@ Override
public void onStart () {
ELog . hdl ( "开始了" );
}
@ Override
public void onNext ( UDPResult result ) {
ELog . hdl ( "收到结果" + result );
}
@ Override
public void onError ( Throwable throwable ) {
ELog . hdl ( "出错" + throwable );
}
@ Override
public void onCompleted () {
ELog . hdl ( "完成" );
}
});
UDPSender . getInstance (). stop ();
public class UDPResult {
/**
* 对方ip地址
*/
private String ip ;
/**
* 对方返回的结果
*/
private byte [] resultData ;
}
v2.0.2 (2017.10.27)
v2.0.1
Warm reminder: It is not recommended to use v1.X version. It is recommended to use the latest version. The usage method remains unchanged.
v1.4.0
[New] When specifying an IP, the package will only be sent once by default (if you need to send it multiple times, you can specify it through the schedule method)
[bug] Only the sender can specify the port, bug, do not use this version
v1.3.9
[Fix] The sudden stop when calling a scheduled task causes the current task to be stopped immediately when the task is restarted next time.
[bug] Only the sender can specify the port, bug, do not use this version
v1.3.8
【Fix】Fix crash under special circumstances
[bug] Only the sender can specify the port, bug, do not use this version
v1.3.7
[Fix] Automatically close related interfaces when the task ends
[bug] Only the sender can specify the port, bug, do not use this version
v1.3.6
[Fix] Call onCompleted method multiple times
[bug] Only the sender can specify the port, bug, do not use this version
v1.3.5
[Fix] Closing the task (UDPSender.getInstance().stop()) does not use the onCompleted method
[New] Set target IP method (default broadcast form)
[bug] Only the sender can specify the port, bug, do not use this version
More historical versions have not been recorded yet