Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dk.itu.androidsocketserver;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketException;
- import java.util.Enumeration;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.widget.TextView;
- public class TCPSendActivity extends Activity {
- private TextView serverStatus;
- // default ip
- //public static String SERVERIP = "10.25.254.241";
- // note: a default seems confusing right ?
- public static String SERVERIP;
- // 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.main);
- 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);
- }
- });
- InetAddress receiversAddress = InetAddress.getByName("10.0.1.7");
- int receiversPort = 7070;
- // create a new socket
- Socket socket = new Socket( receiversAddress, receiversPort );
- OutputStream os = socket.getOutputStream();
- // could have gotten an InputStream as well
- DataOutputStream dos = new DataOutputStream( os );
- String messageAsString = "Hello from android";
- dos.writeUTF(messageAsString);
- dos.flush();
- socket.close();
- } 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment