aboutsummaryrefslogtreecommitdiff
path: root/examples/nativesocketsclient.nim
diff options
context:
space:
mode:
authorElectric-Blue <Electric-Blue@users.noreply.github.com>2016-01-26 02:03:48 +0300
committerElectric-Blue <Electric-Blue@users.noreply.github.com>2016-01-26 02:03:48 +0300
commit0c4a1cc1c2083a871562364de5df4f9d88ec321e (patch)
treeddb8428845b663c078359eda4ec81bdcc157414f /examples/nativesocketsclient.nim
parent27a7a2a18a6db8b67cbe0e86c37cb174de2a1295 (diff)
downloadNimBluez-0c4a1cc1c2083a871562364de5df4f9d88ec321e.tar.gz
NimBluez-0c4a1cc1c2083a871562364de5df4f9d88ec321e.zip
First version.
Diffstat (limited to 'examples/nativesocketsclient.nim')
-rw-r--r--examples/nativesocketsclient.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/nativesocketsclient.nim b/examples/nativesocketsclient.nim
new file mode 100644
index 0000000..80ef026
--- /dev/null
+++ b/examples/nativesocketsclient.nim
@@ -0,0 +1,44 @@
+import os
+import ../nimbluez/bluetoothnativesockets
+
+let
+ serverPort = RfcommPort(15)
+ serverAddress =
+ "provide_your_server_address_string_here"
+ # "00:02:72:0F:5C:87"
+ # "AC:7B:A1:55:E6:4A"
+
+var clientSocket = newBluetoothNativeSocket(SOCK_STREAM, BTPROTO_RFCOMM)
+if clientSocket == InvalidSocket:
+ raiseOSError(osLastError())
+try:
+ echo "Client socket created: ", repr(clientSocket)
+
+ var name = getRfcommAddr(serverPort, serverAddress)
+ if connect(clientSocket,
+ cast[ptr SockAddr](addr(name)),
+ sizeof(name).SockLen) < 0'i32:
+ raiseOSError(osLastError())
+ echo "Connection established."
+
+ var message = "What time you got?"
+
+ echo "Sending message:"
+ echo " ", message
+ let sentLen = clientSocket.send(cstring(message), cint(message.len), cint(0))
+ if sentLen < 0'i32:
+ raiseOSError(osLastError())
+ echo "Characters sent: ", sentLen
+
+ echo "Receiving message:"
+ message = ""
+ message.setLen(1000)
+ let recvLen = recv(clientSocket, cstring(message), cint(message.len), cint(0))
+ if recvLen < 0'i32:
+ raiseOSError(osLastError())
+ message.setLen(recvLen)
+ echo " ", message
+ echo "Characters received: ", recvLen
+finally:
+ clientSocket.close()
+ echo "Client socket closed."