package com.example.eg18_1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText editText;
    private Button button;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
        .penaltyLog() //打印logcat，当然也可以定位到dropbox，通过文件保存相应的log
        .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects() //探测SQLite数据库操作
        .penaltyLog() //打印logcat
        .penaltyDeath()
        .build()); 

        editText = (EditText)findViewById(R.id.editText1);
        button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Socket socket = null;
                String message = editText.getText().toString()+ "\r\n" ;
                try {
                    //创建客户端socket，注意：不能用localhost或127.0.0.1
                	//Android模拟器把自己作为localhost
                    socket = new Socket("192.168.0.5",18888);
                    PrintWriter out = new PrintWriter(
                    		new BufferedWriter(new OutputStreamWriter
                            (socket.getOutputStream())),true);
                    //发送数据
                    out.println(message);
                    //接收数据
                    BufferedReader in = new BufferedReader(
                    		new InputStreamReader(socket.getInputStream()));
                    String msg = in.readLine();
                    if (null != msg){
                        editText.setText(msg);
                        System.out.println(msg);
                    }
                    else{
                        editText.setText("data error");
                    }
                    out.close();
                    in.close();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally{
                    try {
                        if (null != socket){
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}