[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

sample[jx8-android] tcpClient

Sample to send data to TCP Socket server (e.g. server running in desktop or another jx8 instance)

Java implementation

Uploaded Image: alert.gifNote that the following implementation will fail instantiating the socket server, because it will be running MAIN thread.

So, please read the next implementation. The NodeJS based implementation is shorter and efficient.

"[Java] Try to send a simple packet #info request to a server in the local network. Note that if this method fails to create Socket it can be related with Threading and/or permission issues"
| address socket output input line |
self print: 'About to create socket'.
address := InetAddress getByName: ''.
socket := Socket address: address port: 8088.

self print: 'About to create output'.
output := PrintWriter on: socket outputStream buffered.
self print: 'About to send data to server'.
output println: 'info

self print: 'Data sent to server'.
output flush; close.

self print: 'About to create input'.
input := socket inputStream buffered.
self print: 'About to read response from server'.
[	line := input readLine.
	line notNil
] whileTrue: [
	self print: line
input close.
socket close.
self print: #done

NodeJS based implementation

"[NodeJS] Try to send a simple packet #info request to a server in the local network."
| client |
client := NodeJS net
	connect: 8088
	with: ''
	with: [
		self print: #connected.
		client write: 'info

'	].
client on: #data do: [:data|
	self print: data.
	client destroy
client on: #close do: [
	self print: #closed
client on: #error do: [:ex|
	self print: #error ,$= ,ex