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:
B4J Client code:
How can i read data from server without closing the stream in b4j?
Need i modify client or server code?
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?