package test.client.tcp;
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 test.client.tcp.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
/* Called when the activity is first created. */
public class main extends Activity {
private String strMsg = "";
private String read = "";
private String html = "";
private Handler mHandler;
private Socket socket;
private BufferedReader networkReader;
private BufferedWriter networkWriter;
private String ip ="127.0.0.1";
private int port = 9999;
private PrintWriter out;
private ListView mConversationView;
private ArrayAdapter<String> mConversationArrayAdapter;
@Override
protected void onStop() {
//TODO Auto-generated method stub
super.onStop();
try {
Log.d("DMSG", "Socket close...ok");
socket.close();
}catch (IOException e) {
Log.d("DMSG", "Socket close fail!");
//TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
try {
setSocket(ip,port);
Log.d("DMSG", "OK");
}catch (IOException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
Log.d("DMSG", "Error : " + e1);
}
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
mConversationView = (ListView) findViewById(R.id.in);
mConversationView.setAdapter(mConversationArrayAdapter);
mConversationView.setTextFilterEnabled(true);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
EditText et = (EditText) findViewById(R.id.EditText01);
public void onClick(View v) {
if (et.getText().toString() != null || !et.getText().toString().equals("")) {
/*PrintWriter*/ out = new PrintWriter(networkWriter,true);
String return_msg = et.getText().toString();
out.println(return_msg);
out.flush();
strMsg = "Send Message : " + return_msg;
mConversationArrayAdapter.add(strMsg);
Log.d("DMSG", "Msg = " + return_msg);
et.setText("");
}
}
});
try {
Thread checkUpdate = new echoThread(mConversationArrayAdapter, out);
checkUpdate.start();
}
catch(Exception ex) {
Log.d ("DMSG", "Error : " + ex);
}
}
class echoThread extends Thread {
ArrayAdapter<String> conversationArrayAdapter;
PrintWriter pw;
String tmp;
public echoThread(ArrayAdapter<String> mConversationArrayAdapter, PrintWriter out)
{
this.conversationArrayAdapter = mConversationArrayAdapter;
this.pw = out;
Log.d("DMSG", "Thread init.");
}
public void run() {
try {
while (true) {
Log.w("DMSG","chatting is running");
try {
Thread.sleep(100);
} catch (Exception e) {
Log.d("DMSG", "Error : " + e);
}
read = null;
while((read = networkReader.readLine()) != null)
{
Log.d("DMSG", "receive msg");
mHandler.post(showUpdate);
}
}
}catch (Exception e) {
Log.d("DMSG", "Client Thread Error!" + e);
}
}
};
private Runnable showUpdate = new Runnable() {
public void run() {
Toast.makeText(main.this,"Coming word: " + html, Toast.LENGTH_SHORT).show();
mConversationArrayAdapter.add("Receive Message : " + read);
}
};
public void setSocket(String ip,int port)throws IOException {
try {
socket =new Socket(ip, port);
networkWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
networkReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("DMSG", "Client Socket Init...");
}catch (IOException e) {
System.out.println(e);
e.printStackTrace();
Log.d("DMSG", "Client Socket Init fail!");
}
}
}
|