- 浏览: 25522 次
- 性别:
- 来自: 武汉
最新评论
需要用到的权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
service端
public class MainActivity extends Activity { private Button start = null; private EditText bufferText = null; private Button send = null; private ServerThread serverThread = null; private String sendBuffer = null; private String receiveBuffer = null; private TcpSocketServer tss = null; private TextView receiveView = null; private Handler handler = new Handler(){//线程与UI交互更新界面 public void handleMessage(Message msg){ receiveView.setText(receiveBuffer); Toast.makeText(MainActivity.this, receiveBuffer, Toast.LENGTH_SHORT).show(); } }; private String intToIp(int i) { return (i & 0xFF ) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ( i >> 24 & 0xFF) ; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取wifi服务 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); //判断wifi是否开启 if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(ipAddress); TextView tv=(TextView) findViewById(R.id.ip); tv.setText("本机IP:"+ip); receiveView = (TextView)this.findViewById(R.id.receiveID); start = (Button) this.findViewById(R.id.startID); //监听服务器开启 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { if(serverThread == null){ EditText portEditText = (EditText)MainActivity.this.findViewById(R.id.portID); String port = portEditText.getText().toString().trim(); serverThread = new ServerThread(port); serverThread .start(); Toast.makeText(MainActivity.this, port, Toast.LENGTH_SHORT).show(); } }}); send = (Button)this.findViewById(R.id.sendID); bufferText = (EditText)this.findViewById(R.id.bufferID); //监听发送信息 send.setOnClickListener(new OnClickListener() { public void onClick(View v){ sendBuffer = bufferText.getText().toString().trim(); if(sendBuffer != null)//为了避免线程把它弄为buffer = null; Toast.makeText(MainActivity.this, sendBuffer, Toast.LENGTH_SHORT).show(); } }); } class ServerThread extends Thread{ private int port; public ServerThread (String port){ this.port = Integer.parseInt(port); } public void run(){ //建立服务端 if(tss == null) tss = new TcpSocketServer(this.port); new Thread(new WriteThread()).start();//开启“写”线程 new Thread(new ReadThread()).start();//开启“读”线程 } private class ReadThread implements Runnable{ public void run(){ while(true){ if((receiveBuffer = tss.getMessage()) != null){//收到不为null的信息就发送出去 handler.sendEmptyMessage(0); } } } } private class WriteThread implements Runnable{ public void run(){ while(true){ try { //发送数据 if(sendBuffer != null){ //tss.sendMessage(1821,buffer); tss.sendMessage(sendBuffer); sendBuffer = null;//清空,不让它连续发 } } catch (Exception e) { e.printStackTrace(); } } } } } }
public class TcpSocketServer { private ServerSocket ss =null; private Socket s = null; private OutputStream out = null; private InputStream in = null; private String receiveBuffer = null; public TcpSocketServer(int port){ //新建ServerSocket对象,端口为传进来的port; try { //ss= new ServerSocket(1821); System.out.print("no"); ss = new ServerSocket(port); System.out.print("yes"); s = ss.accept(); out = s.getOutputStream(); in = s.getInputStream(); } catch (IOException e) { e.printStackTrace(); } } public void sendMessage(String buffer)throws Exception{ //新建Socket通信对象,接受客户端发来的请求accept(); //Socket s = ss.accept(); //创建输入流对象InputStream InputStream bais = new ByteArrayInputStream(buffer.getBytes()); byte[] buff = new byte[1024]; bais.read(buff); out.write(buff); out.flush(); } public String getMessage(){ byte[] temp = new byte[1024]; try{ if(in.read(temp) > 0) { return receiveBuffer = new String(temp).trim(); }} catch (IOException e) { e.printStackTrace(); } return null; } public String receiveMessage(){ return null; } }
client端
public class MainActivity extends Activity { private EditText ipEdit = null; private EditText portEdit = null; private EditText buffEdit = null; private Button startButton = null; private Button sendButton = null; private TextView receiveView = null; private Socket s = null; private byte[] receiveBuffer = new byte[1024]; private String sendBuffer = new String(); private String ip = null; private int port ; private int cmdCount = 0; private Handler handler = new Handler(){//线程与UI交互更新界面 public void handleMessage(Message msg){ receiveView.setText(new String(receiveBuffer).trim()); Arrays.fill(receiveBuffer, (byte)0);//清空 } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.init(); /*开启socket通信*/ startButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(s == null){//这里要设置验证!!!!!!!!! /*设定ip和port*/ ip = ipEdit.getText().toString(); port = Integer.parseInt(portEdit.getText().toString()); /*开启socket线程*/ new Thread(new SocketClientControl(ip,port)).start(); } Toast.makeText(MainActivity.this, "服务器连接成功", Toast.LENGTH_SHORT).show(); } }); /*发送数据*/ sendButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if(s != null) sendBuffer = buffEdit.getText().toString(); Toast.makeText(MainActivity.this, "send -> "+sendBuffer, Toast.LENGTH_SHORT).show(); } }); } private void init(){ startButton = (Button) this.findViewById(R.id.startID); sendButton = (Button) this.findViewById(R.id.sendID); ipEdit = (EditText) this.findViewById(R.id.ipID); portEdit = (EditText) this.findViewById(R.id.portID); buffEdit = (EditText) this.findViewById(R.id.buffID); receiveView = (TextView) this.findViewById(R.id.recieiveID); } private class SocketClientControl implements Runnable{ private InputStream in = null; private OutputStream out = null; public SocketClientControl(){ } public SocketClientControl(String ip,int port){ try { s = new Socket(ip,port);//获得链接 in = s.getInputStream();//获得输入流 out = s.getOutputStream();//获得输出流 } catch (IOException e) { e.printStackTrace(); System.exit(-1);//要是出问题,就线程退出 } } public void run(){ new Thread(new WriteThread()).start();//开启“写”线程 new Thread(new ReadThread()).start();//开启“读”线程 } private class ReadThread implements Runnable{ public void run() { while(true){ try { if(in.read(receiveBuffer) > 0){//等待命令的输入 cmdCount ++; ; handler.sendEmptyMessage(0);//发送信息,更新UI } } catch (IOException e) { e.printStackTrace(); } } } } private class WriteThread implements Runnable{ public void run(){ while(true){ if(!sendBuffer.equals("")){ try { out.write(sendBuffer.getBytes());//输出 out.flush();//输出刷新缓冲 } catch (IOException e) { e.printStackTrace(); } sendBuffer = ""; } } } } } }
public class SocketClientSingle { private static Socket s = null; private SocketClientSingle() { } public static Socket getSocket(String ip,int port){ try { s = new Socket(ip,port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return s; } }
发表评论
-
ImageLoaderTools图片异步双缓存下载工具
2014-09-12 11:31 664package com.cnki.client.utils; ... -
CookiesTools 网络通讯工具类
2014-09-12 11:29 591package com.cnki.client.utils; ... -
BitmapTools图片处理工具类
2014-09-12 11:25 971package com.cnki.client.utils; ... -
自动更新版本
2014-09-12 11:21 560Android 应用自动更新代码 import java.i ... -
Android 获取ip地址
2013-07-24 15:41 805获取ip地址 1.使用WIFI 首先设置用户权限 <us ... -
Android网络连接判断与处理
2013-07-17 17:20 737获取网络信息需要在AndroidManifest.xml文件中 ... -
判断当前网络是否可用和调用系统设置wifi界面
2013-07-17 16:48 799需要用到的权限 <uses-permission and ... -
获取系统版本内核版本信息
2013-07-09 12:55 2193private static final String L ...
相关推荐
局域网内实现WIFI的两种方法:交换机底下接无线路由器实现WIFI功能
手机通过wifi无线连接到一个局域网,实现了同一个局域网的视频和图片的相互传送。
可以实现手机之间连接WIFI情况下局域网实时传送文件,支持自建WIFI热点
1:两个手机需要在同一个局域网里面 2:修改客户端ClientTest里面的IP地址,IP地址设置为作为服务端的手机的IP地址 3:androidSocketTest 为服务端,要安装到一个手机里并先运行起来,会显示本机IP地址,显示Client传...
两个手机之间必须连接同一个局域网,IP为局域网的IP
本项目是一个安卓手机之间通过wifi通讯的项目源码包括安卓...需要两个安卓端都在一个局域网内并且先启动服务端两台手机wifi互连.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
本程序是利用java实现了UDP下的局域网通信,并打包成了EXE可执行文件,不需要java环境就可以直接运行在自己的电脑上,是学习网络通信的很好的例子
使用Cisco Packet Tracer实现两个局域网通信,亲自测试可以使用
如何实现在win7环境下的,局域网内打印机的共享
TCP实现局域网聊天通讯 使用WPF界面 可以实现聊天
wifi局域网聊天,可以实现手机搜索热点和创建热点,内容丰富
分析了网络即时通信工具的特点,结合了现在校园局域网的的发展规模,提出了基于J2SE的即时通信工具的设计,分析了该系统的功能模型和主要技术,论述在C/S体系结构下,设计一种局域网即时通信工具。主要功能为在线即时...
无线局域网共享,可以帮助你共享同一个局域网
stm32+esp8266实现局域网下小灯控制源码。 1,串口写指令 通过stm32串口2发送AT指令给esp8266,此时可以通过串口一查看配置的AT指令。可以在程序中找到对应的语句 具体指令 AT AT+RST AT+CWMODE_CUR=1 //模式选择 AT...
为实现局域网环境中的即时通讯,方便局域网用户的资源共享,设计并实现局域网用户的即时语音通讯工具。该软件的网络通信采用TCP/IP( Transmission Control Protocol/Internet Protocol)协议,程序架构采用基于C/S( ...
1. 熟悉WinSock API 主要函数的使用; 2. 掌握相应开发工具对...此系统通过VC平台MFC基于对话框程序的编程实现,利用WinSock API开发的基于TCP/IP的局域网网络即时通讯工具,并通过友好的界面来实现基本数据的网络传输。
UDP 局域网异步通讯实现广播,接收发送string数据 用户就可以在一个局域网中聊天了。
利用AsyncUdpSocket实现局域网下的IM
Android 客户端项目源码,平板类型,基于WiFi的Android局域网即时通讯软件
们平常都会使用蓝牙来进行无线...使用Wifi无线工具在Android手机与PC机之间进行数据传输,只要在同一无线局域网环境下。由于采用WiiFi方式联网,速度比蓝牙传输更快,现在就算没USB线,也可以直接传资料了,非常方便。