MainActivity.java
package com.houxy.chatserver;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
private static final int PORT = 1234;
private Button btnSendMSG;
private EditText editMSG;
private TextView receivedText;
private Handler mHandler = new Handler(Looper.getMainLooper());
private TcpServer tcpServer = new TcpServer() {
@Override
public void onReceive(final String s) {
mHandler.post(new Runnable() {
@Override
public void run() {
receivedText.append("Client :"+ s + "\n\n");
}
});
}
@Override
public void onConnectBreak() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"连接失败", Toast.LENGTH_LONG).show();
refreshUI(false);
}
});
}
@Override
public void onConnect() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"连接成功", Toast.LENGTH_LONG).show();
Log.d("runFlag", "1");
refreshUI(true);
Log.d("runFlag", "12");
}
});
}
@Override
public void onSendSuccess(final String s) {
mHandler.post(new Runnable() {
@Override
public void run() {
receivedText.append("我 :" + s + "\n\n");
}
});
}
};
private void refreshUI(boolean isConnected) {
btnSendMSG.setEnabled(isConnected);
btnSendMSG.setBackgroundResource(isConnected ?
R.drawable.send_button : R.drawable.send_button_disabled);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
refreshUI(false);
tcpServer.start(PORT);
Log.d("zhouqi", "create");
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sendMsg:
String msg = editMSG.getText().toString();
editMSG.setText("");
tcpServer.getTransceiver().sendMSG(msg);
break;
default:
break;
}
}
private void initView() {
btnSendMSG = (Button)findViewById(R.id.btn_sendMsg);
editMSG = (EditText)findViewById(R.id.edit_msg);
receivedText = (TextView)findViewById(R.id.tx_receive);
btnSendMSG.setOnClickListener(this);
}
@Override
protected void onStop() {
super.onStop();
tcpServer.disConnected();
Log.d("zhouqi", "stop");
}
}
TcpServer.java
package com.houxy.chatserver;
import android.util.Log;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by asus on 2016/3/23.
*/
public abstract class TcpServer implements Runnable{
private int port;
private ServerSocket serverSocket;
private Socket socket;
private boolean runFlag=true;
private SocketTransceiver transceiver;
public void start(int port){
this.port = port;
new Thread(this).start();
}
@Override
public void run() {
try {
serverSocket = new ServerSocket(port);
while (runFlag){
socket = serverSocket.accept();
this.onConnect();
transceiver = new SocketTransceiver() {
@Override
public void onReceive(String s) {
TcpServer.this.onReceive(s);
}
@Override
public void onConnectBreak() {
TcpServer.this.onConnectBreak();
}
@Override
public void onSendSuccess(String s) {
TcpServer.this.onSendSuccess(s);
}
};
transceiver.start(socket);
}
} catch (IOException e) {
this.onConnectBreak();
e.printStackTrace();
}
//服务器与客服端断开
}
public boolean isConnected(){
return runFlag;
}
public void disConnected(){
runFlag = false;
if(transceiver!=null){
transceiver.stop();
transceiver=null;
}
}
public SocketTransceiver getTransceiver(){
return transceiver;
}
public abstract void onReceive(String s);
public abstract void onConnectBreak();
public abstract void onConnect();
public abstract void onSendSuccess(String s);
}
Transceiver.java
- 这里的程序和Client端的收发器一致,故不再重复贴出代码
Server端截图