Is it possible to use a SocketChannel with a BluetoothSocket on Android - java

you can make a SocketChannel from a SocketAddress, but suppose you have a BluetoothSocket. How to make a SocketChannel?

No, but you can get a ReadableByteChannel and a WriteableByteChannel from its input and output streams via the Channels class.

Related

Java.Nio - ByteBuffer give weird character in json string type message

I have Java Client-Server application I built using standard java.io (Socket and ServerSocket). In both server and client, I use DataInputStream to read the message using readUTF and DataOutputStream to write the message using writeUTF. I use thread-per-connection architecture for this application.
I have an additional feature that I need to add now, which is allowing a client to have a persistent connection open to the server and always listening to what server will write. I found out, with my current thread-per-connection architecture, my application won't scale because it holds the thread when the client opens the persistent connection.
I did some research and think to refactor my server to use java.nio (SocketChannel and ServerSocketChannel). I try to make it compatible with the client (so I don't need to change the client). This is when the problem occurs because I need to change from readUTF method to using ByteBuffer class to read the message, now I got a weird character in my message.
This is my message that works before using readUTF (Server) that come from writeUTF (Client)
{"command":"PUBLISH","resource":{"name":"","description":"","tags":[]}
When I sent the same message to my new server, I got this in my read method
�{"command":"PUBLISH","resource":{"name":"","description":"","tags":[]}
My read method is:
else if (clientKey.isReadable()) {
SocketChannel clientSocket = (SocketChannel) clientKey.channel();
ByteBuffer buffer = ByteBuffer.allocate(4096);
clientSocket.read(buffer);
String message = new String(buffer.array());
Logger.debug(message);
clientSocket.register(selector, SelectionKey.OP_WRITE);
}
This is how I write the message from client:
try (Socket echoSocket = new Socket(hostName, portNumber);
DataOutputStream streamOut = new DataOutputStream(echoSocket.getOutputStream());) {
streamOut.writeUTF(message.toJson());
}
toJson() is just a method to convert java object to JSON string and I use Jackson library to do that.
I have tried to remove it using regex and Normalizer library but it won't work.
Is there anyone experience the same things and solve it?

Socket close vs Inputstream close

Class c extends thread
static Queue<Socket> socketQueue
Make connection to another server or client
And then add socket to socketqueue
Class a extends thread
method a
bufferedinputstream bis = socketQueue.poll
Do work
Make bis null without closing it<br>
Class b extends thread
Method b
Bufferedinputstream bis = socketqueue.poll
Do work
Make bis null without closing it
I did make bufferedinput stream null since i do not want to close the connected socket. Several posts were telling me that closing input/output stream would close the socket as well.
Whenever I use input/output stream with socket, I usually close stream and socket if its not null.
What I am trying to do here is to make the socket alive and reuse when input or output stream is needed without connecting again.
I tried socket.shutdowninput and output, however, this throws an exception when i make another input/output stream with the socket.
Is there anything I have misunderstood or am missing at this point?
A connection over a socket only ever has one InputStream and one OutputStream. As soon as you close any of those (or the Socket itself) the connection is automatically closed. You need to store the streams you need somewhere and use those, you can not get them from the same Socket each time you need them.

Use the underlying Socket/ServerSocket in a SocketChannel/ServerSocketChannel?

I'm trying the Java.nio-package for non-blocking communication. So I got my ServerSocketChannel and all my connected clients (SocketChannel) in a Selector and wait for data (OP_ACCEPT/OP_READ) using Selector.select().
My question is: Can I - instead of using a ByteBuffer and read directly with SocketChannel.read() - use the underlying Socket, get an InputStream and read using that stream? Or will that mess up the selector-stuff?
You can't.
http://download.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#getInputStream%28%29
If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the input stream's read operations will throw an IllegalBlockingModeException.

How to set the send and receive buffer size of a Java nio socket

Is there any way to set the send buffer and recieve buffer of a sock channel in Java Nio? Thanks.
socketChannel.socket().setSendBufferSize() etc.

Timeout for SocketChannel doesn't work

I want to use a SocketChannel and to have a timeout for its read/write methods. I've tried to set a timeout for the Socket that owns my SocketChannel like this:
channel.socket().setSoTimeout(TIMEOUT);
but that doesn't work. Is there any other solution?
According to this article, SocketChannel will not timeout for its read operation but you can get this effect from reading from the channel in another way.
SocketChannel socketChannel;
socketChannel.socket().setSocketTimeout(500);
InputStream inStream = socketChannel.socket().getInputStream();
ReadableByteChannel wrappedChannel = Channels.newChannel(inStream);
reading from the wrappedChannel will timeout according to the socketTimeOut you have set.
If you are familiar with using Java Selector, you can emulate socket timeout yourself using selector. It is helpful to see sun.nio.ch.SocketAdaptor.
It should be careful to use Thread.interrupt().
SocketChannel is InterruptibleChannel. As you read the description of InterruptibleChannel, Thread.interrupt() causes to close SocketChannel.
If you want to use SocketChannel after timeout, you cannot use the InterruptibleChannel feature.
You could also consider making your channel non-blockable and just using System.currentTimeMillis().

Categories

Resources