安卓手机socket通信(服务器和客户端)

安卓手机socket通信(服务器和客户端),第1张

概述本文实例为大家分享了安卓手机socket通信代码,供大家参考,具体内容如下1、socket通信首先要定义好服务端的ip地址和端口号; 

本文实例为大家分享了安卓手机socket通信代码,供大家参考,具体内容如下

1、socket通信首先要定义好服务端的ip地址和端口号; 

(1).首先看服务端的代码:

package com.example.androIDsockettest;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.io.OutputStream;import java.net.ServerSocket;import java.net.socket;import androID.net.wifi.WifiInfo;import androID.net.wifi.WifiManager;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.app.Activity;import androID.content.Context;import androID.vIEw.Menu;import androID.Widget.TextVIEw;public class MainActivity extends Activity { public static ServerSocket serverSocket = null; public static TextVIEw mTextVIEw,textVIEw1;  private String IP = "";  String buffer = ""; public static Handler mHandler = new Handler() { @OverrIDe public voID handleMessage(androID.os.Message msg) {  if (msg.what==0x11) {  Bundle bundle = msg.getData();  mTextVIEw.append("clIEnt"+bundle.getString("msg")+"\n");  } }; }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mTextVIEw = (TextVIEw) findVIEwByID(R.ID.textsss); textVIEw1 = (TextVIEw) findVIEwByID(R.ID.textVIEw1); IP = getlocalip(); textVIEw1.setText("IP addresss:"+IP); new Thread() {  public voID run() {  Bundle bundle = new Bundle();  bundle.clear();  OutputStream output;  String str = "通信成功";  try {   serverSocket = new ServerSocket(30000);   while (true) {   Message msg = new Message();   msg.what = 0x11;   try {    Socket socket = serverSocket.accept();    output = socket.getoutputStream();    output.write(str.getBytes("UTF-8"));    output.flush();    socket.shutdownOutput();    //mHandler.sendEmptyMessage(0);    BufferedReader bff = new BufferedReader(new inputStreamReader(socket.getinputStream()));    String line = null;    buffer = "";    while ((line = bff.readline())!=null) {    buffer = line + buffer;    }    bundle.putString("msg",buffer.toString());    msg.setData(bundle);    mHandler.sendMessage(msg);    bff.close();    output.close();    socket.close();   } catch (IOException e) {    e.printstacktrace();   }   }  } catch (IOException e1) {   // Todo auto-generated catch block   e1.printstacktrace();  }  }; }.start(); } private String getlocalip(){     WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);       WifiInfo wifiInfo = wifiManager.getConnectionInfo();       int ipAddress = wifiInfo.getIpAddress();      // Log.d(Tag,"int ip "+ipAddress);      if(ipAddress==0)return null;      return ((ipAddress & 0xff)+"."+(ipAddress>>8 & 0xff)+"."         +(ipAddress>>16 & 0xff)+"."+(ipAddress>>24 & 0xff));    }  } 

(2).因为是手机做服务端,所以在开始 *** 作的时候客户端是不知道ip和端口号的,但在服务端运行后就可以看到(亲:你可以自己测试) 

2、客户端的代码 

package com.example.andoroIDclIEnt;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStreamReader;import java.io.OutputStream;import java.net.InetSocketAddress;import java.net.socket;import java.net.socketTimeoutException;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.app.Activity;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;public class MainActivity extends Activity { Socket socket = null; String buffer = ""; TextVIEw txt1; button send; EditText ed1; String geted1; public Handler myHandler = new Handler() { @OverrIDe public voID handleMessage(Message msg) {  if (msg.what == 0x11) {  Bundle bundle = msg.getData();  txt1.append("server:" + bundle.getString("msg") + "\n");  } } }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); txt1 = (TextVIEw) findVIEwByID(R.ID.txt1); send = (button) findVIEwByID(R.ID.send); ed1 = (EditText) findVIEwByID(R.ID.ed1); new MyThread("建立连接").start(); send.setonClickListener(new OnClickListener() {  @OverrIDe  public voID onClick(VIEw v) {  geted1 = ed1.getText().toString();  txt1.append("clIEnt:" + geted1 + "\n");  // 启动线程 向服务器发送和接收信息  new MyThread(geted1).start();  } }); } class MyThread extends Thread { public String txt1; public MyThread(String str) {  txt1 = str; } @OverrIDe public voID run() {  // 定义消息  Message msg = new Message();  msg.what = 0x11;  Bundle bundle = new Bundle();  bundle.clear();  try {  // 连接服务器 并设置连接超时为5秒  socket = new Socket();  socket.connect(new InetSocketAddress("172.20.226.11",30000),1000);  // 获取输入输出流  OutputStream ou = socket.getoutputStream();  BufferedReader bff = new BufferedReader(new inputStreamReader(socket.getinputStream()));  // 读取发来服务器信息  String line = null;  buffer = "";  while ((line = bff.readline()) != null) {   buffer = line + buffer;  }  // 向服务器发送信息  ou.write(txt1.getBytes("gbk"));  ou.flush();  bundle.putString("msg",buffer.toString());  msg.setData(bundle);  // 发送消息 修改UI线程中的组件  myHandler.sendMessage(msg);  // 关闭各种输入输出流  bff.close();  ou.close();  socket.close();  } catch (SocketTimeoutException aa) {  // 连接超时 在UI界面显示消息  bundle.putString("msg","服务器连接失败!请检查网络是否打开");  msg.setData(bundle);  // 发送消息 修改UI线程中的组件  myHandler.sendMessage(msg);  } catch (IOException e) {  e.printstacktrace();  } } }}

3、最后别忘记加网络权限

  <uses-permission androID:name="androID.permission.INTERNET" />

源码下载:http://xiazai.jb51.net/201608/yuanma/android-socket(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的安卓手机socket通信(服务器和客户端)全部内容,希望文章能够帮你解决安卓手机socket通信(服务器和客户端)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存