MainActivity.java
package com.houxy.chat;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
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 EditText editIP;
private EditText editPort;
private EditText editMsg;
private Button btnLink;
private Button btnSend;
private TextView textReceive;
private Handler mHandler = new Handler(Looper.getMainLooper());
private TcpClient tcpClient = new TcpClient() {
@Override
public void onConnect() {
refreshUI(true);
}
@Override
public void onConnectBreak() {
refreshUI(false);
}
@Override
public void onReceive(final String s) {
mHandler.post(new Runnable() {
@Override
public void run() {
textReceive.append("server:" + s + "\n\n");
}
});
}
@Override
public void onConnectFalied() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "連接失敗",
Toast.LENGTH_LONG);
}
});
}
@Override
public void onSendSuccess(final String s) {
mHandler.post(new Runnable() {
@Override
public void run() {
textReceive.append("我:" + s + "\n\n");
}
});
}
};
private void refreshUI(final boolean isConnected) {
mHandler.post(new Runnable() {
@Override
public void run() {
editIP.setEnabled(!isConnected);
editPort.setEnabled(!isConnected);
btnLink.setText(isConnected ? "斷開" : "連接");
btnSend.setBackgroundResource(isConnected ?
R.drawable.send_button : R.drawable.send_button_disabled );
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
refreshUI(false);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_Link:
if(tcpClient.isConnected()){
tcpClient.disConnected();
}else {
String IP = editIP.getText().toString().trim();
String strPort = editPort.getText().toString();
int port = Integer.parseInt(strPort);
tcpClient.connect(IP, port);
}
break;
case R.id.btn_sendMsg:
String msg = editMsg.getText().toString();
editMsg.setText("");
tcpClient.getTransceiver().sendMSG(msg);
break;
default:
break;
}
}
@Override
protected void onStop() {
tcpClient.disConnected();
super.onStop();
}
private void initView() {
setContentView(R.layout.activity_main);
editIP = (EditText)findViewById(R.id.ed_ip);
editPort = (EditText)findViewById(R.id.ed_port);
editMsg = (EditText)findViewById(R.id.edit_msg);
btnLink = (Button)findViewById(R.id.btn_Link);
btnSend = (Button)findViewById(R.id.btn_sendMsg);
textReceive = (TextView)findViewById(R.id.tx_receive);
// listView = (ListView)findViewById(R.id.listView);
btnSend.setOnClickListener(this);
btnLink.setOnClickListener(this);
}
}
TcpClient.java
- TcpClient 是Tcp Socket客戶端的程序,主要是實(shí)現(xiàn)socket的連接,并利用SocketTransceiver進(jìn)行數(shù)據(jù)的收發(fā)工作
- 這里對幾個抽象函數(shù)特別介紹一下
public abstract void onConnect();
//當(dāng)socket成功連接的時候回調(diào)此函數(shù)刷新UI界面
public abstract void onConnectBreak();
//當(dāng)連接意外斷開的時候回調(diào)此函數(shù)
public abstract void onReceive(String s);
//當(dāng)收到服務(wù)器端發(fā)送回來的數(shù)據(jù)時,回調(diào)此函數(shù),顯示聊天數(shù)據(jù)在UI界面
public abstract void onConnectFalied();
//開始連接未完成,拋出異常時回調(diào)此函數(shù)
public abstract void onSendSuccess(String s);
//當(dāng)socket 服務(wù)器端接收到數(shù)據(jù)后將發(fā)送的數(shù)據(jù)顯示在客戶端界面
package com.houxy.chat;
import android.util.Log;
import java.io.IOException;
import java.net.Socket;
/**
* Created by asus on 2016/3/23.
*/
public abstract class TcpClient implements Runnable{
private String IP;
private int port;
boolean connect = false;
private SocketTransceiver transceiver;
private Socket socket;
public void connect(String IP, int port){
this.IP = IP;
this.port = port;
new Thread(this).start();
}
@Override
public void run() {
try {
transceiver = new SocketTransceiver() {
@Override
public void onReceive(String s) {
TcpClient.this.onReceive(s);
}
@Override
public void onConnectBreak() {
TcpClient.this.onConnectBreak();
}
@Override
public void onSendSuccess(String s) {
TcpClient.this.onSendSuccess(s);
}
};
socket = new Socket(IP, port);
connect = true;
Log.d("connect", String.valueOf(connect));
this.onConnect();
transceiver.start(socket);
} catch (IOException e) {
this.onConnectFalied();
e.printStackTrace();
}
}
protected boolean isConnected(){
return connect;
}
public void disConnected(){
connect = false;
if(transceiver!=null){
transceiver.stop();
transceiver=null;
}
}
public SocketTransceiver getTransceiver(){
return transceiver;
}
public abstract void onConnect();
public abstract void onConnectBreak();
public abstract void onReceive(String s);
public abstract void onConnectFalied();
public abstract void onSendSuccess(String s);
}
SocketTransceiver.java
- 此類主要是進(jìn)行數(shù)據(jù)的收發(fā)
- 這里的幾個抽象方法同上
package com.houxy.chat;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
/**
* Created by asus on 2016/3/23.
*/
public abstract class SocketTransceiver implements Runnable{
private Socket socket;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private InputStream inputStream;
private OutputStream outputStream;
private boolean runFlag = false;
public void start(Socket socket){
this.socket = socket;
runFlag = true;
new Thread(this).start();
}
public void stop(){
runFlag = false;
try {
socket.shutdownInput();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
} catch (IOException e) {
runFlag = false;
e.printStackTrace();
}
while (runFlag){
//線程死循環(huán) 不停的從服務(wù)器端讀回?cái)?shù)據(jù),并通過onReceiver()方法回傳顯示到UI
dataInputStream = new DataInputStream(inputStream);
try {
String s = dataInputStream.readUTF();
this.onReceive(s);
} catch (IOException e) {
runFlag = false;
e.printStackTrace();
}
}
//斷開連接
try {
inputStream.close();
outputStream.close();
socket.close();
dataInputStream.close();
if (dataOutputStream != null)
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
this.onConnectBreak();//連接被動斷開
}
public void sendMSG(String s){
if( outputStream != null){
dataOutputStream = new DataOutputStream(outputStream);
try {
dataOutputStream.writeUTF(s);
dataOutputStream.flush();
this.onSendSuccess(s);
} catch (IOException e) {
onConnectBreak();
runFlag = false;
e.printStackTrace();
}
}
}
public abstract void onReceive(String s);
public abstract void onConnectBreak();
public abstract void onSendSuccess(String s);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者