Skip to content

Commit b028f7d

Browse files
committed
Rewrite UdpNetCat implementation using DatagramChannel
1 parent a9bab57 commit b028f7d

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

netcat/src/main/java/com/github/dddpaul/netcat/UdpNetCat.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
import android.util.Log;
55

66
import java.io.IOException;
7-
import java.net.DatagramPacket;
8-
import java.net.DatagramSocket;
97
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;
1311

1412
public class UdpNetCat extends NetCat
1513
{
1614
private final String CLASS_NAME = getClass().getSimpleName();
1715

1816
private NetCatTask task;
19-
private DatagramSocket socket;
17+
private DatagramChannel channel;
2018

2119
public UdpNetCat( NetCatListener listener )
2220
{
@@ -48,13 +46,13 @@ public void executeParallel( String... params )
4846
@Override
4947
public boolean isListening()
5048
{
51-
return socket != null && socket.isBound();
49+
return channel != null && channel.isOpen();
5250
}
5351

5452
@Override
5553
public boolean isConnected()
5654
{
57-
return socket != null && socket.isConnected();
55+
return channel != null && channel.isConnected();
5856
}
5957

6058
public class NetCatTask extends Task
@@ -72,41 +70,42 @@ protected Result doInBackground( String... params )
7270
String host = params[1];
7371
port = Integer.parseInt( params[2] );
7472
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();
7876
break;
7977
case LISTEN:
8078
port = Integer.parseInt( params[1] );
8179
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();
8483
break;
8584
case RECEIVE:
8685
if( isListening() ) {
8786
// 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() ) );
9291
}
9392
break;
9493
case SEND:
9594
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();
9897
}
9998
break;
10099
case DISCONNECT:
101100
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() ) );
103102
}
104103
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() ) );
106105
}
107106
if( isConnected() || isListening() ) {
108-
socket.close();
109-
socket = null;
107+
channel.close();
108+
channel = null;
110109
}
111110
break;
112111
}
@@ -117,22 +116,21 @@ protected Result doInBackground( String... params )
117116
return result;
118117
}
119118

120-
private DatagramPacket receiveFromDatagramSocket() throws IOException
119+
private SocketAddress receiveFromChannel() throws IOException
121120
{
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;
127126
}
128127

129-
private void sendToDatagramSocket() throws IOException
128+
private void sendToChannel() throws IOException
130129
{
131130
byte[] buf = new byte[1024];
132131
int bytesRead = input.read( buf, 0, buf.length );
133132
if( bytesRead > 0 ) {
134-
DatagramPacket packet = new DatagramPacket( buf, bytesRead );
135-
socket.send( packet );
133+
channel.send( ByteBuffer.wrap( buf ), channel.socket().getRemoteSocketAddress() );
136134
}
137135
}
138136
}

0 commit comments

Comments
 (0)