TCP/IP communication

michelg

Member
Licensed User
Longtime User
Hi friends,

I have a server program written in VB6 in my laptop. I used the Winsock object that comes with VB6 (tcp/ip). The server program has a Textbox with a number that keep on changing. I want to use my android phone as a client to request the number from the server when I want to. Then I want the server to be able to contact the Android phone when certain very specific number is showing up in the Textbox.

I'm new to Android and I do not know how to start. I understand VB6 fairly.

I'm using Basic4android.

Please, help anything is appreciated.
 

michelg

Member
Licensed User
Longtime User
Thank you for your quick reply. I will check the tutorials out and see what I can get from them.

Once again thanks
 
Upvote 0

michelg

Member
Licensed User
Longtime User
Network tutorial

Thank you for your quick reply. I will check the tutorials out and see what I can get from them.

Once again thanks

Yes, you were right. I look at the Network Tutorial and it was very helpful. However, still need a lot of help. Forgive my ignorance.

I have a few questions for you. you can either help me directly or refer me to the appropriate tutorial.

How can I change my screen-name and password?

Can you tell me how to download B4a apps to the android device?

These are beginners' questions. But, this is what I consider myself as.

Honesly, I appreciate your guidance and support.

Thank you!
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
Not sure exactly what you are asking for.

If you want the server to contact your android phone you will have to have a program running on the phone listening on a specific port - I believe those are called server sockets. When you get the new connection event then you can pull the information and process it.

In VB6 you would have a socket listening and on connection an event is triggered. This is the same here.
 
Upvote 0

michelg

Member
Licensed User
Longtime User
TCP/IP request

TimWil,

I appreciate you taking the time to respond to my post. I thank you a lot.


I already have the VB6 Server listening and waiting for connections. The VB6 Server is working fine. I tested it with other VBclients.

I just need the Android device to request a connection from the Server.
Upon connection the VBServer will send a number to the Android device.


The number the VBServer will send is in a texbox on the Server's program.
The Android device will receive(read) the number on a texbox on its screen.

This is what I want.
1. A button on the Adroid that requests the connection(cmdConnect)
Don't worry about the server; it is already listening.

2. A textbox on the device to see the number that the the Server sends.(txtReader).


I'm working on it. If it doesn't work, I will send you the code, and you can guide me.
this must be very simple for you. But, for me it still presents a challenge.

Again, thank you for your time and you kindness.
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
Here is a sample with socket programming - I get information from the user, open a port on the server, send the information and display the response

the server I am using is actually built in VB6 using the native VB6 socket component
 

Attachments

  • SocketSample.zip
    5 KB · Views: 1,486
Last edited:
Upvote 0

michelg

Member
Licensed User
Longtime User
TCPClient

Guys,

Thank you very much for your sincere dedication to help others.
You guys are incredible. I do not know how much to thank you.

Timwil,

I did not download the zip file on your post because I thought it is the same one that rbsoft posted on another thread. I removed everything that I do not need from rbsoft's client example, and it works for me.

The Part that I need now is how to send notification to the client android device to alert the user that the number is above a certain range. Something like an email notification or a ringing of the Android phone. Any idea will be appreciated.

Thanks a lot guys!
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
I use something like the following for notifications. This was done from a Service Module

B4X:
Sub Process_Globals
   Dim Notify As Notification
End Sub

Sub Service_Create
   Notify.Initialize
   Notify.Notify(1)
   Notify.Sound = True
   Notify.Vibrate = True
   Notify.Icon = "icon"
End Sub

Sub NotifyMe(Message as String)
   Notify.SetInfo(Message , Main)
   Service.StartForeground(1, Notify)
End Sub
 
Last edited:
Upvote 0

michelg

Member
Licensed User
Longtime User
I use something like the following for notifications. This was done from a Service Module

B4X:
Sub Process_Globals
   Dim Notify As Notification
End Sub

Sub Service_Create
   Notify.Initialize
   Notify.Notify(1)
   Notify.Sound = True
   Notify.Vibrate = True
   Notify.Icon = "icon"
End Sub

Sub NotifyMe(Message as String)
   Notify.SetInfo(Message , Main)
   Service.StartForeground(1, Notify)
End Sub


Dear Friend,

I took a very good look at the code you posted for me. It is code for the client device.

What about the VB6server what should the code looks like on the Server's side?

Remember:

Step1. The user cliks a button on the Adroidphone to connect to the VB6Server
Step2. The VB6Server accepts the connection and automatically sends a number to the Androidphone(txtSentText.SendData).

Step3. If the User is not on the phone(not connected), alert him with a notification, sound or email.

My problem is Step3.
How do I code Sttep3 on the Server side?

In plain English, this is what Step3 should say: If the user is not connected, notify him.

In VB6 code, this is what Part3 the Server code should look like:
If sockMain(i).State <> sckConnected Then
???notify him?????

The small section with the interrogation signs is what I want in VB6 coding.

Then the Client side code you posted for me will entter in action.(Client)

I may confuse you but focus only on Step3 in VB6 coding.

Thank you very much. I appreciate you help.

michelg
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Step3. If the User is not on the phone(not connected), alert him with a notification, sound or email.

My problem is Step3.
How do I code Sttep3 on the Server side?

In plain English, this is what Step3 should say: If the user is not connected, notify him.

In VB6 code, this is what Part3 the Server code should look like:
If sockMain(i).State <> sckConnected Then
???notify him?????

The small section with the interrogation signs is what I want in VB6 coding.

Then the Client side code you posted for me will entter in action.(Client)

I may confuse you but focus only on Step3 in VB6 coding.

This would be more likely a thread for a VB6 forum.

I don't fully understand your scenario. Are you looking for a solution if the connection got lost somewhere on the way? If so then your server will not be able to reach your app anymore. Your Android app should send a message (like a ping) in regular intervals to the server to test if the connection still exists, and your sever should send a short answer.

If the connection gets lost then your app should try to reconnect to the server. So your need a little protocol to keep the connection going.

If you want to initiate a connection from the server your could use for example an SMS interceptor in your app. There are examples for this on the forum. But notice that your app must be up and running or have a running service to catch the SMS. And writing a SMS sender in VB6 is not a simple task!

Rolf
 
Last edited:
Upvote 0

michelg

Member
Licensed User
Longtime User
This would be more likely a thread for a VB6 forum.

I don't fully understand your scenario. Are you looking for a solution if the connection got lost somewhere on the way? If so then your server will not be able to reach your app anymore. Your Android app should send a message (like a ping) in regular intervals to the server to test if the connection still exists, and your sever should send a short answer.

---------------------------------------------
--------------------------------------------


Yes, this shoul be for a VB forum. However, I saw the code you posted, and I thought you may be able to help. I used part of your code to initiate the contact with the server. You guys are wonderful!


This is the scenario:

The user initiates a contact with the server. the server sends a number.

The Android app is not on all the time. The user must open it to connect to the server.

when the Android app is not on, the Server wants the attention of the user. Therefore, it sends a notification to the phone.

when the user heard the notification, he will open the app and connect to the server. that is all.

I need the notification in VBcode

I appreciate your concern and your
willingness to help me.
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
You would have to code the android application so that it checks the server every set period. You could set up a service module that starts itself up every 30 seconds (or time that you specify), contacts the server and if the required response is correct from the server then do the notification.

You would not like to imagine what you would have to do to have the server contact the android device!
 
Last edited:
Upvote 0

michelg

Member
Licensed User
Longtime User
You would have to code the android application so that it checks the server every set period. You could set up a service module that starts itself up every 30 seconds (or time that you specify), contacts the server and if the required response is correct from the server then do the notification.

You would not like to imagine what you would have to do to have the server contact the android device!

Timwil,

This is very good advice. However, this is not what I want to do.

Is there a way I can have the server rings the phone. or sends an email. I would be happy if you can guide me in that direction. Forgive my ignorance; when I was using VB6 there was no Android, and I still don't understand the Android system yet.

Thanks a lot. I appreciate your willingness to help me.
 
Last edited:
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
You would not like to imagine what you would have to do to have the server contact the android device!

timwil is absolutely correct here.

It would not be so difficult if you are working only in a WLAN. Then you could use a ServerSocket. It still would mean that your app needs a service component that keeps running in the foreground.

If you are using the mobile network:

Using SMS you could use the SMSInterceptor, of course a service component that keeps running in the foreground is needed. More information here: http://www.b4x.com/forum/additional...-messages-without-notification.html#post80917

Email or PushService - see this link: http://www.b4x.com/forum/basic4android-getting-started-tutorials/11310-using-pop3-communicate-android-devices.html#post63207

Ringing the phone - I don't know if the PhoneStateListener Library is capable of that. Check here: http://www.b4x.com/forum/additional-libraries-classes-official-updates/12377-phonestatelistener.html#post69524 or contact Tomas.

All these would need a service running in foreground mode to catch the notifications of the server. And not all are easy to implement in VB6. So the easiest solution would be to either keep the connection going or use a service that queries the server in intervals. It depends on how fast you need to respond on notifications and how much battery drain you are willing to allow.

Rolf
 
Upvote 0

michelg

Member
Licensed User
Longtime User
Here is a sample with socket programming - I get information from the user, open a port on the server, send the information and display the response

the server I am using is actually built in VB6 using the native VB6 socket component

Tim,

I have my Android project working on the emulator. what should I do to download it to the Android phone? Please forgive my ignorance.

Thanks
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
If you were able to get it working in the emulator then B4A would have created an .APK for you. If you can copy that .APK to the Android device and install it.

Alternatively I usually connect my Android device to my computer and simply press Run in B4A.
 
Upvote 0

michelg

Member
Licensed User
Longtime User
If you were able to get it working in the emulator then B4A would have created an .APK for you. If you can copy that .APK to the Android device and install it.

Alternatively I usually connect my Android device to my computer and simply press Run in B4A.

I though it was that easy until I tried many times on my Android 2.2. There must be something else. I tried connecting the cable and clicked tried, It did not work.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Upvote 0
Top