Hi
This was an exciting week!
For the first time I was able to connect my mobile device with the PC using the
NetCom framework (
http://masm32.com/board/index.php?topic=7757.0)
As I mentioned in the database post (
http://masm32.com/board/index.php?topic=8352.0), I have coded an app using Kotlin, a kind of relative new, improved java dialect. The code is very easy to understand and the learning curve of this language is very easy. There are countless examples on the net that you only have to put together after learning the basics of the language.
There are a few small steps to take to enable communication. In the local network, the port forwarding and the firewall setup must be adjusted so that the messages can be forwarded to our server.
The server code is basically the code from "ObjAsm\examples\X\NetCom\Server" with a slight change of the
ServerProtocol.inc. When a button on the phone is pressed, the client sends a zero terminated UTF8 message and the server responds with a "modified UTF8" string. In this implementation, a simple "OK" is returned and displayed at the bottom of the phone screen.
Now that the client/server communication and the database are working, a suitable protocol must be established for the entire project.
There are so many uses of this framework to explore, that I don’t know where to start…
This is the code of the Kotlin client class:
class NetComClient(private val serverAddress: String, private val serverPort:Int) {
lateinit var clientsocket: Socket
lateinit var dataInputStream:DataInputStream
var connected = FALSE
var information = ""
init {
try {
clientsocket = Socket(serverAddress, serverPort)
dataInputStream = DataInputStream(clientsocket.getInputStream())
connected = TRUE
} catch (ex: Exception) {
information = when (ex) {
is ProtocolException -> "Bad Protocol"
is PortUnreachableException -> "Port unreachable"
is NoRouteToHostException -> "No route to host"
is ConnectException -> "Connection failed"
is UnknownHostException -> "Unknown Host"
is BindException -> "Socket binding failed"
is IOException -> "IO failed"
is SocketException -> "Socket failed"
else -> throw ex
}
}
}
fun send(message: String) {
if (connected != FALSE) {
try {
clientsocket.outputStream.write("$message\u0000".toByteArray())
} catch (ex: Exception) {
information = when (ex) {
is ProtocolException -> "Bad Protocol"
is PortUnreachableException -> "Port unreachable"
is NoRouteToHostException -> "No route to host"
is ConnectException -> "Connection failed"
is UnknownHostException -> "Unknown Host"
is BindException -> "Socket binding failed"
is IOException -> "IO failed"
is SocketException -> "Socket failed"
else -> throw ex
}
}
}
}
fun read(): String {
try {
var response: String
response = dataInputStream.readUTF()
return response
} catch (ex: Exception) {
information = when (ex) {
is ProtocolException -> "Bad Protocol"
is PortUnreachableException -> "Port unreachable"
is NoRouteToHostException -> "No route to host"
is ConnectException -> "Connection failed"
is UnknownHostException -> "Unknown Host"
is BindException -> "Socket binding failed"
is IOException -> "IO failed"
is SocketException -> "Socket failed"
is EOFException -> "EOF reached"
else -> throw ex
}
return information
}
}
fun disconnect() {
if (connected != FALSE) {
try {
clientsocket.close()
connected = FALSE
} catch (ex: Exception) {
information = when (ex) {
is ProtocolException -> "Bad Protocol"
is PortUnreachableException -> "Port unreachable"
is NoRouteToHostException -> "No route to host"
is ConnectException -> "Connection failed"
is UnknownHostException -> "Unknown Host"
is BindException -> "Socket binding failed"
is IOException -> "IO failed"
is SocketException -> "Socket failed"
else -> throw ex
}
}
}
}
}
Biterider