Socket; send works, but cant get text back

byterbit

Member
Licensed User
Longtime User
Trying to send and receive text to a Java echo server on my cloud linux box. (this will be a key aspect of the app) ; the Java server and client work, test strings go both ways. After a week of work I can now send from an Android client to a Java server and see the server pick up the text in one test program, but unlike the Java client I cant get the Android app to hear the response. No firewalls. I’ve tried all the methods and test code I can find on the site.

If I understand the documentation there is no need, as with Java, to create a looping read structure, but it’s unclear as to how to pick up data from the socket having turned on “listen”; there are many ways to design a program here and without further guidance I could go around in circles for weeks missing success by a small bit of code.

Will (Socket1.InputStream) work by itself or do I need to work with ServerSocket ?

Here's some modified test code I picked up on the site and have worked with. The first example sends successfully to the Java EchoServer. If you can point me to some complete simple working code that listens and displays the echoed text it would help very much.


=================================
Sub Process_Globals
Dim Socket1 As Socket
End Sub

Sub Globals
Dim Button_ARM As Button
Dim Button_STAY As Button
Dim Button_AUTO As Button
Dim Button_OFF As Button
Dim Label_Received As Label
Dim Label_Sent As Label
Dim tr As TextReader
Dim tw As TextWriter
Dim sb As StringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket32")
Socket1.Initialize("Socket1")

Socket1.Connect("192.168.0.45" , 2222, 2000) 'My IP address goes here


End Sub

Sub Socket1_Connected (Successful As Boolean)
If Successful = False Then
Msgbox(LastException.Message, "Error connecting")
Return
End If
tr.Initialize(Socket1.InputStream)
tw.Initialize(Socket1.OutputStream)

tw.WriteLine("INFO")
Label_Sent.Text = "Sent INFO"
tw.Flush
sb.Initialize
sb.Append(tr.ReadLine)
Label_Received.Text = sb.ToString
'Socket1.Close
End Sub

Sub Button_ARM_Click
tw.WriteLine("O001")
tw.Flush
Label_Sent.Text = "Sent O001"
End Sub

Sub Button_STAY_Click
tw.WriteLine("O002")
tw.Flush
Label_Sent.Text = "Sent O002"
End Sub

Sub Button_OFF_Click
tw.WriteLine("O000")
tw.Flush
Label_Sent.Text = "Sent O000"
End Sub

====================================
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim ServerSocket1 As ServerSocket
Dim AStreams As AsyncStreams
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.

Dim Timer1 As Timer
Dim btnConnect As Button
Dim btnClose As Button
Dim btnTest As Button
Dim lvLog As ListView
Dim lblMyIP As Label
Dim label1 As Label
Dim label2 As Label
Dim label4 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket3")


'Todo: Change typeface of lvLog
' lvlog.Initialize("")
'lvlog.Color ="white"
' lvLog.SingleLineLayout.ItemHeight = 16dip
' lvLog.SingleLineLayout.Label.TextSize = 12
lblMyIP.Initialize("")
label1.text = ServerSocket1.GetMyIP

End Sub


Sub btnTest_Click
Dim Buffer() As Byte
msg = "Hello 2222"
Buffer = msg.GetBytes("UTF8")
Astreams.Write(Buffer)
' lvLog.AddSingleLine ("Sent " & msg)

label2.Text = "Sent " & msg
End Sub
Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45",2222,5000)
'lvLog.AddSingleLine ("Do Connect")
End Sub
Sub btnClose_Click
Socket1.Close
' lvLog.AddSingleLine ("Do Close")
label2.Text = "close"
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
'lvLog.AddSingleLine ("Is Connected")
label1.text = "connected"
End If
End Sub
Sub Activity_Resume
ServerSocket1.Listen
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Timer1.Enabled = False
Socket1.Close
ServerSocket1.Close 'stop listening
End If
End Sub

Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Socket1 = NewSocket
Timer1.Enabled = True
InputStream1 = Socket1.InputStream
OutputStream1 = Socket1.OutputStream
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerSocket1.Listen 'Continue listening to new incoming connections
label4.text = InputStream1
End Su
 

byterbit

Member
Licensed User
Longtime User
Send and receive.

I’m trying to develop an Android app that can both send and receive.
1) At a button push the phone will send it’s phone # and GPS position to the java server. 2) The server “hears” and sends a reply back that the Android will display. The reply will say “yes, we heard you”. I need to display this and other information back to the phone.
I am using code with server.socket in it because I can not understand from the code samples I have found how to get this functioning (and so I am grabbing anything plausible and experimenting with it ).

If you can point me to a working code sample that allows for sending and receiving I would be relived. I'm sure it's there, I just cant find it.

Many thanks
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Maybe I am overlooking it, but I don't see the sub...

B4X:
Sub AStreams_NewData (Buffer() As Byte)
   '...
End Sub

where you would read the incoming data.

Rolf
 
Upvote 0

byterbit

Member
Licensed User
Longtime User
I looked over the Bluetooth code, but I don’t see its relevance

I looked over the Bluetooth code, but I must be missing something; I don’t see its relevance to my attempt to send and receive via sockets.

This code below will successfully contact my Java server, but the server does not show sent text until I fully shut down the Android client(thank you Rbsoft; I added AStreams_NewData (Buffer() As Byte), and it may help after I get through this issue)
So, when I hit btnTest, I see nothing on the server, but when I exit the Android app all the “hello’s” appear.

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim ServerSocket1 As ServerSocket
Dim AStreams As AsyncStreams
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.

Dim Timer1 As Timer
Dim btnConnect As Button
Dim btnClose As Button
Dim btnTest As Button
Dim lvLog As ListView
Dim lblMyIP As Label
Dim label1 As Label
Dim label2 As Label
Dim label4 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket3")


'Todo: Change typeface of lvLog
' lvlog.Initialize("")
'lvlog.Color ="white"
' lvLog.SingleLineLayout.ItemHeight = 16dip
' lvLog.SingleLineLayout.Label.TextSize = 12
lblMyIP.Initialize("")
label1.text = ServerSocket1.GetMyIP

End Sub


Sub btnTest_Click
Dim Buffer() As Byte
msg = "Hello 2222"
Buffer = msg.GetBytes("UTF8")
Astreams.Write(Buffer)
' lvLog.AddSingleLine ("Sent " & msg)

label2.Text = "Sent " & msg
End Sub
''''''''''''''''
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label2.Text = "BACK !!!!! " & msg
End Sub


''''''''''


Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45",2222,5000)
'lvLog.AddSingleLine ("Do Connect")
End Sub
Sub btnClose_Click
Socket1.Close
' lvLog.AddSingleLine ("Do Close")
label2.Text = "close"
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
'lvLog.AddSingleLine ("Is Connected")
label1.text = "connected"
End If
End Sub
Sub Activity_Resume
ServerSocket1.Listen
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Timer1.Enabled = False
Socket1.Close
ServerSocket1.Close 'stop listening
End If
End Sub

Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Socket1 = NewSocket
Timer1.Enabled = True
InputStream1 = Socket1.InputStream
OutputStream1 = Socket1.OutputStream
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerSocket1.Listen 'Continue listening to new incoming connections
label4.text = InputStream1
End Sub
=======================
This code, taken verbatim from the site, appears not to function; that is my java server shows no indication of being contacted.

Sub process_globals
Dim UDPSocket1 As UDPSocket
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
UDPSocket1.Initialize("UDP", 0, 8000)
End If
Dim Packet As UDPPacket
Dim data() As Byte
data = "Hello from Android".GetBytes("UTF8")
Packet.Initialize(data, "192.168.0.45", 2222)
UDPSocket1.Send(Packet)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Msgbox("Message received: " & msg, "")
End Sub
 
Upvote 0

byterbit

Member
Licensed User
Longtime User
This is not my code, this is code taken from other frustrated people on this site

This is not my code, this is code taken from other frustrated people on this site that I was experimenting with. I hope you can point me to a simple complete working example of two way socket based communication.
I want to send text to an IP address on a port and receive a reply to the Android app. If this can be done with your tool, very good. If it can not be done it would be good of you to inform me of this such that I stop wasting my time.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Here is a basic EchoClient project. It should work with your java server too. Just change IP adress and port #.

I have added a small VB Echo server for testing. The source code for the server you can find here:
Basic TCP/IP Multi-Client Echo Server by Ninnghizidha

As I mentioned before the only thing in your code that seems to be missing is the Sub TcpStreams_NewData.

This would be easier to detect if you would have used code tags in your message as Erel has mentioned twice already. Makes code in post much easier to read. You need to click the "Go Advanced" option to use them when writing in the Forum.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   'TcpIp communication
   Dim TcpStreams          As AsyncStreams 
   Dim Socket1          As Socket 
   Dim ServerSocket1       As ServerSocket 
   Dim MyIP             As String 
   Dim ServerIp          As String 
   Dim port             As Int
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.

   Dim lblMyIp As Label
   Dim txtServerIp As EditText
   Dim txtPort As EditText
   Dim btnConnect As Button
   Dim txtMsg As EditText
   Dim btnSendMsg As Button
   Dim lblRecMsg As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout(1)
End Sub

Sub Activity_Resume
   ServerSocket1.Initialize(0, "")
   MyIP = ServerSocket1.GetMyIP 
   lblMyIp.Text = MyIP
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnConnect_Click
   Socket1.Initialize("Socket1")
   ServerIp = txtServerIp.Text
   port = txtPort.Text 
   Socket1.Connect(ServerIp,port,5000)
   SendData("Welcome")
End Sub

Sub btnSendMsg_Click
   SendData(txtMsg.Text)
End Sub


Sub Socket1_Connected(Connected As Boolean)As Boolean 
   If Connected = True Then
      ToastMessageShow("Connected",True)
      TcpStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"tcpStreams")
      InputStream1 = Socket1.InputStream
      OutputStream1 = Socket1.OutputStream
   Else
      'ToastMessageShow("Server not available",True)
   End If
End Sub

Sub SendData(Msg As String)
   Dim Buffer() As Byte 
   
   Msg = txtMsg.Text  
   If Socket1.Connected = True Then
      Try
         Buffer = Msg.GetBytes("UTF8")
         TcpStreams.Write(Buffer)
      Catch
         Log("TcpStreams.Write Error")
      End Try
   Else
      Log("Connection lost")
   End If
End Sub

Sub TcpStreams_NewData (Buffer() As Byte)
    Dim Msg As String
   
    Msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   lblRecMsg.Text = Msg
End Sub
Rolf
 

Attachments

  • EchoClient.zip
    13.5 KB · Views: 671
Last edited:
Upvote 0

Osi

Member
Licensed User
Longtime User
It sounds like, if you want to receive and send pure text, you would need a string terminator such as a CrLF or some other character/combo.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

KY Leng

Member
Licensed User
Longtime User
Thank you very much for posting the sample code on how to use Socket and AStream to communicate with Server by specifying the Port and IP.

However, do someone know how to write B4A code to make our phone or embedded PC that run on Android to be a webserver? Just sample code that can read text from a text file and display it on the web and when user press some thing like button, then write back to another text file. Both text file are located on the phone or embedded PC.

I try many day searching such simple example on the forum but cannot find.

Thank you in advance, and hope to get a light soon.
 
Upvote 0

byterbit

Member
Licensed User
Longtime User
Thanks

Thanks very much.
My java server sees the connection (resolving the address with m_clientSocket.getInetAddress().getHostName()) ) but does not display sent text (much less a reply) until I exit the android simulator. Simply stopping the test in the emulator and returning to B4A does nothing; I must fully shut the emulator down as a running program on my computer. At this point my java server dumps all text I've sent.
Again, the Java client I have works fine, both ways.
Late here, and I'm not doing work for my clients as I experiment in this, so there's a good deal of testing I cant get to tonight(though at the moment I cant imagine where to start) . I'll try your server as well.
Very kind of you to help like this.
 
Upvote 0

byterbit

Member
Licensed User
Longtime User
Thanks

Thanks very much.
My java server sees the connection (resolving the address with m_clientSocket.getInetAddress().getHostName()) ) but does not display sent text (much less a reply) until I exit the android simulator. Simply stopping the test in the emulator and returning to B4A does nothing; I must fully shut the emulator down as a running program on my computer. At this point my java server dumps all text I've sent.
Again, the Java client I have works fine, both ways.
Late here, and I'm not doing work for my clients as I experiment in this, so there's a good deal of testing I cant get to tonight(though at the moment I cant imagine where to start) . I'll try your server as well.
Very kind of you to help like this.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Sounds more to me like a conflict between your Java server and the (Java based emulator). Did you try with a real device?

Rolf

Sent from my GT-P1000 using Tapatalk 2
 
Upvote 0

byterbit

Member
Licensed User
Longtime User
Same issue

Rbsoft : Same issue ! Tried it with my Android (OS V 2.2.1, a LG model using Virgin Mobile) and my cloud server running the java echo server and I see more or less the same problem; contact is made but no text is received (much less returned) ; I need to study this more ( tried TcpStreams.Close but it made no difference.)
Erel, I’ve looked at the AsyncStreams Tutorial at least 5 times and ran it at least once and I still don’t see it’s relevance to what I’m trying to do;I’m missing something.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Since I do not know your Java Echo Server - does it use a certain protocol? Any special start sequence or end delimiters? If so then you might just have to adapt the code in the echo client. (Usually an echo server does not need this, it just echoes back whatever you sent to it.)

Also what text format does your Java Echo Server expect? UTF8, ASCII, or anything else?

Rolf
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Thank you very much for posting the sample code on how to use Socket and AStream to communicate with Server by specifying the Port and IP.

However, do someone know how to write B4A code to make our phone or embedded PC that run on Android to be a webserver? Just sample code that can read text from a text file and display it on the web and when user press some thing like button, then write back to another text file. Both text file are located on the phone or embedded PC.

I try many day searching such simple example on the forum but cannot find.

Thank you in advance, and hope to get a light soon.

Yes, you could make a server app with it. You could use this in a LAN. But be aware that it might not necessarily work with a mobile connection. In many countries (like in Germany) the providers might block the access from outside if your app acts as a server.

Rolf
 
Upvote 0
Top