Hello, I've just wrote a simple program for the communication between a Pc and an Android Phone.
The PC server side is wrote with B4J and the code is the following:
The code of the phone side, wrote in B4A is this:
I can't understand why the devices don't communicate....maybe the firewall? Or I have wrote something wrong?
The PC server side is wrote with B4J and the code is the following:
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim AStreams As AsyncStreams
Dim Server As ServerSocket
Dim Socket1 As Socket
Private Label1 As Label
Private EditText1 As TextField
Private Label2 As Label
Private TextField2 As TextField
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.RootPane.LoadLayout("Main")
MainForm.Show
Server.Initialize(5500, "Server")
Server.Listen
Log("MyIp = " & Server.GetMyIP)
TextField2.Text = Server.GetMyIP
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Label1.Text = "Connected"
Socket1 = NewSocket
'Can only use prefix mode if both sides of the connection implement the prefix protocol!!!
AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
Else
Label1.Text = "Problems"
End If
Server.Listen
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
EditText1.Text = msg
Log(msg)
End Sub
Sub AStreams_Error
Label1.Text = "Errori"
Log("AStreams_Error")
End Sub
Sub AStreams_Terminated
Log("AStreams_Terminated")
End Sub
'press on the Done button to send text
Sub EditText1_EnterPressed
If AStreams.IsInitialized = False Then Return
If EditText1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = EditText1.Text.GetBytes("UTF8")
AStreams.Write(buffer)
EditText1.SelectAll
Log("Sending: " & EditText1.Text)
End If
End Sub
Sub Activity_Pause(UserClosed As Boolean)
If UserClosed Then
Log("closing")
AStreams.Close
Socket1.Close
End If
End Sub
The code of the phone side, wrote in B4A is this:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim AStreams As AsyncStreams
Dim Server As ServerSocket
Dim Socket1 As Socket
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private EditText1 As EditText
Private EditText2 As EditText
Private Button1 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Activity.LoadLayout("Main")
If FirstTime Then
Server.Initialize(5500, "Server")
Server.Listen
EditText2.text = Server.GetMyIP
End If
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.96","5500",0)
If Socket1.Connected = True Then
Button1.Text = "OK"
Else
Button1.Text = "KO"
End If
' EditText1.ForceDoneButton = True
' Activity.AddView(EditText1, 10dip, 10dip, 300dip, 60dip)
End Sub
Sub Activity_Resume
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
ToastMessageShow("Connected", False)
Socket1 = NewSocket
'Can only use prefix mode if both sides of the connection implement the prefix protocol!!!
AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
Else
ToastMessageShow(LastException.Message, True)
End If
Server.Listen
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
ToastMessageShow(msg, False)
Log(msg)
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
Log("AStreams_Error")
End Sub
Sub AStreams_Terminated
Log("AStreams_Terminated")
End Sub
'press on the Done button to send text
Sub Activity_Pause(UserClosed As Boolean)
If UserClosed Then
Log("closing")
AStreams.Close
Socket1.Close
End If
End Sub
Sub Button1_Click
If AStreams.IsInitialized = False Then Return
If EditText1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = EditText1.Text.GetBytes("UTF8")
AStreams.Write(buffer)
EditText1.SelectAll
Log("Sending: " & EditText1.Text)
End If
End Sub
I can't understand why the devices don't communicate....maybe the firewall? Or I have wrote something wrong?