I need help exchanging data between two devices

Yamato

New Member
Licensed User
Longtime User
Hi everyone!
I was trying to exchange a string between two devices, but i can't.
I've readed the network library documentation and the example, but the example doesn't work on my device. (I supose the problem is that i don't know how to use it correctly) and i don't understand at all how the network library works... I'm :sign0104: at this things...

Here is the code i was trying to use :

B4X:
Sub Process_Globals
   Dim SKT As Socket
   Dim MySKT As Socket
   Dim SSK As ServerSocket
   Dim ASync As AsyncStreams
   Dim READ As InputStream
   Dim SEND As OutputStream
   Dim buffer() As Byte
End Sub
Sub Globals
   Dim txt_msg As EditText
   Dim cmd_send,cmd_conect As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
End Sub

Sub Activity_Resume
   If SSK.IsInitialized = False Then
      SSK.Initialize(8558,"SSK")
      SSK.Listen
   End If
End Sub

Sub SSK_NewConnection (Successful As Boolean, NewSocket As Socket)
   READ = NewSocket.InputStream
   SEND = NewSocket.OutputStream
   ASync.InitializePrefix(READ,True,SEND,"AStreams")
   SKT = NewSocket
   SSK.Listen
   Msgbox("ServerSocket conected","")
End Sub

Sub MySKT_Connected (Successful As Boolean)
   Msgbox("Socket conected","")
End Sub
Sub AStreams_NewData (Newbuffer() As Byte)
   READ.ReadBytes(Newbuffer,0,Newbuffer.Length)
   Msgbox(BytesToString(Newbuffer,0,Newbuffer.Length,"UTF-8"),"")
End Sub

Sub AStreams_Error
   ToastMessageShow("Error in ASync",False)
End Sub

Sub cmd_send_click
   buffer = txt_msg.Text.GetBytes("UTF-8")
   SEND.WriteBytes(buffer,0,buffer.Length)
End Sub

Sub cmd_conectar_click
   If MySKT.IsInitialized Then MySKT.Close
   MySKT.Initialize("MySKT")
   MySKT.Connect(SSK.GetMyIP,8558,0)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
   If UserClosed Then
      SKT.Close
      MySKT.Close
      SSK.Close
   End If
End Sub

Thanks in advance!
 

Yamato

New Member
Licensed User
Longtime User
No, it passes trough the Socket and Server Socket connection events, but when the send button is pressed, nothing happens (and it's not by the event name, this is a mistake when i was writing the post)

Anyway, if there is a simplier way to do this, i'd prefer to use it instead of use my code.

Here is the project.
 

Attachments

  • Conexion.zip
    347.1 KB · Views: 142
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use File - Export as zip when uploading projects.

I see that the server initializes AsyncStreams when the socket is connected. However the client does seem to initialize it (should be in Sub MySKT_Connected).

You should then use AStreams to send and receive messages. I recommend you to check the Bluetooth tutorial.
 
Upvote 0

Yamato

New Member
Licensed User
Longtime User
It finally works, the bluetooth example helped me a lot to understand the async streams.

I copied the chat activity code from the bluetooth example to make it work.
The other problem i had was the Host-Client connection. I was connecting the socket of one device on his own server.

Now at the start i ask the user if the device will be server or socket to make the connection properly.
At the server part is shown a label with the IP, then the client part has to put that IP in the dialog, if the IP is correct, the connection will start successfully and the chat activity will be opened.

Thanks Erel!

Here is the project working.
 

Attachments

  • Connection.zip
    8.2 KB · Views: 169
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…