java - server and client chat over wifi in android devices.ex Samsung Duos, Galaxy -


I am a new bee for Android development, and I like a chat app between 2 or more Android devices like Samsung Duos I'm planning for or higher version, without using the Internet and using only WiFi 1) Is this possible? If so, suggest some code section or link to establish a connection between WiFi and transfer text messages between them.

You must create the same app for two devices that use chat.

Useful for putting in AndroidManifest.xml:

  & lt; Usage-permission Android: name = "android.permission.VIBRATE" /> & Lt; Usage-permission Android: name = "android.permission.INTERNET" /> & Lt; Usage-permission Android: name = "android.permission.ACCESS_WIFI_STATE" / & gt; & Lt; Usage-permission Android: name = "android.permission.ACCESS_NETWORK_STATE" /> & Lt; Usage-permission Android: name = "android.permission.WRITE_EXTERNAL_STORAGE" /> & Lt; Usage-permission Android: name = "android.permission.READ_EXTERNAL_STORAGE" />   

Then in the main activity class and other sections where the data is transferred, it must be inserted:

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy Builder () .permitAll () Build (); StrictMode.setThreadPolicy (policy);   

This part of the code enables the device to send and receive data from external devices connected to the same WiFi network.

OK, so you have to make chairs. A socket allows you to send and receive special IP and port data forms. You have a very simple example:

  socket client = new socket ("1.2.3.4", 2000); // It will connect to other device output stream = client.getOutputStream (); // This will send you data in InputStream = client.getInputStream (); // This will let you get the data   

Now if you can send and receive, then there is a common question I have ever solved here ...

What if you want to know if your friend sent you data? You need to create a BufferedReader :

  iBuff = new BufferedReader (new InputStreamReader) (client.getInputStream () ));   

Then you can know whether data is available in buffer:

  while (iBuff.ready ()) {// while there is data in it Buffer in. Raid (byte, 0, 1); // read}   

If you try to read with empty buffer, it will be blocked. This is for Input.available () to know whether the data is available or not, it needs to be read, and you will have the same problem.

There you have some tips on where I spent so many hours to fix it :) I hope this will help you.

PD: Sorry for my English, I'm Spanish ^^ '

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -