Android and Java sockets – simple messenger

Hello all, as you requested in the previous TCP server-client tutorials, I made a new tutorial on how to create multiple client connections to a single server.

I posted the projects on GitHub here for you so I will post here only the main classes and functionality that I consider important to walk through. You will find there the Android client and the Java server, both should be ready to run.

OK so I started using the old project and changed it a bit to support multiple client connections.

Part 1 – The Client (User)

I will first explain what I added new on the Android side. I added a new Preference Activity class where the user can set its username for now(you can add there other stuff).

I linked the saved username preference value to the client connection so that the client will send each time the correct username and not a hard coded one.

I also check if the user set his username whenever he presses the “Connect” button and if he doesn’t, I display an error message inside a Toast.

That’s all for the client.

Part 2 – The server side

OK so to make the old server accept multiple connections, I had to make a loop and listen to new connections instead of trying to get only the first connection that gets into way like in the simple sample.

Before starting with that, I created a new model called User, the user class contains simple data of a real user like username, user id and the message he is trying to send.

Also I made a new UserManager class that manages a single client connection at a time. The UserManager class is basically what was before the TcpServer but id doesn’t contain the server socket as it handles only a client socket.

UserManager extends Thread to run a new loop on a secondary thread that listens to incoming messages from the client and also has the ability to send messages to that client.

On the TcpServer class I changed the runServer() method and make it like this:

That way the server will continuously search for new client connections and every time it catches one, creates a new UserManager and passes out the client socket to the manager in order to start the secondary thread where the messages are awaited.

Please note that the sample code can be enhanced and is just as an example.

At the moment the server can send only broadcast messages. As an exercise you can try and enhance it and create your version that can send messages only to a certain user or so.

I hope the code is documented enough, if not, please post your problem here and I will try to help you.

FAQ

Q: I started my client and server but I can’t see any messages, why?
A:
1. Please take a look at your logs, both on Android and Java and if you got some errors, your best friend is google and stackoverflow, if that doesn’t solve the problem then please post a comment here with the logs and the steps to reproduce the issue.
2. If no logs appear, please make sure you have the client and server devices in the same network and the firewall doesn’t block any of them.

Thank you ! šŸ™‚

Android TCP Connection Enhanced

[postad]

I made a new TCP Java server that supports now also multiple connections, please check it out here!We decided to make a separate post for this enhanced TCP Connection. You will still be able to see theĀ old TCP Connection TutorialĀ so I will put in this post only the code.TCP SERVER IN JAVA1. Create a new project in Java
2. Create a java class named “Constants”. The code for this class is below:

3. Create another class called “TcpServer”. The code for this class is below:

4. Create a new java class class called “MainScreen”. The code for this class is below:

TCP CLIENT IN ANDROID

1. Create an xml file under res – layout and name it “list_item”. The code for this xml file is below:

2. In main.xml file (the one which is generated by IDE) put the following code:

3. If you don’t have the “menu” directory under res – menu, you should create one and after it’s done, create a new xml file called “main_menu.xml” in the menu directory. Ā Then put the following code:

4. Now you have to create the java classes (in the src directory). The first class that you have to create is the activity class. So create a new java class and name it ClientActivity. The code for the activity is below:

5. Now create another java class and call it ClientListAdapter. The code is just below:

6. Create another java class called TcpClient.Ā 

7. Be sure that you AndroidManifest.xml file contains the Internet permission. The code for AndroidManifest is below:

If you need more exaplanations about TCP connection you can read theĀ old TCP Connection Tutorial. There are more details.

I hope this tutorial helped you šŸ˜€