4
4
import android .util .Log ;
5
5
6
6
import java .io .IOException ;
7
- import java .net .DatagramPacket ;
8
- import java .net .DatagramSocket ;
9
7
import java .net .InetSocketAddress ;
10
-
11
- import static com . github . dddpaul . netcat . NetCater . State . CONNECTED ;
12
- import static com . github . dddpaul . netcat . NetCater . State . IDLE ;
8
+ import java . net . SocketAddress ;
9
+ import java . nio . ByteBuffer ;
10
+ import java . nio . channels . DatagramChannel ;
13
11
14
12
public class UdpNetCat extends NetCat
15
13
{
16
14
private final String CLASS_NAME = getClass ().getSimpleName ();
17
15
18
16
private NetCatTask task ;
19
- private DatagramSocket socket ;
17
+ private DatagramChannel channel ;
20
18
21
19
public UdpNetCat ( NetCatListener listener )
22
20
{
@@ -48,13 +46,13 @@ public void executeParallel( String... params )
48
46
@ Override
49
47
public boolean isListening ()
50
48
{
51
- return socket != null && socket . isBound ();
49
+ return channel != null && channel . isOpen ();
52
50
}
53
51
54
52
@ Override
55
53
public boolean isConnected ()
56
54
{
57
- return socket != null && socket .isConnected ();
55
+ return channel != null && channel .isConnected ();
58
56
}
59
57
60
58
public class NetCatTask extends Task
@@ -72,41 +70,42 @@ protected Result doInBackground( String... params )
72
70
String host = params [1 ];
73
71
port = Integer .parseInt ( params [2 ] );
74
72
Log .d ( CLASS_NAME , String .format ( "Connecting to %s:%d (UDP)" , host , port ) );
75
- socket = new DatagramSocket ();
76
- socket .connect ( new InetSocketAddress ( host , port ) );
77
- result .object = socket ;
73
+ channel = DatagramChannel . open ();
74
+ channel .connect ( new InetSocketAddress ( host , port ) );
75
+ result .object = channel . socket () ;
78
76
break ;
79
77
case LISTEN :
80
78
port = Integer .parseInt ( params [1 ] );
81
79
Log .d ( CLASS_NAME , String .format ( "Listening on %d (UDP)" , port ) );
82
- socket = new DatagramSocket ( port );
83
- result .object = socket ;
80
+ channel = DatagramChannel .open ();
81
+ channel .socket ().bind ( new InetSocketAddress ( port ) );
82
+ result .object = channel .socket ();
84
83
break ;
85
84
case RECEIVE :
86
85
if ( isListening () ) {
87
86
// Connect after receive is necessary for further sending
88
- DatagramPacket packet = receiveFromDatagramSocket ();
89
- Log .d ( CLASS_NAME , String .format ( "Received data from %s (UDP)" , packet . getSocketAddress () ) );
90
- socket .connect ( packet . getSocketAddress () );
91
- Log .d ( CLASS_NAME , String .format ( "Connected to %s (UDP)" , packet . getSocketAddress () ) );
87
+ SocketAddress remoteSocketAddress = receiveFromChannel ();
88
+ Log .d ( CLASS_NAME , String .format ( "Received data from %s (UDP)" , remoteSocketAddress ) );
89
+ channel .connect ( remoteSocketAddress );
90
+ Log .d ( CLASS_NAME , String .format ( "Connected to %s (UDP)" , channel . socket (). getRemoteSocketAddress () ) );
92
91
}
93
92
break ;
94
93
case SEND :
95
94
if ( isConnected () ) {
96
- Log .d ( CLASS_NAME , String .format ( "Sending to %s (UDP)" , socket .getRemoteSocketAddress () ) );
97
- sendToDatagramSocket ();
95
+ Log .d ( CLASS_NAME , String .format ( "Sending to %s (UDP)" , channel . socket () .getRemoteSocketAddress () ) );
96
+ sendToChannel ();
98
97
}
99
98
break ;
100
99
case DISCONNECT :
101
100
if ( isConnected () ) {
102
- Log .d ( CLASS_NAME , String .format ( "Disconnecting from %s (UDP)" , socket .getRemoteSocketAddress () ) );
101
+ Log .d ( CLASS_NAME , String .format ( "Disconnecting from %s (UDP)" , channel . socket () .getRemoteSocketAddress () ) );
103
102
}
104
103
if ( isListening () ) {
105
- Log .d ( CLASS_NAME , String .format ( "Stop listening on %d (UDP)" , socket .getLocalPort () ) );
104
+ Log .d ( CLASS_NAME , String .format ( "Stop listening on %d (UDP)" , channel . socket () .getLocalPort () ) );
106
105
}
107
106
if ( isConnected () || isListening () ) {
108
- socket .close ();
109
- socket = null ;
107
+ channel .close ();
108
+ channel = null ;
110
109
}
111
110
break ;
112
111
}
@@ -117,22 +116,21 @@ protected Result doInBackground( String... params )
117
116
return result ;
118
117
}
119
118
120
- private DatagramPacket receiveFromDatagramSocket () throws IOException
119
+ private SocketAddress receiveFromChannel () throws IOException
121
120
{
122
- byte [] buf = new byte [ 1024 ] ;
123
- DatagramPacket packet = new DatagramPacket ( buf , buf . length );
124
- socket .receive ( packet );
125
- output .write ( packet . getData (), 0 , packet . getLength () );
126
- return packet ;
121
+ ByteBuffer buf = ByteBuffer . allocate ( 1024 ) ;
122
+ buf . clear ( );
123
+ SocketAddress remoteSocketAddress = channel .receive ( buf );
124
+ output .write ( buf . array (), 0 , buf . position () );
125
+ return remoteSocketAddress ;
127
126
}
128
127
129
- private void sendToDatagramSocket () throws IOException
128
+ private void sendToChannel () throws IOException
130
129
{
131
130
byte [] buf = new byte [1024 ];
132
131
int bytesRead = input .read ( buf , 0 , buf .length );
133
132
if ( bytesRead > 0 ) {
134
- DatagramPacket packet = new DatagramPacket ( buf , bytesRead );
135
- socket .send ( packet );
133
+ channel .send ( ByteBuffer .wrap ( buf ), channel .socket ().getRemoteSocketAddress () );
136
134
}
137
135
}
138
136
}
0 commit comments