java socket本地通信_android native c java进行本地socket通信 | 学步园

本文介绍了一种Java与Native跨平台通信的方法,利用本地Socket实现服务器端Java应用与客户端Native应用的数据交互,包括Java服务端代码示例及Native客户端C语言实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sizcache="1" sizset="2">方式一:java做服务器端,native做client端

1. 建立java应用程序,建立Server 类

/*

* 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);

for(int i=0;i

Log.d(TAG, "data["+i+"]="+data[i]);

}

byte[] values = TypeUtils.float2Byte(-1234567.122f);

float fd = -1234567.122f;

Log.d(TAG, " fd="+fd);

for(int i=0;i

Log.d(TAG, "values["+i+"]="+values[i]);

}

os.write(values);

os.flush();

Log.v(TAG, "after write: ");

}

} catch (IOException e) {

Log.v(TAG, "in echo thread loop: " + e.getMessage());

}

}

};

t.start();

}

}

 
 
 
 

2.将float转换成byte[]数组的工具类

import java.nio.ByteBuffer;

import java.nio.FloatBuffer;

public class TypeUtils {

public static byte[] floatToByte(float v) {

ByteBuffer bb = ByteBuffer.allocate(4);

byte[] ret = new byte[4];

FloatBuffer fb = bb.asFloatBuffer();

fb.put(v);

bb.get(ret);

return ret;

}

public static byte[] float2Byte(float f) {

byte[] b = new byte[4];

int l = Float.floatToIntBits(f);

for (int i = 0; i < b.length; i++) {

b[i] = new Integer(l).byteValue();

l = l >> 8;

}

return b;

}

public static byte[] doubleToByte(double d) {

byte[] b = new byte[8];

long l = Double.doubleToLongBits(d);

for (int i = 0; i < b.length; i++) {

b[i] = new Long(l).byteValue();

l = l >> 8;

}

return b;

}

public static float byteToFloat(byte[] v) {

ByteBuffer bb = ByteBuffer.wrap(v);

FloatBuffer fb = bb.asFloatBuffer();

return fb.get();

}

public static float byte2Float(byte[] b) {

int l = 0;

l = b[0];

l &= 0xff;

l |= ((int) b[1] << 8);

l &= 0xffff;

l |= ((int) b[2] << 16);

l &= 0xffffff;

l |= ((int) b[3] << 24);

l &= 0xffffffffl;

return Float.intBitsToFloat(l);

}

}

3.在 native中建立client

#include

static union FloatValue{

char val[4];

float f;

} mf_t;

static __inline__ int

qemud_fd_write(int fd, const void* buff, int len)

{

int len2;

do {

len2 = write(fd, buff, len);

} while (len2 < 0 && errno == EINTR);

return len2;

}

static __inline__ int

qemud_fd_read(int fd, void* buff, int len)

{

int len2;

do {

len2 = read(fd, buff, len);

} while (len2 < 0 && errno == EINTR);

return len2;

}

int main(int argc, char **argv)

{

int fd;

char answer[200];

char name[5]= "test!";

int namelen = 5;

/* connect to qemud control socket */

fd = socket_local_client( "server_test",

ANDROID_SOCKET_NAMESPACE_ABSTRACT,

SOCK_STREAM );

if (fd < 0) {

printf("no qemud control socket: %s \n", strerror(errno));

return -1;

}

/* send service name to connect */

if (qemud_fd_write(fd, name, namelen) != namelen) {

printf("can't send service name to qemud: %s \n",

strerror(errno));

close(fd);

return -1;

}

printf(".... before qemud_fd_read \n");

/* read answer from daemon */

int res =qemud_fd_read(fd, answer, 200);

printf(" .....after qemud_fd_read ");

if (res) {

printf("connect to service through qemud res =%d answer0 =%d ,answer1 =%d

answer2 =%d ,answer3 =%d \n",res,answer[0],answer[1],answer[2],answer[3]);

mf_t.val[0] = answer[0];

mf_t.val[1] = answer[1];

mf_t.val[2] = answer[2];

mf_t.val[3] = answer[3];

printf(" .....after convert f=%f \n",mf_t.f);

close(fd);

return -1;

}

return 0;

}

这样就实现了java和native进行通信的目的了,而且是本地socket哟。

 
 
 
 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值