基於TCP/IP的手機聊天遊戲(附帶原始碼和解釋)之客戶端類
客戶端很簡單,就是開一個執行緒處理使用者的資料傳送和接收,並做出相應的介面處理。
由於其簡單,我就不再羅嗦,看程式碼:
MIDlet類:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
/**
* @author 孫東風
*
**/
public class ChatMIDlet extends MIDlet implements Runnable,CommandListener{
private static final String SERVER_IP = "127.0.0.1";
private Command exit = new Command("exit",Command.EXIT,1);
private GameScreen screen;
private DataInputStream in;
private DataOutputStream out;
private StreamConnection conn;
private boolean done;
private int player_id;
public static Display display;
Form login_form = new Form("登陸介面");
TextField name_textfield = new TextField("請輸入呢稱 :","",10,TextField.ANY);
Command loginCommand = new Command("登陸",Command.SCREEN,1);
static String name;
public ChatMIDlet() {
super();
login_form.append(name_textfield);
login_form.addCommand(loginCommand);
login_form.setCommandListener(this);
display = Display.getDisplay(this);
}
protected void startApp(){
try {
conn = (StreamConnection) Connector.open("socket://"+SERVER_IP+":"+Server.PORT);
in = new DataInputStream(conn.openInputStream());
out = new DataOutputStream(conn.openOutputStream());
} catch (IOException e) {
closeConnection();
System.out.println("*** could not connect to server: " + e);
destroyApp(true);
}
Display.getDisplay(this).setCurrent(login_form);
}
public DataInputStream getInput(){
return in;
}
public DataOutputStream getOutput(){
return out;
}
//關閉所有資源
private void closeConnection() {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (conn != null) {
conn.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean bool){
System.out.println("MidpTestClient.destroyApp()");
Message msg = new Message(Message.SIGNOFF, Message.NO_VALUE,null);
try {
msg.archive(out);
} catch (IOException e) {
}
closeConnection();
Display.getDisplay(this).setCurrent(null);
}
public void handleStatus(Message msg){
GameScreen.revStr = msg.getStr();
screen.repaint();
}
public void handleError(){
Message msg = new Message(Message.SIGNOFF, Message.NO_VALUE,null);
try {
msg.archive(out);
} catch (IOException e) {
e.printStackTrace();
}
}
public void handleUnknown(Message msg){
System.out.println("received unknown message: " + msg);
}
public void run() {
Message msg;
while (!done) {
try {
msg = Message.createFromStream(in);
} catch (IOException e) {
System.out.println("cant read message from stream");
continue;
}
switch (msg.getType()) {
case Message.SERVER_STATUS:
System.out.println("Client receive SERVER_STATUS");
handleStatus(msg);
break;
case Message.ERROR:
handleError();
break;
default:
handleUnknown(msg);
break;
}
}
}
public void commandAction(Command cmd, Displayable g) {
if (cmd == exit) {
done = true;
destroyApp(true);
notifyDestroyed();
}else if(cmd == loginCommand){
if(name_textfield.getString().length() != 0){
name = name_textfield.getString();
Message msg = new Message(Message.SIGNUP,Message.NO_VALUE,name_textfield.getString());
try{
msg.archive(out);
msg = Message.createFromStream(in);
if (msg.getType() != Message.SIGNUP_ACK) {
System.out.println("*** could not sign up: " + msg);
destroyApp(true);
}
player_id = Message.player_id;
System.out.println("received sign-up ack, id = " + player_id);
System.out.println("--------------1");
screen = new GameScreen();
screen.initialize(this, player_id);
done = false;
Thread thread = new Thread(this);
thread.start();
Display.getDisplay(this).setCurrent(screen);
}catch(Exception e){
System.out.println("*** could not sign up with server: " + e);
destroyApp(true);
}
}else{
Alert alert = new Alert("警告","使用者名稱和密碼不能為空",null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
}
}
GameScreen類:
import javax.microedition.lcdui.*;
public class GameScreen extends Canvas implements CommandListener{
public Form message_form = new Form("Send Message Form");
public Command sendCommand = new Command("Send",Command.OK,1);
public Command sendCommand2 = new Command("Send",Command.OK,1);
public TextField content_textfield = new TextField("Content :","",10,TextField.ANY);
public String content;
public static String revStr = "null";
public int player_id;
ChatMIDlet chatmidlet;
public GameScreen(){
message_form.append(content_textfield);
message_form.addCommand(sendCommand2);
message_form.setCommandListener(this);
this.addCommand(sendCommand);
this.setCommandListener(this);
}
public void initialize(ChatMIDlet midlet,int player_id){
this.chatmidlet = midlet;
this.player_id = player_id;
}
protected void paint(Graphics g) {
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xffffff);
g.drawString(revStr,0,0,Graphics.LEFT|Graphics.TOP);
}
public void commandAction(Command cmd, Displayable g) {
if(cmd == sendCommand){
System.out.println("CommandListenning this");
ChatMIDlet.display.setCurrent(message_form);
}else if(cmd == sendCommand2){
content = content_textfield.getString();
Message msg = new Message(Message.CLIENT_STATUS,player_id,content);
try{
msg.archive(chatmidlet.getOutput());
}catch(Exception e){
e.printStackTrace();
System.out.println("Send Message Failed!");
}
ChatMIDlet.display.setCurrent(this);
}
}
}
後話:希望此文能為3G到來之前吹點熱風,催化催化。
效果圖如下:
輸入呢稱並傳送到Server端
輸入聊天內容
顯示呢稱和說話內容
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=775438
相關文章
- 基於TCP/IP的手機聊天遊戲(附帶原始碼和解釋)之共享類TCP遊戲原始碼
- 基於TCP/IP的手機聊天遊戲(附帶原始碼和解釋)之伺服器端類TCP遊戲原始碼伺服器
- 基於c語言的TCP客戶端、服務端基礎程式碼C語言TCP客戶端服務端
- 基於node的tcp客戶端和服務端的簡單通訊TCP客戶端服務端
- Telegram原始碼之安卓客戶端配置原始碼安卓客戶端
- TCP/UDP簡易通訊框架原始碼,支援輕鬆管理多個TCP服務端(客戶端)、UDP客戶端TCPUDP框架原始碼服務端客戶端
- 【基於OpenAI的ChatGPT】搭建屬於自己的微信聊天機器人(附帶註冊英國手機號碼方式)OpenAIChatGPT機器人
- 「完整案例」基於Socket開發TCP傳輸客戶端TCP客戶端
- 初識Modbus TCP/IP-------------C#編寫Modbus TCP客戶端程式(二)TCPC#客戶端
- Nginx基於客戶端請求頭的訪問分類Nginx客戶端
- TCP程式設計之服務端和客戶端的開發TCP程式設計服務端客戶端
- 查詢SQLServer的客戶端IP和主機名SQLServer客戶端
- 基於Nodejs的Tcp封包和解包NodeJSTCP
- Spring Cloud系列(四):Eureka原始碼解析之客戶端SpringCloud原始碼客戶端
- JavaScript判斷手機、平板電腦客戶端程式碼JavaScript客戶端
- IM即時通訊設計 高併發聊天服務:伺服器 + qt客戶端(附原始碼)伺服器QT客戶端原始碼
- OpenSNS開源社交系統+基於ThinkPHP 5.3以上+PC端+客戶端+手機網頁版PHP客戶端網頁
- Tars-Java客戶端原始碼分析Java客戶端原始碼
- pomelo獲取客戶端IP客戶端
- Android-TCP客戶端的實現AndroidTCP客戶端
- 基於WebSocket的modbus通訊(二)- 客戶端Web客戶端
- ecshop仿小米商城原始碼 帶手機移動端原始碼
- FtpWebRequest類_ftp客戶端FTPWeb客戶端
- TCP通訊客戶端和服務端簡單程式碼實現TCP客戶端服務端
- JS獲取客戶端IP地址與機器名JS客戶端
- rsync備份【基於客戶端與服務端】客戶端服務端
- 建立一個Twisted Reactor TCP客戶端ReactTCP客戶端
- python建立tcp服務端和客戶端PythonTCP服務端客戶端
- golang實現tcp客戶端服務端程式GolangTCP客戶端服務端
- 笨辦法學C 練習45:一個簡單的TCP/IP客戶端TCP客戶端
- MapReduce——客戶端提交任務原始碼分析客戶端原始碼
- Java中OpenAI API客戶端原始碼教程JavaOpenAIAPI客戶端原始碼
- c# 獲取客戶端IPC#客戶端
- 獲取客戶端真實IP客戶端
- 基於c語言的UDP客戶端、服務端二合一基礎程式碼C語言UDP客戶端服務端
- 使用java語言基於SMTP協議手寫郵件客戶端Java協議客戶端
- Java Netty伺服器客戶端聊天示範程式碼JavaNetty伺服器客戶端
- Dubbo原始碼解析之客戶端初始化及服務呼叫原始碼客戶端