package com.mnk.TCPsoketTest;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import android.content.Intent;

public class TCPsoketTest extends Activity {
  /** Called when the activity is first created. */
	private String return_msg;
  @Override
  	public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    
    
    // �� ���� ��ü�� ���Ͽ� ���̾ƿ��� �����Ѵ�.
    final EditText et = (EditText)findViewById(R.id.EditText01);
    final EditText et2 = (EditText)findViewById(R.id.EditText02);
    Button btn = (Button)findViewById(R.id.Button01);
    Button btnj = (Button)findViewById(R.id.Button02);
    final TextView tv = (TextView)findViewById(R.id.TextView01);
    
    startService(new Intent("com.rosa.service.test"));
    
    // ��ư Ŭ�� ������
    btn.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
    	//stopService(new Intent("com.rosa.service.test"));
    	
     if(et.getText().toString() != null || !et.getText().toString().equals("")){
    	 if(et2.getText().toString() != null || !et2.getText().toString().equals("")){
    		 String et3 = et.getText().toString() + " " + et2.getText().toString();    		 
    		 TCPclient tp = new TCPclient(et3);
    		 tp.run();
    		 // ��ư�� Ŭ���ϸ� ����Ʈ�ؽ�Ʈ�信 ���� �ؽ�Ʈ�� ��Ʈ���̷� ������ TCPclient ��ü�� �����.
     
    		// Toast t = Toast.makeText(getApplicationContext(), return_msg, Toast.LENGTH_LONG);
    		// t.show();
    		 tv.setText(return_msg);
    		 if(return_msg.equals("OK")){    			 
    			 startActivity(new Intent(getApplicationContext(),MenuView.class));
    		 }
    	 }
     }
     
    }
    });
    
    btnj.setOnClickListener(new OnClickListener(){
    	 public void onClick(View v){
    		 startActivity(new Intent(getApplicationContext(),JoinView.class));    		 
    	 }
    
    });
        
  }
  
  private class TCPclient implements Runnable {
	  // Ŭ������ ��ü�� ������ IP�� ��Ʈ. �׸��� �Է��� �ؽ�Ʈ ��Ʈ���� �����Ѵ�.
  private static final String serverIP = "10.0.18.79"; // ex: 192.168.0.100
  private static final int serverPort = 4442; // ex: 5555
  private String msg;
  //private String return_msg;
  
  public TCPclient(String _msg){
   this.msg = _msg;
  }
  @Override
  public void run() {
   // TODO Auto-generated method stub
   try {
      // IP �ּҸ� ���� �����ּҸ� ���´�.
      InetAddress serverAddr = InetAddress.getByName(serverIP);
      
      Log.d("TCP", "C: Connecting...");
      
      // �����ּҿ� ��Ʈ�� ���� ���� ��ü�� �����.
      Socket socket = new Socket(serverAddr, serverPort);
      
     try {
      Log.d("TCP", "C: Sending: '" + msg + "'");
      PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
      // ���Ͽ� �޽�����Ʈ���� �߰��Ͽ� ������ ������.
      out.println(msg);
      Log.d("TCP", "C: Sent.");
         Log.d("TCP", "C: Done.");
         
         // ������ ���� ���ϸ޽����� ���� ������ ���ۿ� ��´�.
         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
         return_msg = in.readLine();
         
         Log.d("TCP", "C: Server send to me this message -->" + return_msg);
        } catch(Exception e) {
          Log.e("TCP", "C: Error1", e);
      } finally {
       socket.close();
      }
      } catch (Exception e) {
        Log.e("TCP", "C: Error2", e);
      }
  }
  
  }
}