Last active
December 19, 2017 04:52
-
-
Save kheldiente/a422b36178d7917ba831d8f597fabfe2 to your computer and use it in GitHub Desktop.
Non blocking socket channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.Log; | |
import android.widget.Toast; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.UnknownHostException; | |
import java.nio.ByteBuffer; | |
import java.nio.CharBuffer; | |
import java.nio.channels.SocketChannel; | |
import java.nio.charset.Charset; | |
import java.nio.charset.CharsetDecoder; | |
/** | |
* Created by kheldiente on 11/18/16. | |
*/ | |
public class TCPClientNIO { | |
private static final String TAG = TCPClientNIO.class.getSimpleName(); | |
public SocketChannel socketChannel = null; | |
public InetSocketAddress isa = null; | |
public RecvThread rt = null; | |
Context ctx; | |
boolean connected = false; | |
boolean connectionRefused = false; | |
boolean connectionTimeout = false; | |
String SERVER_IP = ""; | |
String SERVER_PORT = ""; | |
String mThreadName = ""; | |
public TCPClientNIO(Context context, String ip, String port, String threadName) { | |
ctx = context; | |
SERVER_IP = ip; | |
SERVER_PORT = port; | |
mThreadName = threadName; | |
socketChannel = null; | |
isa = null; | |
rt = null; | |
connected = false; | |
connectionTimeout = false; | |
connectionRefused = false; | |
} | |
public boolean isConnected() { | |
return connected; | |
} | |
public boolean isConnectionTimedOut() | |
{ | |
return connectionTimeout; | |
} | |
public boolean isConnectionRefused() { | |
return connectionRefused; | |
} | |
public void run() { | |
int result = 0; | |
connected = false; | |
connectionRefused = false; | |
connectionTimeout = false; | |
try { | |
socketChannel = SocketChannel.open(); | |
isa = new InetSocketAddress(SERVER_IP, Integer.parseInt(SERVER_PORT)); | |
socketChannel.connect(isa); | |
socketChannel.configureBlocking(false); | |
receiveMessage(); | |
Log.d(TAG, "connected"); | |
connected = true; | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
Log.d(TAG, "connection refused"); | |
connectionRefused = true; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Log.d(TAG, "connection refused"); | |
connectionRefused = true; | |
} | |
} | |
public String getMessageFromServer() { | |
return messageFromServer; | |
} | |
public void setMessageFromServer(String msg) { | |
messageFromServer = msg; | |
} | |
public void stop() { | |
if(connected) { | |
Log.d(TAG, "init to connection closing"); | |
try { | |
messageFromServer = null; | |
interruptThread(); | |
socketChannel.close(); | |
connected = false; | |
connectionRefused = false; | |
connectionTimeout = false; | |
Log.d(TAG, "connection closed"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public boolean sendMessage(String msg) { | |
int nBytes = 0; | |
boolean sent = false; | |
if (msg == null) { | |
Toast.makeText(ctx, "Msg is null", Toast.LENGTH_SHORT).show(); | |
return sent; | |
} | |
if(connected) { | |
Log.d(TAG, "sending message: " + msg); | |
ByteBuffer bytebuf = ByteBuffer.allocate(1024); | |
try { | |
bytebuf = ByteBuffer.wrap(msg.getBytes()); | |
nBytes = socketChannel.write(bytebuf); | |
sent = true; | |
Log.d(TAG, "sent message"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Log.d(TAG, "connection timeout when sending"); | |
connectionTimeout = true; | |
} | |
} | |
return sent; | |
} | |
public void receiveMessage() { | |
rt = new RecvThread(mThreadName, socketChannel); // Receive Thread is an alias for thread!! | |
rt.start(); | |
} | |
public void interruptThread() { | |
rt.val = false; | |
} | |
String messageFromServer = null; | |
public class RecvThread extends Thread { | |
public SocketChannel sc = null; | |
public boolean val = true; | |
public RecvThread(String str, SocketChannel client) { | |
super(str); | |
sc = client; | |
} | |
public void run() { | |
int nBytes = 0; | |
ByteBuffer buf = ByteBuffer.allocate(2048); | |
try { | |
while (val) { | |
while ((nBytes = nBytes = socketChannel.read(buf)) > 0) { | |
buf.flip(); | |
Charset charset = Charset.forName("us-ascii"); | |
CharsetDecoder decoder = charset.newDecoder(); | |
CharBuffer charBuffer = decoder.decode(buf); | |
String result = charBuffer.toString(); | |
messageFromServer = result; | |
Log.d(TAG, "message: " + result); | |
buf.flip(); | |
} | |
} | |
Log.d(TAG, "listener while loop has stopped"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Log.d(TAG, "connection timeout"); | |
connectionTimeout = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment