package com.hb.adt0303;
import android.support.v7.app.ActionBarActivity;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MultiServer extends Activity implements Runnable{
private static final int CONNECT = 0;
private static final int CONNECTED = 1;
private static final int MESSAGE = 2;
private static final int DISCONNECTED = 3;
private static final int TEMP = 4;
private Socket sc = null;
private String host = "203.236.209.131";
private int port = 8000;
private String user = "ace"; //ibkhb
private TextView ta = null;
private EditText tf = null;
private Button btn1 = null;
private String str;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
btn1 = (Button)findViewById(R.id.sendBtn);
ta = (TextView)findViewById(R.id.state);
tf = (EditText)findViewById(R.id.input);
tf.setImeOptions(EditorInfo.IME_ACTION_NONE);
tf.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (sc == null) return false;
String str = tf.getText().toString().trim();
if (str.equals("")) return false;
try {
OutputStream os = sc.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println(user + "]" + str);
ta.append("\r\n[send]" + str);
tf.setText("");
tf.requestFocus();
} catch (Exception e2) {
ta.append("server failed...");
sc = null;
}
return true;
}
return false;
}
});
btn1.setOnClickListener(new View.OnClickListener(){//보내기
public void onClick(View v) {
if (sc == null) return ;
String str = tf.getText().toString().trim();
if (str.equals("")) return ;
try {
OutputStream os = sc.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println(user + "]" + str);
ta.append("\r\n[send]" + str);
tf.setText("");
tf.requestFocus();
} catch (Exception e2) {
ta.append("server failed...");
sc = null;
}
}
});
handler.sendEmptyMessageDelayed(CONNECT, 10);
} //end
public void onDestroy() {
super.onDestroy();
System.exit(0);
} //end
public void connect() {
try {
sc = new Socket(host, port);
ta.setText("server connect ...");
handler.sendEmptyMessageDelayed(CONNECTED, 10);
Thread th = new Thread(this);
th.start();
} catch (Exception e) {
System.out.println(e.toString());
sc = null;
}
} //end
public void run() {
try {
if (sc == null) return;
InputStream is = sc.getInputStream();
while((str = readLine(is)) != null) {
handler.sendEmptyMessageDelayed(MESSAGE, 10);
}
} catch (Exception e) {
handler.sendEmptyMessageDelayed(DISCONNECTED, 10);
sc = null;
}
} //end
public static String readLine(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while (true) {
int b = in.read();
if (b < 0) {
throw new IOException("Data truncated");
}
if (b == 0x0A) {
break;
}
buffer.write(b);
} //while end
return new String(buffer.toByteArray(), "UTF-8");
} //end
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case CONNECT:
connect();
break;
case CONNECTED:
runOnUiThread(new Runnable() {
public void run() {
ta.setText("server connect ...");
}
});
break;
case MESSAGE:
runOnUiThread(new Runnable() {
@Override
public void run() {
ta.append("\r\n" + str);
}
});
break;
case DISCONNECTED:
runOnUiThread(new Runnable() {
public void run() {
ta.append("\r\nserver failed...");
}
});
break;
case 4:
runOnUiThread(new Runnable() {
public void run() {
}
});
default:
break;
} //switch end
}; //handleMessage( ) end
};
} //class END
RECENT COMMENT