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 😀

Android TCP Connection Tutorial

In this tutorial we will make a TCP Connection. The server will be written in Java and the client will be written in Android. Actually it will be a very simple messenger client.

Note: Now we have an enhanced version of TCP Connection here, but it’s just the code. For more details and explanations you still have to read the current tutorial. 🙂

1. Make the Server in Java
To create the server create a new project in Java and make a class called “TCPServer”. In this class put the following code:
UPDATED

UPDATE: NEW CLASS
Now create another java class and call it “ServerBoard”. In this class we will make the “messenger” UI (a very simple one) with a field where the messages will be added, a text field where the server side can enter a message and 2 buttons: one to start the connection and one to send the messages. The code is like this:

2. Create the Client in Android

  • To create the Client, create a new project and call the class that creates with the project ”   MyActivity”. 
  • After this make a new class and call it “TCPClient”
  • Now in the class TCPClient put the following code:
UPDATED
 

At the beginning of this class you have a constant SERVERIP. In this constant you have to put your computer IPv4. To do this open the commander and type ipconfig and press Enter. Then you will see your computer’s IP.

Also the SERVERPORT from this class (TCPClient) must be the same with the SERVERPORT from server class (TCPServer).

Now that the server and client classes are done, we have to set the interface a little. For this we will make a ListView in MyActivity class where we will get the messages sent and received, an EditText to enter the message from client side and a button to send the message. So let’s begin.

  • go to res – layout – main.xml and put this code in main.xml:

  • create a new xml file and call it “list_item.xml”. This will contain a TextView for the messages which will be added to the ListView. The code will look like this:

  • create a new class and call it “MyCustomAdapter”.  

UPDATED

  • And now in the MyActivity class put the following code:

  • And one last thing. Go to AndroidManifest.xml and put the internet permission just before the :

Now that the project is done we must see how it actually works

1. First, you must open the server, so run the TCPServer class, the one that is made in Java and a window called ServerBoard will appear. Press on the start button like in the picture below. Do NOT start the android project before you press the start button!

2. The server waits the client to connect now, so run the android project on the emulator and wait for the project to load.

3. After the project is loaded enter a message in the EditText and send it. Your text should appear on the ListView.
4. Now go back to the ServerBoard and send a message to the client from Android. The message from server side should appear on the ListView too.
NOTE: If you test this on a device and it’s not working, probably you should turn off all your firewall (from Windows, from antivirus) . And make sure that your device WIFI is turned on (because the android client must be on the same network as the server).
 

I hope that this tutorial helped you 🙂