目前想要做一個良葛格之前有做過的多人聊天的Java程式
目前卡在說傳過來的訊息都是亂碼(不知道為什麼?)
還有不知道怎樣寫關調視窗就結束skt連接的方法(找了一段時間找不到)
以下為我修改良葛格client端的程式碼套用在android上的
package com.demo.android.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import android.os.Handler;
public class ChatClientSocket extends Thread{
private Socket skt; // 客戶端連線Socket物件
private InetAddress host; // 指定的伺服端IP
private int port; // 指定的伺服端連接埠
private BufferedReader theInputStream;
private PrintStream theOutputStream;
private Chat chatBox;
private static String message;
public ChatClientSocket(String ip, int port) {
try {
// 取得伺服端的InetAddress物件、通訊連接埠
host = InetAddress.getByName(ip);
this.port = port;
}
catch (IOException e) {
e.printStackTrace();
}
}
// 指定這個Socket的訊息觀察者
public void setMessageObserver(Chat box) {
chatBox = box;
}
public String getMessage() {
return message;
}
public void run() {
try {
message = "嘗試連線......";
chatBox.UpdateTextView();
// 建立Socket物件並嘗試連線
skt = new Socket(host, port);
message = "連線成功\n";
chatBox.UpdateTextView();
// 從InputStream建立讀取緩衝區
theInputStream = new BufferedReader(
new InputStreamReader(skt.getInputStream()));
// 從OutputStream中建立PrintStream物件
theOutputStream = new PrintStream(skt.getOutputStream());
// 讀取資料迴圈
while((message = theInputStream.readLine()) != null) {
// 將之顯示在TextView上
message = ": " + message + "\n";
chatBox.UpdateTextView();
}
if(message == null) {
skt.close();
message = "連線中斷!\n";
chatBox.UpdateTextView();
}
}catch (IOException e) {
message = e.toString();
chatBox.UpdateTextView();
}
}
//將要傳送的訊息傳給server端
public void dataOutput(String data) {
theOutputStream.println(data);
}
}
package com.demo.android.chat;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Chat extends Activity {
/** Called when the activity is first created. */
private Button go; //發送訊息的按鈕
private Button clientBtn; //連接伺服器的按鈕
private Button serverBtn;
private EditText tfAddress ; //輸入IP
private EditText tfPort ; //輸入Port
private EditText tfChat ; //輸入要傳送的訊息
private TextView result ; //伺服器端傳送的訊息display在這
private ChatClientSocket clientSkt; //客戶端連線處理執行緒
public static Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
public void run() {
update();
}
};
public void UpdateTextView(){
mHandler.post(mUpdateResults);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Listen for button clicks
result = (TextView) findViewById(R.id.textview);
tfAddress =(EditText)findViewById(R.id.ip);
tfPort = (EditText)findViewById(R.id.port);
tfChat = (EditText)findViewById(R.id.chat);
clientBtn = (Button)findViewById(R.id.connect);
go = (Button)findViewById(R.id.go_btn);
clientBtn.setOnClickListener(connectserver);
go.setOnClickListener(goserver);
}
//連接伺服器
private OnClickListener connectserver = new OnClickListener(){
public void onClick(View v){
setClient();
}
};
//傳送訊息給伺服器
private OnClickListener goserver = new OnClickListener(){
public void onClick(View v){
// 將資料透過連線執行緒送出
clientSkt.dataOutput(tfChat.getText().toString());
// 清除下方文字欄位內容
tfChat.setText("");
}
};
public void setServerBtn(Button serverBtn) {
this.serverBtn = serverBtn;
}
public Button getServerBtn() {
return serverBtn;
}
//對textView進行更新
private void update() {
result.append(clientSkt.getMessage());
}
private void setClient() {
// 取得指定的IP與連接埠
int port = Integer.parseInt(tfPort.getText().toString());
// 建立客戶端連線執行緒
clientSkt = new ChatClientSocket(tfAddress.getText().toString(), port);
clientSkt.setMessageObserver(this);
// 啟動執行緒進行連線
clientSkt.start();
}
}