Android 如何创建socket服务器

来源:互联网 发布:linux怎么安装语言包 编辑:程序博客网 时间:2024/05/16 14:19

1、根据本地的IP地址监听8888端口创建服务器


ConnectivityManager connect_Manager = (ConnectivityManager) getSystemService(mContext.CONNECTIVITY_SERVICE);

NetworkInfo netInfo =  connect_Manager.getActiveNetworkInfo();

if(netInfo == null){
Toast.makeText(mContext, "no Network", Toast.LENGTH_SHORT).show();
return;
}

if(netInfo.getType() == ConnectivityManager.TYPE_WIFI){
local_ip_address = intIP_to_StringIP(get_wifi_ipaddress());
//Log.i("123", "string_ip"+IP_ADDRESS);
}else if(netInfo.getType() == ConnectivityManager.TYPE_MOBILE){

local_ip_address = get_gprs_IpAddress();
}
if(local_ip_address != null){
//create service
//Log.i("123", "ceate service ip"+local_ip_address);
final int port = 8888;
Toast.makeText(mContext, "ip:"+local_ip_address+"--port:"+port, Toast.LENGTH_SHORT).show();
new Thread(){
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(port, 2,InetAddress.getByName(local_ip_address));
Socket socket = null;
while(true){

socket =  serverSocket.accept();

async_read_socket_date(socket);



}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();


}

2、且接收客户端的数据 async_read_socket_date()


public void async_read_socket_date(final Socket socket) {
new Thread(){

public void run() {
while(true){
InputStream inputStream;
try {
inputStream = socket.getInputStream();


//Log.i("123", "read len before");
byte[] rec_data = new byte[1024];
int read_len = inputStream.read(rec_data);
//Log.i("123", "read len"+read_len);
if(read_len > 0){
Log.i("123", new String(rec_data,0,read_len));


}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
socket.sendUrgentData(0xFF);
} catch (IOException e) {
// TODO Auto-generated catch block
try {
socket.close();
break;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


}
}
}.start();
}

注:以下是获取本机联机的IP地址的方法

public String intIP_to_StringIP(int ip) {
return (ip&0xFF)+"."+((ip>>8)&0xFF)+"."+((ip>>16)&0xFF)+"."+((ip>>24)&0xFF);
}
public int  get_wifi_ipaddress() {
WifiManager wifiManager = (WifiManager) getSystemService(mContext.WIFI_SERVICE);

WifiInfo wifiInfo =  wifiManager.getConnectionInfo();

int ip = wifiInfo.getIpAddress();
//Log.i("123", "wifi_ip"+ip);
return ip;
}
public String get_gprs_IpAddress()  
    {  
        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())  
                   {  
                       return inetAddress.getHostAddress().toString();  
                   }  
               }  
           }  
        }  
        catch (SocketException ex)  
        {  
            Log.e("gprsPreference IpAddress", ex.toString());  
        }  
        return null;  
    }