Skip to content

Commit a048d91

Browse files
committed
Send after receive in UDP listen mode works
1 parent 05d49bb commit a048d91

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ protected Result doInBackground( String... params )
6666
Op op = Op.valueOf( params[0] );
6767
Result result = new Result( op, Proto.UDP );
6868
try {
69-
Log.d( CLASS_NAME, String.format( "Executing %s operation", op ) );
69+
Log.d( CLASS_NAME, String.format( "Executing %s operation (UDP)", op ) );
7070
int port;
7171
switch( op ) {
7272
case CONNECT:
7373
String host = params[1];
7474
port = Integer.parseInt( params[2] );
75-
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (UDP)", host, port ) );
7675
channel = DatagramChannel.open();
7776
channel.connect( new InetSocketAddress( host, port ) );
7877
channel.configureBlocking( false );
79-
Log.i( CLASS_NAME, "Connected to " + channel.socket().getRemoteSocketAddress() );
78+
Log.i( CLASS_NAME, String.format( "Connected to %s (UDP)", channel.socket().getRemoteSocketAddress() ) );
8079
result.object = channel.socket();
8180
break;
8281
case LISTEN:
@@ -89,18 +88,11 @@ protected Result doInBackground( String... params )
8988
break;
9089
case RECEIVE:
9190
if( isListening() ) {
92-
SocketAddress remoteSocketAddress = receiveFromChannel();
93-
if( remoteSocketAddress != null ) {
94-
// Connect after receive is necessary for further sending
95-
Log.d( CLASS_NAME, String.format( "Received data from %s (UDP)", remoteSocketAddress ) );
96-
channel.connect( remoteSocketAddress );
97-
Log.d( CLASS_NAME, String.format( "Connected to %s (UDP)", channel.socket().getRemoteSocketAddress() ) );
98-
}
91+
receiveFromChannel();
9992
}
10093
break;
10194
case SEND:
10295
if( isConnected() ) {
103-
Log.d( CLASS_NAME, String.format( "Sending to %s (UDP)", channel.socket().getRemoteSocketAddress() ) );
10496
sendToChannel();
10597
}
10698
break;
@@ -119,19 +111,31 @@ protected Result doInBackground( String... params )
119111
return result;
120112
}
121113

122-
private SocketAddress receiveFromChannel() throws Exception
114+
private void receiveFromChannel() throws Exception
123115
{
124116
SocketAddress remoteSocketAddress = null;
117+
int bytesReceived = 0;
125118
try {
126119
ByteBuffer buf = ByteBuffer.allocate( Constants.MAX_PACKET_SIZE );
127120
buf.clear();
128121
while( !task.isCancelled() ) {
129-
remoteSocketAddress = channel.receive( buf );
130-
if( remoteSocketAddress != null ) {
131-
Log.d( CLASS_NAME, String.format( "%d bytes was received from %s", buf.position() - 1, remoteSocketAddress ));
122+
if( isListening() && !isConnected() ) {
123+
remoteSocketAddress = channel.receive( buf );
124+
bytesReceived = buf.position() - 1;
125+
}
126+
if( isConnected() ) {
127+
bytesReceived = channel.read( buf );
128+
}
129+
if( bytesReceived > 0 ) {
130+
Log.d( CLASS_NAME, String.format( "%d bytes was received from %s", bytesReceived, remoteSocketAddress ));
132131
output.write( buf.array(), 0, buf.position() );
133132
publishProgress( CONNECTED.toString(), output.toString() );
134133
buf.clear();
134+
// Connect after receive is necessary for further sending
135+
if( !isConnected() ) {
136+
channel.connect( remoteSocketAddress );
137+
Log.d( CLASS_NAME, String.format( "Connected to %s (UDP)", channel.socket().getRemoteSocketAddress() ) );
138+
}
135139
}
136140
Thread.sleep( 100 );
137141
}
@@ -143,11 +147,11 @@ private SocketAddress receiveFromChannel() throws Exception
143147
// This exception is thrown when socket for receiver thread is closed by netcat
144148
Log.w( CLASS_NAME, e.toString() );
145149
}
146-
return remoteSocketAddress;
147150
}
148151

149152
private void sendToChannel() throws IOException
150153
{
154+
Log.d( CLASS_NAME, String.format( "Sending to %s (UDP)", channel.socket().getRemoteSocketAddress() ) );
151155
byte[] buf = new byte[1024];
152156
int bytesRead = input.read( buf, 0, buf.length );
153157
if( bytesRead > 0 ) {

0 commit comments

Comments
 (0)