android_Server_Socket_通信序列化实现

没事就简单写了下关于Android与JAVA Server之间的通信实现,老规矩,直接贴代码吧:

android客户端:

package com.socket.client;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.util.Enumeration;


import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ReceiveDemo extends Activity{


private TextView hostName;
private TextView port;
private TextView address;
private TextView mac;
private Button getInfo;
private Button other;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver);
findViewById();
}

private void findViewById(){
hostName = (TextView) findViewById(R.id.hostName);
port = (TextView) findViewById(R.id.port);
address = (TextView) findViewById(R.id.address);
mac = (TextView) findViewById(R.id.mac);

getInfo = (Button) findViewById(R.id.getInfo);
other = (Button) findViewById(R.id.other);
getInfo.setOnClickListener(listener);
other.setOnClickListener(listener);
}

private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.getInfo:
// getLocalMacAddress();
processThread();
break;
case R.id.other:

break;
}
}
};

private void processThread(){
try {
new Thread(new Runnable() {
@Override
public void run() {
httpConn();
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}

private int ports = 1818;
private String ip  = "192.168.1.7";
Socket s;
private void httpConn(){
try {
s = new Socket(ip, ports);
ObjectOutputStream obj = new ObjectOutputStream(s.getOutputStream());
SeieslizeBean sb = new SeieslizeBean();
sb.setHostName(s.getInetAddress().getHostName());
sb.setPorts(s.getPort());
sb.setAddress(s.getInetAddress().getHostAddress());
sb.setMac(getLocalMacAddress());

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
sb.setBssid(info.getBSSID());
sb.setClasss(info.getClass().toString());
sb.setHiddenSsid(info.getHiddenSSID());
sb.setIpAddress(info.getIpAddress());
sb.setLinkSpeed(info.getLinkSpeed());
sb.setMacAddress(info.getMacAddress());
sb.setNetWorkId(info.getNetworkId());
sb.setRssi(info.getRssi());
sb.setSsid(info.getSSID());
// sb.setSs(info.getSupplicantState());

for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()){
System.out.println(inetAddress.getHostName()+" | "+inetAddress.getHostAddress());
System.out.println(inetAddress.getCanonicalHostName().toString());
System.out.println(inetAddress.getAddress().toString());
System.out.println(inetAddress.getClass().toString());
System.out.println(inetAddress.getLocalHost().toString());
inetAddress.getHostAddress().toString();
sb.setHostName2(inetAddress.getHostName());
sb.setInetAddr2(inetAddress.getHostAddress());
sb.setCanoicalHostName(inetAddress.getCanonicalHostName());
sb.setAddres2(inetAddress.getAddress().toString());
sb.setClasss(inetAddress.getClass().toString());
sb.setLocalHost(inetAddress.getLocalHost().toString());
}
}
}

obj.writeObject(sb);
obj.flush();
// obj.close();

ObjectInputStream in = new ObjectInputStream(s.getInputStream());
SeieslizeBean line = (SeieslizeBean) in.readObject();
System.out.println(line.toString());
} catch (Exception e) {
try {
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();

}

private String getLocalMacAddress(){
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String str = info.getMacAddress();
System.out.println(info.getBSSID());
System.out.println(info.getClass());
System.out.println(info.getHiddenSSID());
System.out.println(info.getIpAddress());
System.out.println(info.getLinkSpeed());
System.out.println(info.getMacAddress());
System.out.println(info.getNetworkId());
System.out.println(info.getRssi());
System.out.println(info.getSSID());
System.out.println(info.getSupplicantState());
getLocalIpAddress();
return str;
}

private String getLocalIpAddress(){
try {
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()){
System.out.println(inetAddress.getHostName()+" | "+inetAddress.getHostAddress());
System.out.println(inetAddress.getCanonicalHostName().toString());
System.out.println(inetAddress.getAddress().toString());
System.out.println(inetAddress.getClass().toString());
System.out.println(inetAddress.getLocalHost().toString());
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
}
return null;
}
}


package com.socket.client;


import java.io.Serializable;


public class SeieslizeBean implements Serializable{


private static final long serialVersionUID = 1L;


private String hostName;
private int ports;
private String address;
private String mac;

public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPorts() {
return ports;
}
public void setPorts(int ports) {
this.ports = ports;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}


private String bssid;
private String classs;
private boolean hiddenSsid;
private int ipAddress;
private int linkSpeed;
private String macAddress;
private int netWorkId;
private int rssi;
private String ssid;
// private SupplicantState ss;


public String isBssid() {
return bssid;
}
public void setBssid(String bssid) {
this.bssid = bssid;
}
public String getClasss() {
return classs;
}
public void setClasss(String classs) {
this.classs = classs;
}
public boolean isHiddenSsid() {
return hiddenSsid;
}
public void setHiddenSsid(boolean hiddenSsid) {
this.hiddenSsid = hiddenSsid;
}
public int getIpAddress() {
return ipAddress;
}
public void setIpAddress(int ipAddress) {
this.ipAddress = ipAddress;
}
public int getLinkSpeed() {
return linkSpeed;
}
public void setLinkSpeed(int linkSpeed) {
this.linkSpeed = linkSpeed;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public int getNetWorkId() {
return netWorkId;
}
public void setNetWorkId(int netWorkId) {
this.netWorkId = netWorkId;
}
public int getRssi() {
return rssi;
}
public void setRssi(int rssi) {
this.rssi = rssi;
}
public String getSsid() {
return ssid;
}
public void setSsid(String ssid) {
this.ssid = ssid;
}
/* public SupplicantState getSs() {
return ss;
}
public void setSs(SupplicantState ss) {
this.ss = ss;
}*/

private String hostName2;
private String inetAddr2;
private String canoicalHostName;
private String addres2;
private String calsss2;
private String localHost;


public String getHostName2() {
return hostName2;
}
public void setHostName2(String hostName2) {
this.hostName2 = hostName2;
}
public String getInetAddr2() {
return inetAddr2;
}
public void setInetAddr2(String inetAddr2) {
this.inetAddr2 = inetAddr2;
}
public String getCanoicalHostName() {
return canoicalHostName;
}
public void setCanoicalHostName(String canoicalHostName) {
this.canoicalHostName = canoicalHostName;
}
public String getAddres2() {
return addres2;
}
public void setAddres2(String addres2) {
this.addres2 = addres2;
}
public String getCalsss2() {
return calsss2;
}
public void setCalsss2(String calsss2) {
this.calsss2 = calsss2;
}
public String getLocalHost() {
return localHost;
}
public void setLocalHost(String localHost) {
this.localHost = localHost;
}
@Override
public String toString() {
return "SeieslizeBean [addres2=" + addres2 + ", address=" + address
+ ", bssid=" + bssid + ", calsss2=" + calsss2
+ ", canoicalHostName=" + canoicalHostName + ", classs="
+ classs + ", hiddenSsid=" + hiddenSsid + ", hostName="
+ hostName + ", hostName2=" + hostName2 + ", inetAddr2="
+ inetAddr2 + ", ipAddress=" + ipAddress + ", linkSpeed="
+ linkSpeed + ", localHost=" + localHost + ", mac=" + mac
+ ", macAddress=" + macAddress + ", netWorkId=" + netWorkId
+ ", ports=" + ports + ", rssi=" + rssi + ", ssid=" + ssid
+ "]";
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主机名:"
/>
<TextView
android:id="@+id/hostName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
  </LinearLayout>
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="端口号:"
/>
<TextView
android:id="@+id/port"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
  </LinearLayout>
   <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP地址:"
/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
  </LinearLayout>
   <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAC地址:"
/>
<TextView
android:id="@+id/mac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
  </LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/getInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="getInfo"
/>
<Button
android:id="@+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="other"
/>
</LinearLayout>
</LinearLayout>

服务端

package com.server;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerDemo {


public static void main(String[] args) {
new ServerDemo().servers();
}

ServerSocket ss;
Socket s;
private int port = 1818;
private void servers(){
try {
ss = new ServerSocket(port);
System.out.println("start receive for the client....");
while(true){
s = new Socket();
s = ss.accept();
System.out.println("connect: "+s.getInetAddress()+" address!");
SeieslizeBean sb = new SeieslizeBean();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
sb = (SeieslizeBean) in.readObject();
System.out.println(sb.toString());
in.close();

ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
SeieslizeBean sbs = new SeieslizeBean();
String os = getOSName();
System.out.println(os);
String mac  = null;
if(os.startsWith("windows")){
mac = getWindowsMACAddress();
System.out.println(mac);
}else{
mac = getUnixMACAddress();
System.out.println(mac);
}
sbs.setHostName(os);
sbs.setMac(mac);
out.writeObject(sbs);
out.flush();
out.close();
}
} catch (Exception e) {
try {
s.close();
ss.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

/** 
     * 获取当前操作系统名称. 
     * return 操作系统名称 例如:windows xp,linux 等. 
     */  
    public static String getOSName() {  
        return System.getProperty("os.name").toLowerCase();  
    }  
    
    /** 
     * 获取unix网卡的mac地址. 
     * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法. 
     * @return mac地址 
     */  
    public static String getUnixMACAddress() {  
        String mac = null;  
        BufferedReader bufferedReader = null;  
        Process process = null;  
        try {  
            process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息  
            bufferedReader = new BufferedReader(new InputStreamReader(process  
                    .getInputStream()));  
            String line = null;  
            int index = -1;  
            while ((line = bufferedReader.readLine()) != null) {  
                index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]  
                if (index >= 0) {// 找到了  
                    mac = line.substring(index +"hwaddr".length()+ 1).trim();//  取出mac地址并去除2边空格  
                    break;  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (bufferedReader != null) {  
                    bufferedReader.close();  
                }  
            } catch (IOException e1) {  
                e1.printStackTrace();  
            }  
            bufferedReader = null;  
            process = null;  
        }  
  
        return mac;  
    }  
  
    /** 
     * 获取widnows网卡的mac地址. 
     * @return mac地址 
     */  
    public static String getWindowsMACAddress() {  
        String mac = null;  
        BufferedReader bufferedReader = null;  
        Process process = null;  
        try {  
            process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息  
            bufferedReader = new BufferedReader(new InputStreamReader(process  
                    .getInputStream()));  
            String line = null;  
            int index = -1;  
            while ((line = bufferedReader.readLine()) != null) {  
                index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]  
                if (index >= 0) {// 找到了  
                    index = line.indexOf(":");// 寻找":"的位置  
                    if (index>=0) {  
                        mac = line.substring(index + 1).trim();//  取出mac地址并去除2边空格  
                    }  
                    break;  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (bufferedReader != null) {  
                    bufferedReader.close();  
                }  
            } catch (IOException e1) {  
                e1.printStackTrace();  
            }  
            bufferedReader = null;  
            process = null;  
        }  
  
        return mac;  
    }  
    
}


package com.server;


import java.io.Serializable;


public class SeieslizeBean implements Serializable{


private static final long serialVersionUID = 1L;


private String hostName;
private int ports;
private String address;
private String mac;

public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPorts() {
return ports;
}
public void setPorts(int ports) {
this.ports = ports;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}


private String bssid;
private String classs;
private boolean hiddenSsid;
private int ipAddress;
private int linkSpeed;
private String macAddress;
private int netWorkId;
private int rssi;
private String ssid;


public String isBssid() {
return bssid;
}
public void setBssid(String bssid) {
this.bssid = bssid;
}
public String getClasss() {
return classs;
}
public void setClasss(String classs) {
this.classs = classs;
}
public boolean isHiddenSsid() {
return hiddenSsid;
}
public void setHiddenSsid(boolean hiddenSsid) {
this.hiddenSsid = hiddenSsid;
}
public int getIpAddress() {
return ipAddress;
}
public void setIpAddress(int ipAddress) {
this.ipAddress = ipAddress;
}
public int getLinkSpeed() {
return linkSpeed;
}
public void setLinkSpeed(int linkSpeed) {
this.linkSpeed = linkSpeed;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public int getNetWorkId() {
return netWorkId;
}
public void setNetWorkId(int netWorkId) {
this.netWorkId = netWorkId;
}
public int getRssi() {
return rssi;
}
public void setRssi(int rssi) {
this.rssi = rssi;
}
public String getSsid() {
return ssid;
}
public void setSsid(String ssid) {
this.ssid = ssid;
}

private String hostName2;
private String inetAddr2;
private String canoicalHostName;
private String addres2;
private String calsss2;
private String localHost;


public String getHostName2() {
return hostName2;
}
public void setHostName2(String hostName2) {
this.hostName2 = hostName2;
}
public String getInetAddr2() {
return inetAddr2;
}
public void setInetAddr2(String inetAddr2) {
this.inetAddr2 = inetAddr2;
}
public String getCanoicalHostName() {
return canoicalHostName;
}
public void setCanoicalHostName(String canoicalHostName) {
this.canoicalHostName = canoicalHostName;
}
public String getAddres2() {
return addres2;
}
public void setAddres2(String addres2) {
this.addres2 = addres2;
}
public String getCalsss2() {
return calsss2;
}
public void setCalsss2(String calsss2) {
this.calsss2 = calsss2;
}
public String getLocalHost() {
return localHost;
}
public void setLocalHost(String localHost) {
this.localHost = localHost;
}
@Override
public String toString() {
return "SeieslizeBean [addres2=" + addres2 + ", address=" + address
+ ", bssid=" + bssid + ", calsss2=" + calsss2
+ ", canoicalHostName=" + canoicalHostName + ", classs="
+ classs + ", hiddenSsid=" + hiddenSsid + ", hostName="
+ hostName + ", hostName2=" + hostName2 + ", inetAddr2="
+ inetAddr2 + ", ipAddress=" + ipAddress + ", linkSpeed="
+ linkSpeed + ", localHost=" + localHost + ", mac=" + mac
+ ", macAddress=" + macAddress + ", netWorkId=" + netWorkId
+ ", ports=" + ports + ", rssi=" + rssi + ", ssid=" + ssid
+ "]";
}

}

posted @ 2011-07-09 12:45  移动应用开发  阅读(1156)  评论(0编辑  收藏  举报