android ipv6 jni,Using a java socket from JNI / C++ code

I have a java app that creates a socket to talk to a server process, eg new java.net.Socket(String host, int port). This app includes a bunch of legacy c++ code that needs to suck gobs of data from that server and process it. This is currently implemented by having the native code create its own socket and connect to the server, eg:

sock = socket(AF_INET, SOCK_STREAM, 0);

struct hostent* hp = gethostbyname(host);

if (!hp)

{

unsigned long addr = inet_addr(host);

hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);

}

struct sockaddr_in name;

name.sin_family = AF_INET;

memcpy(&name.sin_addr, hp->h_addr, hp->h_length);

name.sin_port = htons(port);

connect(sock, (sockaddr*)&name, sizeof(name));

On Windows vista/7 machines with multiple NICs (eg wired and wifi or vpn connections), these two sockets can end up with different local addresses. The java code appears to choose a "better" interface (the wired Gb enet = higher MTU?), the native (naive?) code gets the "default" interface (stick in a usb wifi device and it becomes your default - yuck).

This causes some problems for me, I don't think the details are relevant. Two questions:

Is is possible for me to re-use the java socket from the JNI code (portably? assume Sun JDK). This would avoid the issue completely, but so far I don't see any way to interact with the java.net.Socket stuff from JNI/native code.

Since the answer to the first question is probably NO, how is java creating that socket (choosing the interface) ? Code snippets welcomed. I have looked around in the openjdk stuff and haven't found what I am looking for.

Thanks,

Chris

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值