Share My Creation Simple Multi-User Chat Demo

Sooner or later somebody will want to know how to do this. It seems to be a fairly common "What next after Hello World?" sort of project.

The attached ZIP archive has two B4J programs: SimpleServer and SimpleClient.

While there is a huge amount I have to learn yet about B4J, these seem to be working properly. That doesn't mean they're bug-free or that I haven't done some things clumsily but these ought to be useful to help others get started with TCP sockets.

As provided these use ASCII for transmission with a CR message delimiter. You could easily change them to use UTF-8 and another delimiter or a length prefix instead.

There is a ReadMe.txt in the archive and I'll try to post a screenshot here.
 

Attachments

  • SimpleChat.zip
    19.5 KB · Views: 943
  • ScreenShot.png
    ScreenShot.png
    56.4 KB · Views: 2,423

LucaMs

Expert
Licensed User
Longtime User
Thank you, Dilettante (anche se io credo che potrei scrivere "Grazie" :))

I need example like this (I have not downloaded it yet but I'm going to do it).

If you could convert it for BlueTooth and for PC to Android-device, I would avoid many headaches :p

Ciao e grazie


P.S. Sorry, Bob, "dilettante" is an italian word! lol
 
Last edited:

dilettante

Active Member
Licensed User
Longtime User
It should be easy enough to convert SimpleClient into an Android version that interoperates with my Java SimpleServer using TCP sockets. Probably a little more work but not a lot to convert SimpleServer to an Android version.

To operate over Bluetooth the simplest thing might be to use a library containing a Java implementation of BluetoothSocket and BluetoothServerSocket, but I don't know whether such a library exists (either for Java itself or a even wrapped version for B4J to use). Android already has a Bluetooth Library that contains such classes, though I have not used it.

But I believe the custom here would be to create a question thread in order to get help with changes like this. It would probably bring in more people with specific experience using Bluetooth between an Android device and a Java device (such as a PC).
 
Last edited:

dilettante

Active Member
Licensed User
Longtime User
Thanks Erel.

That wouldn't really be multiuser (COM-port per Android device/user might work) but it probably addresses the needs of many people. The use of sockets is probably constrained by platform limitations, since I haven't found anything like the Android Bluetooth library for Java on Windows or Linux.
 

LucaMs

Expert
Licensed User
Longtime User
Yes, but I'd just need a conversation between pc and smartphone (without VB.Net or other!
Why I did not searched???

Thank you, Erel, this was for me the Christmas gift.

Thanks for your help, dilettante.

And again, happy holidays
 

BPak

Active Member
Licensed User
Longtime User
Love the Chat Project, dilettante. Ran great on my Mac as well. Only thing I had to do is put a Client.jar in another location to get the second Client working.
 

Attachments

  • Chat.jpg
    Chat.jpg
    234.8 KB · Views: 651

jmon

Well-Known Member
Licensed User
Longtime User
Hi,
Thanks a lot for sharing with us. I have an issue, the server constantly drops the connection and I keep on getting the "#Server closed connection" message on the client side. Any idea what could cause this issue?

Thanks.
 

dilettante

Active Member
Licensed User
Longtime User
Hi,
Thanks a lot for sharing with us. I have an issue, the server constantly drops the connection and I keep on getting the "#Server closed connection" message on the client side. Any idea what could cause this issue?

I haven't seen this. Are you using an unreliable WiFi or cellular data network or something? This is one of the reasons why HTTP 1.0 often works best for mobile clients: request, response, abandon the connection.

15 minutes (and counting), no drop-outs yet here.
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
I haven't seen this. Are you using an unreliable WiFi or cellular data network or something? This is one of the reasons why HTTP 1.0 often works best for mobile clients: request, response, abandon the connection.

15 minutes (and counting), no drop-outs yet here.
Thanks for your reply. I am on a LAN network. I will check if something else could be dropping the connection. I'll post again if I find something.

[EDIT] I apologize, your example works perfectly and doesn't drop the connection. I made a mistake when I modified your example in the "srvListener_NewConnection". I was setting the "intClient" to a wrong number and when new users where joining the conversation, they would be assigned to a wrong number and overlapping the old users, thus dropping the connection.
My bad!
 
Last edited:

dilettante

Active Member
Licensed User
Longtime User
Love the Chat Project, dilettante. Ran great on my Mac as well. Only thing I had to do is put a Client.jar in another location to get the second Client working.

I'm still curious about this. On a Mac does the JAR file get locked so you can't run a second instance, or did I misinterpret the issue?
 

BPak

Active Member
Licensed User
Longtime User
Not a Mac specialist but it just would not run a second Client from same location. Guess it stops it from running second instance.

However, when copying the prog to another folder it was allowed to run a second program of the same name.

I could test it more if required.?
 

BPak

Active Member
Licensed User
Longtime User
I tested it again on Mac Lion and it will only run one instance.

However Right-click the Icon for the Client and select Copy from popup menu and a copy of the client is created. It can be ran and used as the second instance.
I also tried using Right-Click and selecting Duplicate which did same as Copy.

Did search from Spotlight for 'run another instance of an application' and came up with a Web Search for Google and found a couple web sites that confirm the Mac is ONE instance only by design.

https://www.google.com/search?rls=en_AU&q=run+another+instance+of+an+application&ie=UTF-8&oe=UTF-8

http://reviews.cnet.com/8301-13727_...multiple-instances-of-an-application-in-os-x/

One says Copy as I have done. The other says it can be done from the Console
 

jmon

Well-Known Member
Licensed User
Longtime User
Thanks for your great script. It works very well.

I sometimes have a bug that would crash the server. This fixes the bug:
B4X:
Public Sub Close()
    astmClient.Close
    sckClient.IsInitialized Then
    sckClient.Close
    sckClient = Null
    mInUse = False 'switching this line
    CallSub2(mParent, mEventName & "_Terminated", Me) 'and this line
End Sub
I think you have to put the mInUse = False before the callSub2, because when calling the _Terminated event in the main module would still have this client registered as inUse = true and that could cause and crash the server if trying to send data to this client.
 
Top