The following code sends hello from a client thread to a server thread over a UnixSocketChannel, and the server prints out the character codes (pardon my Scala)
package mill.clientserver
import java.nio.channels.Channels
import jnr.unixsocket.{UnixServerSocketChannel, UnixSocketAddress, UnixSocketChannel}
object Main {
val tmp = java.nio.file.Files.createTempDirectory("") + "/sock"
val addr = new UnixSocketAddress(tmp)
def main(args: Array[String]): Unit = {
new Thread(() => server()).start()
new Thread(() => client()).start()
}
def server() = {
val io = UnixServerSocketChannel.open()
io.socket().bind(addr)
val in = Channels.newInputStream(io.accept())
while(true) println(in.read())
}
def client() = {
Thread.sleep(1000)
val ioSocket = UnixSocketChannel.open(addr)
val in = Channels.newInputStream(ioSocket)
val out = Channels.newOutputStream(ioSocket)
// new Thread(() => println(in.read())).start()
Thread.sleep(100)
out.write("hello\n".getBytes)
}
}
If you uncomment the new Thread(() => println(in.read())).start(), the out.write("hello\n".getBytes) appears to never make it to the server thread, and nothing is printed. It appears blocking on the UnixSocketChannel's in.read() also disables out.write. Not sure if this is expected or a bug, but another socket library I tried https://github.com/sbt/ipcsocket appears to allow writes while some other thread is read-blocked
The following code sends
hello
from a client thread to a server thread over aUnixSocketChannel
, and the server prints out the character codes (pardon my Scala)If you uncomment the
new Thread(() => println(in.read())).start()
, theout.write("hello\n".getBytes)
appears to never make it to theserver
thread, and nothing is printed. It appears blocking on theUnixSocketChannel
'sin.read()
also disablesout.write
. Not sure if this is expected or a bug, but another socket library I tried https://github.com/sbt/ipcsocket appears to allow writes while some other thread is read-blocked