Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-to-many TCP communications
    primarykey
    data
    text
    <p>I am trying to put together a basic Java proof-of-concept to have a server (which will most likely just be a Windows or Linux laptop) that can send messages to a number of WIFI-connected android devices on the same subnet (initially 20-30, but it could be quite a number in the future - 2 or 300? - not sure, but I need to allow for the possibility). The app will send a message to all devices (the same message initially - but I may allow for special cases somewhere down the track). Each device can then reply, and I will want to check which ones replied and store the replies.</p> <p>I tried some test code using UDP (with some help from a tutorial on nakov.com), but it was very unreliable and the phone often locked up while trying to receive a packet - although it worked perfectly sometimes. My client code for the android device is shown below for reference (note, this code just sends a request for a message, and then receives and displays it - I hadn't got around to writing it the way I wanted it since it didn't seem reliable enough to use anyway).</p> <p>So, I think I probably need to use TCP for reliability (although I am open to suggestions). Would 2-300 individual TCP concurrent connections be ok? And its likely that all the replies would come in to the server at once (or over a 10-15 second period), would this flood it in a way it couldn't handle? Is it likely to be ok for only 20-30 devices?</p> <p>I know this is a kind of out-there question, but I'm hoping someone with more understanding of TCP than I have can shed some light and give me some pointers.</p> <p>Cheers</p> <p><strong>Steve</strong></p> <p>UDP Client App:</p> <pre><code>package nz.co.et.quoteclient; import java.io.IOException; import java.net.DatagramSocket; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class QuoteClient extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.Button01); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (wifiManager.isWifiEnabled()) { TextView errorText = (TextView) findViewById(R.id.TextView01); int serverPort = 4445; InetAddress address = null; try { address = InetAddress.getByName("192.168.1.4"); } catch (UnknownHostException e) { errorText.setText("Failed at setting server IP Address"); e.printStackTrace(); } byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length, address, serverPort); DatagramSocket socket = null; try { socket = new DatagramSocket(); } catch (SocketException e) { errorText.setText("Failed at creating socket"); e.printStackTrace(); } try { socket.send(packet); } catch (IOException e) { errorText.setText("Failed at sending request packet"); e.printStackTrace(); } // get response packet = new DatagramPacket(buf, buf.length); try { socket.receive(packet); } catch (IOException e) { errorText.setText("Failed at receiving quote packet from server"); e.printStackTrace(); } // display response String message = new String(packet.getData()); String data = ""; for (int i = 0; i &lt; message.length(); i++) { int tmp = (int) message.charAt(i); data = (tmp &lt;= 0 || tmp &gt;= 256) ? data += "" : data + message.charAt(i); } Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show(); // errorText.setText(data); } else { Toast.makeText(getApplicationContext(), "WIFI not enabled", Toast.LENGTH_LONG).show(); } } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload