亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

基于TCP/IP的手機聊天游戲(附帶源碼和解釋)之客

系統 2208 0

客戶端很簡單,就是開一個線程處理用戶的數據發送和接收,并做出相應的界面處理。

由于其簡單,我就不再羅嗦,看代碼:

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到來之前吹點熱風,催化催化。

效果圖如下:

基于TCP/IP的手機聊天游戲(附帶源碼和解釋)之客戶端類

輸入呢稱并傳送到Server端

基于TCP/IP的手機聊天游戲(附帶源碼和解釋)之客戶端類

輸入聊天內容

基于TCP/IP的手機聊天游戲(附帶源碼和解釋)之客戶端類

顯示呢稱和說話內容

基于TCP/IP的手機聊天游戲(附帶源碼和解釋)之客戶端類


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 婷婷夜夜躁天天躁人人躁 | 精品视频 久久久 | 亚洲欧美精品国产一区色综合 | 日韩精品成人免费观看 | 超级碰碰青草久热国产 | 国产人成精品免费视频 | www.四虎在线观看 | 精品看片 | 91精品国产美女福到在线不卡 | 久久成人国产精品免费 | 永久免费精品影视网站 | 成年女人色毛片免费看 | 精品欧美一区二区三区 | 桃花阁成人网在线观看 | 国产一区二区在线视频观看 | 特黄特级a级黄毛片免费观看多人 | 亚洲一区二区三区影院 | 青青青国产精品国产精品久久久久 | 天天舔天天干 | 国产视频毛片 | 日韩高清欧美精品亚洲 | 精品日韩一区二区三区 | 老司机亚洲精品影院在线 | 亚洲综合网址 | 亚洲国产精品视频 | 九九九九热精品免费视频 | 中文字幕在线激情日韩一区 | 国产亚洲精品精品国产亚洲综合 | 欧美成人免费在线视频 | 午夜时刻免费实验区观看 | 2020国产精品永久在线观看 | 成人免费视频在线 | 久久国产视频网 | 成人短视频视频在线观看网站 | 91视频香蕉视频 | 欧美视频在线一区二区三区 | 国产精品久久久久毛片真精品 | 苦瓜se影院在线视频网站 | 爱爱小视频在线观看网站 | 韩国午夜影院 | xx色综合 |