CopyPastor

Detecting plagiarism made easy.

Score: 0.8173084259033203; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2012-05-05
by Shankar Agarwal

Original Post

Original - Posted on 2012-03-25
by ertan2002



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Have you looked at [Qualcomm's AllJoyn library][1]? It works over Bluetooth or wifi only.
The below code is copied from this [Incorporating Socket Programming into your Applications][2]
public class ServerActivity extends Activity { private TextView serverStatus; // default ip public static String SERVERIP = "10.0.2.15"; // designate a port public static final int SERVERPORT = 8080; private Handler handler = new Handler(); private ServerSocket serverSocket; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.server); serverStatus = (TextView) findViewById(R.id.server_status); SERVERIP = getLocalIpAddress(); Thread fst = new Thread(new ServerThread()); fst.start(); } public class ServerThread implements Runnable { public void run() { try { if (SERVERIP != null) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Listening on IP: " + SERVERIP); } }); serverSocket = new ServerSocket(SERVERPORT); while (true) { // listen for incoming clients Socket client = serverSocket.accept(); handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Connected."); } }); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String line = null; while ((line = in.readLine()) != null) { Log.d("ServerActivity", line); handler.post(new Runnable() { @Override public void run() { // do whatever you want to the front end // this is where you can be creative } }); } break; } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); } }); e.printStackTrace(); } } } else { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Couldn't detect internet connection."); } }); } } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Error"); } }); e.printStackTrace(); } } } // gets the ip address of your phone's network private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("ServerActivity", ex.toString()); } return null; } @Override protected void onStop() { super.onStop(); try { // make sure you close the socket upon exiting serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }

[1]: https://developer.qualcomm.com/mobile-development/mobile-technologies/peer-peer-alljoyn [2]: http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
I had same problem... I wanted to use comet but I havent much time to research about it..
you can see some sample here.. http://blogs.webtide.com/dyu/entry/android_chat_using_jetty_cometd
and I choosed infinite loops for communication
here is my code..

public class ServerActivity extends Activity { private TextView serverStatus; // default ip public static String SERVERIP = "10.0.2.15"; // designate a port public static final int SERVERPORT = 12345; private Handler handler = new Handler(); private ServerSocket serverSocket; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.server); serverStatus = (TextView) findViewById(R.id.server_status); SERVERIP = getLocalIpAddress(); Thread fst = new Thread(new ServerThread()); fst.start(); } public class ServerThread implements Runnable { String line = ""; public void run() { try { if (SERVERIP != null) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Listening on IP: " + SERVERIP); } }); serverSocket = new ServerSocket(SERVERPORT); while (true) { // listen for incoming clients Socket client = serverSocket.accept(); handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Connected."); } }); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); // byte[] resultBuff = new byte[0]; // byte[] buff = new byte[1024]; // int k = -1; // while((k = client.getInputStream().read(buff, 0, buff.length)) > -1) { // byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size = bytes already read + bytes last read // System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy previous bytes //System.arraycopy(buff, 0, tbuff, resultBuff.length, k); // copy current lot //resultBuff = tbuff; // call the temp buffer as your result buff //} //System.out.println(resultBuff.length + " bytes read.");

while ((line = in.readLine()) != null) { Log.d("ServerActivity", line); handler.post(new Runnable() { @Override public void run() { serverStatus.setText(line); } }); } break; } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); } }); e.printStackTrace(); } } } else { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Couldn't detect internet connection."); } }); } } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Error"); } }); e.printStackTrace(); } } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;