收藏 分销(赏)

两个模拟器socket通讯之服务端.docx

上传人:仙人****88 文档编号:12004869 上传时间:2025-08-26 格式:DOCX 页数:4 大小:13.97KB 下载积分:10 金币
下载 相关 举报
两个模拟器socket通讯之服务端.docx_第1页
第1页 / 共4页
两个模拟器socket通讯之服务端.docx_第2页
第2页 / 共4页


点击查看更多>>
资源描述
package com.tokonow; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import .ServerSocket; import .Socket; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.KeyEvent; 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 { Intent intent0 = new Intent(); public static final String SERVERIP = "127.0.0.1";// 这个地址其实没有用到 public static final int SERVERPORT = 8191; // 注意这里和上面重定向的配置是有关系的。 private DataInputStream in; private DataOutputStream out; private Handler messageHandler; String minput = null; private Thread mThread = null; ServerSocket serverSocket; String moutput=""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chat); Bundle bundle = new Bundle(); bundle = getIntent().getExtras(); String name = bundle.getString("name"); TextView text1 = null; text1 = (TextView) findViewById(R.id.tittle); final EditText input = (EditText) findViewById(R.id.chat_input); text1.setText("与" + name + "聊天中"); // new ServerThread().start(); // 得到当前线程的Looper实例,由于当前线程是UI线程也可以通过Looper.getMainLooper()得到 Looper looper = Looper.myLooper(); // 此处甚至可以不需要设置Looper,因为 Handler默认就使用当前线程的Looper messageHandler = new MessageHandler(looper); try { serverSocket = new ServerSocket(SERVERPORT); Socket client = serverSocket.accept(); System.out.println("S: Receiving...\n" + client.getInetAddress()); client.getInetAddress(); in = new DataInputStream(client.getInputStream()); out = new DataOutputStream(client.getOutputStream()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Button send = (Button) findViewById(R.id.send); send.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub minput = input.getText().toString(); try { out.writeUTF(minput); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); mThread = new Thread(mRunnable); mThread.start(); } private Runnable mRunnable = new Runnable() { public void run() { while (true) { try { byte buffer[] = new byte[1024]; in.read(buffer); String msg; msg = new String(buffer, "GB2312").trim(); if (msg != null) { Message message = Message.obtain(); message.obj = msg; messageHandler.sendMessage(message); // 发送消息 } } catch (Exception e) { // Log.e(DEBUG_TAG, e.toString()); } } } }; class MessageHandler extends Handler { public MessageHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { // 处理收到的消息,把天气信息显示在title上 TextView text = (TextView) findViewById(R.id.chat_output); moutput+=(String) msg.obj+"\n"; text.setText(moutput); text.setTextColor(Color.BLACK); // setTitle((String) msg.obj); } } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { try { if (out != null) out.close(); if (in != null) in.close(); // if(mSocket!=null)mSocket.close(); } catch (Exception e) { e.printStackTrace(); } this.finish(); return true; } return super.onKeyDown(keyCode, event); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服