当前位置:编程学习 > JAVA >>

android native c java进行本地socket通信

<pre name="code" class="html">方式一:java做服务器端,native做client端 
1. 建立java应用程序,建立Server 类 
<pre name="code" class="html">/* 
 * Copyright (C) 2009 The Android Open Source Project 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.example.hellojni; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.net.LocalServerSocket; 
import android.net.LocalSocket; 
 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
 
 
public class HelloJni extends Activity 

    public static final String SOCKET_NAME = "server_test"; 
    private static final String TAG = "SocketService"; 
    private LocalServerSocket mServerSocket = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
 
    Log.v(TAG, "onCreate"); 
        try { 
            mServerSocket = new LocalServerSocket(SOCKET_NAME); 
        } catch (IOException e) { 
            Log.v(TAG, "in onCreate, making server socket: " + e); 
            return; 
        } 
 
        Thread t = new Thread() { 
                @Override public void run() { 
                    LocalSocket socket = null; 
                    while (true) { 
                        try { 
                            Log.v(TAG, "Waiting for connection..."); 
                            socket = mServerSocket.accept(); 
                            Log.v(TAG, ".....Got socket: " + socket); 
                            if (socket != null) { 
                                startEchoThread(socket); 
                            } else { 
                                return;  // socket shutdown? 
                            } 
                        } catch (IOException e) { 
                            Log.v(TAG, "in accept: " + e); 
                        } 
                    } 
                } 
            }; 
        t.start(); 
 
 
    } 
 
private void startEchoThread(final LocalSocket socket) { 
        Thread t = new Thread() { 
                @Override public void run() { 
                    try { 
                        InputStream is = socket.getInputStream(); 
                        OutputStream os = socket.getOutputStream(); 
                        InputStreamReader isr = new InputStreamReader(is); 
                        while (true) { 
                            char[] data = new char[128]; 
 
                            int ret = isr.read(data); 
                       &n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,