B4J Question [SOLVED] Java server socket and B4J socket client

PatrikCavina

Active Member
Licensed User
Hi everyone,
I'm trying to create a simple Java server socket that prints the text sent by the client socket b4j.
The client connects to the server, but the data from the b4j client is never read by the server, unless the client calls the "SendAllAndClose" method.

Java Server code:

B4X:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer{
    public static void main(String[] args) throws IOException {
        
        int portNumber = 6009;
        
        try {
            ServerSocket ss = new ServerSocket(portNumber);
            System.out.println("Waiting for connection...");
            Socket inboundSocket = ss.accept();

            System.out.println("inputstream read");
            DataInputStream is = new DataInputStream(inboundSocket.getInputStream());

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          
            String fromStream = reader.readLine();
            System.out.println(fromStream);

            System.out.println("bufferreader read");
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
B4J Client code:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Start
End Sub

Sub Start
    Dim sock As Socket
    sock.Initialize("Socket")
    sock.Connect("192.168.1.71", 6009, 0)
    Wait For Socket_Connected (Successful As Boolean)
    If Successful Then
        Log("Connection successful")
        Dim astream As AsyncStreams
        astream.Initialize(sock.InputStream, sock.OutputStream, "Stream")
        Sleep(5000)
        Log("Send data")
        astream.Write("I come from b4j".GetBytes("UTF8"))
        astream.SendAllAndClose '<------- If i comment this line, text are not sent
    End If
End Sub
How can i read data from server without closing the stream in b4j?
Need i modify client or server code?
 

Harris

Expert
Licensed User
Because with B4J I already know how to do it, and I'd like to do it with java too
Just an observation... Why beat yourself up? Answer: We all understand - because you can.

masochist. If you call someone a masochist, you either mean that they take pleasure in pain, or — perhaps more commonly — that they just seem to.

:(
 

PatrikCavina

Active Member
Licensed User
Mine is a pure thirst for knowledge.
I like to learn, understand and test myself.
If you want to call it masochism, then I am a masochist.

Meanwhile, I thank @Daestrum for the advice he gave me that made me solve the problem.
Below the solution to my problem:

As i'm using reader.readline i must append CRLF to the message of b4j client, so the server knows it has a complete line to read.
So b4j code became:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Start
End Sub

Sub Start
    Dim sock As Socket
    sock.Initialize("Socket")
    sock.Connect("192.168.1.71", 6009, 0)
    Wait For Socket_Connected (Successful As Boolean)
    If Successful Then
        Log("Connection successful")
        Dim astream As AsyncStreams
        astream.Initialize(sock.InputStream, sock.OutputStream, "Stream")
        Sleep(5000)
        Log("Send data")
        Dim msg As String = "I come from b4j" & CRLF '<------------ I append CRLF to the string
        astream.Write(msg.GetBytes("UTF8")) '<------- The Message is received by the server
    End If
End Sub
 
Top