Android Question localsocket howto

ajk

Active Member
Licensed User
Longtime User
I need to access localsocket. How to start with this? Working Java code example attached below


B4X:
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ClientSocket {
    public static final String SOCKET_NAME = "com.connect.pc4";
    public static LocalSocket socket=null;
    private InputStream InpStrim;              
    private OutputStream OutStrim;              

    public boolean Connect() {
            try {
                socket = new LocalSocket();
                socket.connect(new LocalSocketAddress(SOCKET_NAME));
                socket.setReceiveBufferSize(327680);
                socket.setSendBufferSize(327680);
                socket.setSoTimeout(1000);
                InpStrim = socket.getInputStream();
                OutStrim = socket.getOutputStream();
            }
            } catch (IOException e) {
                return false;
            }
                return true
    }

    public boolean Disconnect() {
        boolean rc = true;
        try {
            InpStrim.close();
            OutStrim.close();
            socket.close();
        } catch (IOException e) {
            rc = false;
        } catch (Exception e) {
            rc = false;
        }
        return rc;
    }
}
 

ajk

Active Member
Licensed User
Longtime User
Moved to the questions forum. You should use Socket from the Network library.

But how to connect to LocalSocketAddress using Network library? No port address specified in such case.

Question still important - I have 6 devices to connect with this method (different addresses, different protocols)...
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
ok but I have to connet like in example with different local addresses
B4X:
public static final String SOCKET_NAME = "com.connect.pc4";
socket.connect(new LocalSocketAddress(SOCKET_NAME));

to "com.connect.pc4", "com.connect.pc5", "com.connect.pc6" and so on on single device
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
It's a POS with implemented hardware (drawer, fiscal printer, com ports, scanner & other). I have to establish communication with these devices.

I have made connection with in-lined code. But it seems to me that special library could be useful so simplify connections directly from B4A code.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code to create a local socket:
B4X:
Sub OpenLocalSocket (LocalSocketName As String) As AsyncStreams
   Dim localsocket As JavaObject
   Dim address As JavaObject
   address.InitializeNewInstance("android.net.LocalSocketAddress", Array(LocalSocketName))
   localsocket.InitializeNewInstance("android.net.LocalSocket", Null)
   localsocket.RunMethod("connect", Array(address))
   Dim astream As AsyncStreams
   astream.Initialize(localsocket.RunMethod("getInputStream", Null), _
     localsocket.RunMethod("getOutputStream", Null), "astream")
   Return astream
End Sub

You will probably need to add some permissions and maybe add the NetworkOnMainThread workaround.

Put this code in a class and create a class instance for each connection.
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
I have bounded in-class created AsyncStream with AStreams with keyboard
like
B4X:
    AStreams=LOCALsocket.OpenLocalSocket("com.connect.pc6")
    LogColor(AStreams,Colors.DarkGray)

I have
anywheresoftware.b4a.randomaccessfile.AsyncStreams@2d80c3cb

I have AStreams.IsInitialized=True
in log but

but no

AStreams_NewData is fired

AStreams.StreamTotal
AStreams.StreamReceived

are also equal to 0
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
Yes I see but is returned as AsyncStreams?
I think You suggest to use it in AsyncStreamsObject code like structure?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think You suggest to use it in AsyncStreamsObject code like structure?
No. (You should never use AsyncStreamsObject as it was replaced by B4XSerializator)

Yes I see but is returned as AsyncStreams?
True. If you go over the code you will see that I set the event name to astream. So you need to write:
B4X:
Sub astream_NewData(Buffer() As Byte)
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
OK ready - working
I have used code from #10 post to get localsocket and then

B4X:
Sub AStream_NewData (Buffer() As Byte)  'KBD
    '    LogColor("xxx",Colors.Blue)
    CallSub2(Main, "KbdNewData", Buffer)
End Sub

Thank You for help
 
Upvote 0
Top