@@ -66,17 +66,16 @@ protected Result doInBackground( String... params )
66
66
Op op = Op .valueOf ( params [0 ] );
67
67
Result result = new Result ( op , Proto .UDP );
68
68
try {
69
- Log .d ( CLASS_NAME , String .format ( "Executing %s operation" , op ) );
69
+ Log .d ( CLASS_NAME , String .format ( "Executing %s operation (UDP) " , op ) );
70
70
int port ;
71
71
switch ( op ) {
72
72
case CONNECT :
73
73
String host = params [1 ];
74
74
port = Integer .parseInt ( params [2 ] );
75
- Log .d ( CLASS_NAME , String .format ( "Connecting to %s:%d (UDP)" , host , port ) );
76
75
channel = DatagramChannel .open ();
77
76
channel .connect ( new InetSocketAddress ( host , port ) );
78
77
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 () ) );
80
79
result .object = channel .socket ();
81
80
break ;
82
81
case LISTEN :
@@ -89,18 +88,11 @@ protected Result doInBackground( String... params )
89
88
break ;
90
89
case RECEIVE :
91
90
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 ();
99
92
}
100
93
break ;
101
94
case SEND :
102
95
if ( isConnected () ) {
103
- Log .d ( CLASS_NAME , String .format ( "Sending to %s (UDP)" , channel .socket ().getRemoteSocketAddress () ) );
104
96
sendToChannel ();
105
97
}
106
98
break ;
@@ -119,19 +111,31 @@ protected Result doInBackground( String... params )
119
111
return result ;
120
112
}
121
113
122
- private SocketAddress receiveFromChannel () throws Exception
114
+ private void receiveFromChannel () throws Exception
123
115
{
124
116
SocketAddress remoteSocketAddress = null ;
117
+ int bytesReceived = 0 ;
125
118
try {
126
119
ByteBuffer buf = ByteBuffer .allocate ( Constants .MAX_PACKET_SIZE );
127
120
buf .clear ();
128
121
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 ));
132
131
output .write ( buf .array (), 0 , buf .position () );
133
132
publishProgress ( CONNECTED .toString (), output .toString () );
134
133
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
+ }
135
139
}
136
140
Thread .sleep ( 100 );
137
141
}
@@ -143,11 +147,11 @@ private SocketAddress receiveFromChannel() throws Exception
143
147
// This exception is thrown when socket for receiver thread is closed by netcat
144
148
Log .w ( CLASS_NAME , e .toString () );
145
149
}
146
- return remoteSocketAddress ;
147
150
}
148
151
149
152
private void sendToChannel () throws IOException
150
153
{
154
+ Log .d ( CLASS_NAME , String .format ( "Sending to %s (UDP)" , channel .socket ().getRemoteSocketAddress () ) );
151
155
byte [] buf = new byte [1024 ];
152
156
int bytesRead = input .read ( buf , 0 , buf .length );
153
157
if ( bytesRead > 0 ) {
0 commit comments