天地币:所用到的 Android Socket 通讯编程技术试验
1、为了开发“天地币”这个Android手机项目,须要用到Socket编程。
2、天地币是一种类似于比特币的虚拟货币。
3、为了赚取CSDN的C币,须要写篇博客。
4、干脆将调试Socket的项目发出来跟网友分享。
闲话休提,直接上代码,首先是字符串的定义:
<? xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">xyzSocket</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="ipaddr_str">本机IP:</string>
<string name="serveripaddr_str">服务器IP:</string>
<string name="server_str">设置为服务端</string>
<string name="client_str">连接为客户端</string>
<string name="mymachine_str">本机就是</string>
<string name="send_str">发送</string>
<string name="online_str">0在线</string>
<string name="threeline_str">第1行\n第2行\n第3行\n</string> </resources>
其次是xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.xyzsocket.MainActivity$PlaceholderFragment" > <RelativeLayout
android:id="@+id/relativelayout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
> <TextView
android:id="@+id/ipaddr_label_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/ipaddr_str"
/> <EditText
android:id="@+id/myipaddr_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ipaddr_label_1"
android:layout_toLeftOf="@+id/noline_label_1"
/> <TextView
android:id="@+id/online_label_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/online_str"
/> </RelativeLayout> <RelativeLayout
android:id="@+id/relativelayout_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativelayout_1"
> <EditText
android:id="@+id/server_receiver"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:text="@string/threeline_str"
/> </RelativeLayout> <RelativeLayout
android:id="@+id/relativelayout_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativelayout_2"
> <TextView
android:id="@+id/ipaddr_label_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/serveripaddr_str"
/> <EditText
android:id="@+id/serveripaddr_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ipaddr_label_2"
android:layout_toLeftOf="@+id/serveripaddr_check"
/> <CheckBox
android:id="@+id/serveripaddr_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/mymachine_str"
/> </RelativeLayout> <RelativeLayout
android:id="@+id/relativelayout_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativelayout_3"
> <EditText
android:id="@+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/butt_send"
/> <Button
android:id="@+id/butt_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/send_str"
/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_below="@+id/send_message"
> <Button
android:id="@+id/butt_server"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_str"
android:layout_below="@+id/send_message"
/> <Button
android:id="@+id/butt_client"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/client_str"
android:layout_below="@+id/send_message"
/> </LinearLayout> </RelativeLayout> </RelativeLayout>
最基本的是代码:
package com.example.xyzsocket; import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build; public class MainActivity extends ActionBarActivity { private EditText et01 = null;
private EditText et02 = null;
private EditText et03 = null;
private EditText et04 = null;
private Button bn01 = null;
private Button bn02 = null;
private Button bn03 = null;
private CheckBox cb01 = null;
private TextView tv01 = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} private String intToIp(int i){
return (i & 0xFF) + "." + ((i>>8) & 0xFF) + "." + ((i>>16) & 0xFF) + "." + ((i>>24) & 0xFF);
} @Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if( et01 == null ){
et01 = (EditText) findViewById(R.id.myipaddr_edit);
if(et01 != null){
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
et01.setText(intToIp(ipAddress));
}
}
if( et02 == null ){
et02 = (EditText) findViewById(R.id.server_receiver);
if(et02 != null){
}
}
if( et03 == null ){
et03 = (EditText) findViewById(R.id.serveripaddr_edit);
if(et03 != null){
}
}
if( et04 == null ){
et04 = (EditText) findViewById(R.id.send_message);
if(et04 != null){
et04.setEnabled(false);
}
}
if( bn01 == null ){
bn01 = (Button) findViewById(R.id.butt_server);
if( bn01 != null ){
bn01.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run()
{
//网络訪问的代码放在这里
server();
}
}.start();
}
});
}
}
if( bn02 == null ){
bn02 = (Button) findViewById(R.id.butt_client);
if( bn02 != null ){
bn02.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run()
{
//网络訪问的代码放在这里
client();
}
}.start();
}
});
}
}
if( bn03 == null ){
bn03 = (Button) findViewById(R.id.butt_send);
if( bn03 != null ){
bn03.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run()
{
//网络訪问的代码放在这里
sendmsg();
}
}.start();
}
});
bn03.setEnabled(false);
}
}
if( cb01 == null ){
cb01 = (CheckBox) findViewById(R.id.serveripaddr_check);
if( cb01 != null ){
cb01.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Message msg = new Message();
msg.arg1 = isChecked?1:0;
msg.what = 103;
handler.sendMessage(msg);
}
});
}
}
if( tv01 == null ){
tv01 = (TextView) findViewById(R.id.online_label_1);
if( tv01 != null ){ }
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
} private Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
myHandleMessage(msg);
} }; private void myHandleMessage(Message msg){
switch(msg.what){
case 101:
bn01.setText("服务器中止");
break;
case 102:
bn01.setText(getString(R.string.server_str));
break;
case 103:
if(msg.arg1>0){
et03.setText(et01.getText().toString());
}
break;
case 104:
bn02.setText(msg.arg1>0?"客户端中止":getString(R.string.client_str));
bn03.setEnabled(msg.arg1>0);
et04.setEnabled(msg.arg1>0);
break;
case 110:
String str = et02.getText().toString();
int i = str.indexOf("\n");
if(i>0){
str = str.substring(i+1);
}
str += (String)msg.obj;
str += "\n";
et02.setText(str);
break;
case 111:
Log.i("DEBUG", "客户端收到:" + (String)msg.obj);
break;
case 112:
Log.i("DEBUG", "服务器中止了!!");
tv01.setText("" + list.size() + "在线");
bn02.setText(getString(R.string.client_str));
bn03.setEnabled(false);
et04.setEnabled(false);
break;
case 113:
Log.i("DEBUG", "客户端退出了!!");
tv01.setText("" + list.size() + "在线");
break;
case 114:
tv01.setText("" + list.size() + "在线");
break;
}
} ServerSocket aServerSocket = null;
ArrayList list = new ArrayList(); private void server(){
myServerThread thread;
if( aServerSocket != null ){
try{
aServerSocket.close();
aServerSocket = null;
}catch(IOException e){
e.printStackTrace();
}
if( bn01 != null )
{
Message msg = new Message();
msg.what = 102;
handler.sendMessage(msg);
}
return;
}
try {
aServerSocket = new ServerSocket(55555);
Log.i("DEBUG", "already listen 55555 port.");
} catch (Exception e) {
e.printStackTrace();
}
if(aServerSocket == null){
Log.i("DEBUG", "侦听失败了!!");
return;
}
Log.i("DEBUG", "侦听成功了!!");
if( bn01 != null )
{
Message msg = new Message();
msg.what = 101;
handler.sendMessage(msg);
}
int num = 0;
while (num < 10) {
Socket aSessionSoket = null;
try {
Log.i("DEBUG", "侦听前!!");
aSessionSoket = aServerSocket.accept();
Log.i("DEBUG", "侦听后!!");
thread = new myServerThread(aSessionSoket);
thread.start();
Message msg = new Message();
msg.what = 114;
handler.sendMessage(msg);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Log.i("DEBUG", "服务器手动中止了!!");
for(int i=0;i<list.size();i++){
Socket s = (Socket)list.get(i);
try{
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
list.clear();
break;
}
num = list.size();
Log.i("DEBUG", "socket count = " + num);
}
} class myServerThread extends Thread {
public Socket aSessionSoket = null; public myServerThread(Socket socket) {
aSessionSoket = socket;
list.add(socket);
} public void run() {
DataInputStream aDataInput = null;
DataOutputStream aDataOutput = null;
try {
aDataInput = new DataInputStream(aSessionSoket.getInputStream());
aDataOutput = new DataOutputStream(aSessionSoket.getOutputStream());
while (true) {
Log.i("DEBUG", "服务器接收前!!");
String str = aDataInput.readUTF(); // read msg.
Log.i("DEBUG", "服务器接收后!!");
Message msg = new Message();
msg.obj = str;
msg.what = 110;
handler.sendMessage(msg);
aDataOutput.writeUTF("OK:" + str);
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } finally {
try {
if (aDataInput != null)
aDataInput.close();
if (aDataOutput != null)
aDataOutput.close();
list.remove(aSessionSoket);
aSessionSoket.close();
// 这里的退出是客户端主动断开
Message msg = new Message();
msg.what = 113;
handler.sendMessage(msg);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
} Socket clientsocket = null; private void client(){
Message msg;
if( clientsocket != null ){
try{
clientsocket.close();
clientsocket = null;
}catch(IOException e){
e.printStackTrace();
return;
}
msg = new Message();
msg.what = 104;
msg.arg1 = 0;
handler.sendMessage(msg);
return;
}
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName ( et03.getText().toString() );
Log.d ( "DEBUG" , "C: Connecting..." );
// 与服务器获取连接
clientsocket = new Socket(serverAddr, 55555); } catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(clientsocket == null){
Log.i("DEBUG", "连接失败了!!");
return;
}
if( bn03 != null )
{
msg = new Message();
msg.what = 104;
msg.arg1 = 1;
handler.sendMessage(msg);
}
myClientThread thread = new myClientThread(clientsocket);
thread.start();
} class myClientThread extends Thread {
public Socket aSessionSoket = null; public myClientThread(Socket socket) {
aSessionSoket = socket;
} public void run() {
DataInputStream aDataInput = null;
try {
aDataInput = new DataInputStream(aSessionSoket.getInputStream());
while (true) {
Log.i("DEBUG", "客户端接收前!!");
String str = aDataInput.readUTF();
Log.i("DEBUG", "客户端接收后!!");
Message msg = new Message();
msg.obj = str;
msg.what = 111;
handler.sendMessage(msg);
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Message msg = new Message();
msg.what = 112;
handler.sendMessage(msg); } finally {
try {
if (aDataInput != null)
aDataInput.close();
aSessionSoket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
} private void sendmsg(){
DataOutputStream aDataOutput = null;
String message = et04.getText().toString();
if(message.isEmpty()){
return;
}
try {
Log.i ( "DEBUG" , "C: Sending: '" + message + "'" );
aDataOutput = new DataOutputStream(clientsocket.getOutputStream());
aDataOutput.writeUTF(message); } catch (Exception e) {
e.printStackTrace();
} finally {
} } }
别忘了加入权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
最后简单说说使用方法:
1、软件自己主动检測到自己wifi的IP地址。
2、一台机器能够同一时候扮演server和client。
3、其它机器能够登录到同一台server。
网络通讯。就这么简单。
天地币:所用到的 Android Socket 通讯编程技术试验的更多相关文章
- Protobuf实现Android Socket通讯开发教程
本节为您介绍Protobuf实现Android Socket通讯开发教程,因此,我们需要先了理一下protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式 ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...
- android 蓝牙通讯编程 备忘
1.启动App后: 判断->蓝牙是否打开(所有功能必须在打牙打开的情况下才能用) 已打开: 启动代码中的蓝牙通讯Service 未打开: 发布 打开蓝牙意图(系统),根据Activity返回进场 ...
- Android Socket通信编程
安卓客户端通过socket与服务器端通讯一般可以按照以下几个步骤:(1).通过IP地址和端口实例化Socket,请求连接服务器:socket = new Socket(HOST, PORT); //h ...
- 门禁系统socket通讯编程
最近遇到一个socke udp协议通讯的需求,而且是16进制数据接收.这样在传输参数的时候老是提示参数错误,因为计算机是不能直接传输16进制的,会自行转换,所有以下代码非常完美的解决我的问题,同时也让 ...
- 初识Socket通讯编程(一)
一.什么是socket? 当两台计算机需要通信的时候,往往我们使用的都是TCP去实现的,但是并不会直接去操作TCP协议,通常是通过Socket进行tcp通信.Socket是操作系统提供给开发者的一个接 ...
- 高性能、高可用性Socket通讯库介绍 - 采用完成端口、历时多年调优!(附文件传输程序)
前言 本人从事编程开发十余年,因为工作关系,很早就接触socket通讯编程.常言道:人在压力下,才可能出非凡的成果.我从事的几个项目都涉及到通讯,为我研究通讯提供了平台,也带来了动力.处理socket ...
- C#网络编程技术微软Socket实战项目演练(三)
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的第三部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理 ...
- Android之okhttp实现socket通讯(非原创)
文章大纲 一.okhttp基础介绍二.socket通讯代码实战三.项目源码下载四.参考文章 一.okhttp基础介绍 https://www.jianshu.com/p/e3291b7808e7 ...
随机推荐
- webDriver API——第14部分Color Support
class selenium.webdriver.support.color.Color(red, green, blue, alpha=1) Bases: object Color conversi ...
- KVM Run Process之KVM核心流程
在"KVM Run Process之Qemu核心流程"一文中讲到Qemu通过KVM_RUN调用KVM提供的API发起KVM的启动,从这里进入到了内核空间执行,本文主要讲述内核中KV ...
- 【VBA编程】06.控制语句
[IF...THEN...语句] If condition Then [statements1] else [statements2] end if condition 为一个逻辑表达式,表示做选择时 ...
- Spring Boot(一)启动方式
1.系统自动生成 SpringApplication.run(XX.class, args); 2.创建SpringApplication对象 SpringApplication app = new ...
- 如何监控Tomcat服务器
如何监控Tomcat服务器 发表于:2009-06-25来源:作者:点击数:2651 标签:tomcatTomcat服务器 在进行 性能测试 时,一般都需要对应用服务器进行监控,监控的指标包括应用服务 ...
- hiberbnate 缓存策略概述
1. 首先了解什么是缓存 这里说的缓存并不是指计算机的内存或者CPU的一二级缓存. 缓存是指为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能的一种策略.即对物理数据源的复制,存在于内 ...
- Linux下自动备份Oracle数据库并删除指定天数前的备份
说明: Oracle数据库服务器 操作系统:CentOS IP:192.168.0.198 端口:1521 SID:orcl Oracle数据库版本:Oracle11gR2 具体操作: 1.root用 ...
- Hibernate JPA实体继承的映射(二) @MappedSuperclass
基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclass注解将实体类的多个属性分别封装到不同的非实体类中. 1.@MappedSuperclass注解只能标准在类上:@ ...
- django源码分析----Related继承结构
在django中关联关系大概可以分成many-to-one(foriegnkey).one-to-one.many-to-many 这三种.它们有如下的类结构 class RelatedField(F ...
- Atitit.词法分析的理论原理 part2
Atitit.词法分析的理论原理 part2 1. 转换图1 1.1. 转换图是由程序流程图改进而成的.同样,转换图也可以等价地转换为程序流程图3 1.2. 2.2.3 构造词法分析器(2)流程程 ...