B4J Question Confused on TCP Server libraries

QtechLab

Active Member
Licensed User
Longtime User
Hello everyone,

Today i've needed to create a TCP socket server, i've chosen to use B4J instead of VB.NET.

I searched many "How to...TCP..." threads on the boards but i'm very confused. This is the first time that i need to use server side socket.

I tried to write a little server with jNetwork library, i manage new connection and i save the client socket in a list. I have to read and write data from/to specific clients one by one, how can i do that?

I see that many people use jServer instead jNetwork.. I have to exchange packets through many many clients to the server, some clients are mobile devices and some other are security control panel (something like the raspberryPI).

Can someone explain me the differences between use jServer instead of jNetwork? jServer create a web server, do you suggest me to use that? i'm really confused.

Thanks in advance.

Best regards,
Marcello
 

Roycefer

Well-Known Member
Licensed User
Longtime User
If your clients can make HTTP GET requests, then jServer is definitely the way to go. If they can't, you'll probably have to use jNetwork and AsyncStreams. From what it sounds like (mobile devices and Ras Pis), your clients can make HTTP GET requests.

This will get you started with jServer: http://b4x.com/android/forum/threads/webapp-web-apps-overview.39811/

This will get you started with AsyncStreams (which you would almost certainly use to manage jNetwork): https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/#content
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
I cannot change the security control panel data stream functions, i think i won't use the jServer because i can't implement HTTP requests!

I'm thinking about something like the image.

1) The device send data to the server;
2) The server find the coupled control panel and send that data to the control panel;
3) The control panel analyze the data and send a response to the server;
4) The server send that data (the response from the control panel) to the coupled mobile device

The mobile devices and the control panels are coupled in a database table.

This procedure is drawn with the gray arrows.

The yellow arrow is the actual transmission.
transmission.jpg
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Are you writing the app that's running on the mobile devices and with which the server will be communicating? If so, you might use jServer to communicate with the mobile devices and jNetwork to communicate with the control panel.
 
Upvote 0

QtechLab

Active Member
Licensed User
Longtime User
The app and the control panel software are ready, i miss only the server...

I solved my trouble by writing a Class that manage the clients

B4X:
'Class module
Sub Class_Globals
   
   Private fx As JFX
   Private Async As AsyncStreams
   Private mSocket As Socket
   Private mID As Int
   
   Private destination As Socket
   Private destAsync As AsyncStreams
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(sock As Socket, id As Int, dest As Socket)

   mSocket.Initialize("socket")
   mSocket = sock
   Async.Initialize(mSocket.InputStream, mSocket.OutputStream, "AsyncStream")
   mID = id
   
   If dest <> Null Then
     destination.Initialize("Destination")
     destination  = dest
     destAsync.Initialize(Null, destination.OutputStream, "DestinationStream")
   End If
   
End Sub

public Sub GetSocket As Socket
   Return mSocket
End Sub


Sub AsyncStream_NewData(Buffer() As Byte)
   
   Dim tmpStr As String
   tmpStr = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
   
   If destAsync.IsInitialized Then
     SocketManager.RaiseEvent(mSocket, tmpStr & " -> " & destination.RemoteAddress)
     destAsync.Write(Buffer)
   Else
     SocketManager.RaiseEvent(mSocket, tmpStr & " -> null")
   End If
   
End Sub
 
Upvote 0
Top