pellekrogholt

Untitled

Oct 26th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package dk.itu.androidsocketserver;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.net.InetAddress;
  7. import java.net.NetworkInterface;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.net.SocketException;
  11. import java.util.Enumeration;
  12.  
  13. import android.app.Activity;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.util.Log;
  17. import android.widget.TextView;
  18.  
  19.  
  20. public class TCPSendActivity extends Activity {
  21.  
  22.     private TextView serverStatus;
  23.  
  24.     // default ip
  25.     //public static String SERVERIP = "10.25.254.241";
  26.     // note: a default seems confusing right ?
  27.     public static String SERVERIP;
  28.  
  29.     // designate a port
  30.     public static final int SERVERPORT = 8080;
  31.  
  32.     private Handler handler = new Handler();
  33.  
  34.     private ServerSocket serverSocket;
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.main);
  40.         serverStatus = (TextView) findViewById(R.id.server_status);
  41.  
  42.         SERVERIP = getLocalIpAddress();
  43.  
  44.         Thread fst = new Thread(new ServerThread());
  45.         fst.start();
  46.     }
  47.  
  48.     public class ServerThread implements Runnable {
  49.  
  50.         public void run() {
  51.             try {
  52.                 if (SERVERIP != null) {
  53.                     handler.post(new Runnable() {
  54.                         @Override
  55.                         public void run() {
  56.                             serverStatus.setText("Listening on IP: " + SERVERIP);
  57.                         }
  58.                     });
  59.  
  60.                     InetAddress receiversAddress = InetAddress.getByName("10.0.1.7");
  61.                     int receiversPort = 7070;
  62.                    
  63.                    
  64.                     // create a new socket
  65.                     Socket socket = new Socket( receiversAddress, receiversPort );
  66.                    
  67.                     OutputStream os = socket.getOutputStream();
  68.                     // could have gotten an InputStream as well
  69.                    
  70.                     DataOutputStream dos = new DataOutputStream( os );
  71.                    
  72.                     String messageAsString = "Hello from android";
  73.                    
  74.                     dos.writeUTF(messageAsString);
  75.                    
  76.                     dos.flush();
  77.                    
  78.                     socket.close();
  79.                    
  80.                
  81.                 } else {
  82.                     handler.post(new Runnable() {
  83.                         @Override
  84.                         public void run() {
  85.                             serverStatus.setText("Couldn't detect internet connection.");
  86.                         }
  87.                     });
  88.                 }
  89.             } catch (Exception e) {
  90.                 handler.post(new Runnable() {
  91.                     @Override
  92.                     public void run() {
  93.                         serverStatus.setText("Error");
  94.                     }
  95.                 });
  96.                 e.printStackTrace();
  97.             }
  98.         }
  99.     }
  100.  
  101.     // gets the ip address of your phone's network
  102.     private String getLocalIpAddress() {
  103.         try {
  104.             for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
  105.                 NetworkInterface intf = en.nextElement();
  106.                 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
  107.                     InetAddress inetAddress = enumIpAddr.nextElement();
  108.                     if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
  109.                 }
  110.             }
  111.         } catch (SocketException ex) {
  112.             Log.e("ServerActivity", ex.toString());
  113.         }
  114.         return null;
  115.     }
  116.  
  117.     @Override
  118.     protected void onStop() {
  119.         super.onStop();
  120.         try {
  121.              // make sure you close the socket upon exiting
  122.              serverSocket.close();
  123.          } catch (IOException e) {
  124.              e.printStackTrace();
  125.          }
  126.     }
  127.  
  128. }
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment