Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package androidChatClient.src;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6.  
  7. import android.app.Activity;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.view.View;
  12. import android.widget.*;
  13.  
  14. public class Main extends Activity {
  15.    
  16.     Socket socket;
  17.     private EditText _output;
  18.     private EditText _input;
  19.    
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.        
  24.         String ip = "192.168.1.3";
  25.         _output = (EditText)findViewById(R.id.output);
  26.        
  27.         try {
  28.             _output.append("Connecting to..."+ip+"\n");
  29.             InetAddress ia = InetAddress.getByName(ip);
  30.             socket = new Socket(ia, 7777);
  31.             _output.append("Connected\n");
  32.             socket.getOutputStream().write(new String(InetAddress.getLocalHost().getHostAddress()+" joined.").getBytes());
  33.            
  34.         } catch (Exception e) {
  35.             // TODO Auto-generated catch block
  36.             System.out.println("Client:" + e);
  37.             e.printStackTrace();
  38.         }
  39.        
  40.         class ListenTask extends AsyncTask<Void, String, Void>{
  41.             protected Void doInBackground(Void... args) {
  42.                 boolean run = true;
  43.                 while(run){
  44.                 return null;
  45.             }
  46.         }
  47.     }
  48.    
  49.     public void exit(View view){
  50.         try{
  51.         socket.getOutputStream().write(new String(InetAddress.getLocalHost().getHostAddress()+" left.").getBytes());
  52.         socket.close();
  53.         System.exit(0);
  54.         }catch (Exception e){
  55.         }
  56.     }
  57.    
  58.     public void send(View view){
  59.         Editable textTemp;
  60.         _input = (EditText)findViewById(R.id.input);
  61.         textTemp = _input.getText();
  62.         String text = textTemp.toString();
  63.        
  64.         try {
  65.             socket.getOutputStream().write(new String(text).getBytes());
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.        
  70.         _input.setText("");
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement