Posix本地通信用于同一设备或native和framework层之间

   新建本地Socket:socket

   同样socket函数能够被用来创建一个本地的socket。子通过指导者函数来创建这socket在这PF_LOCAL协议族。为了使它对于你试验所用类型的连接,代替修改原因的native帮助函数,一个新的native函数将被定义来创建这局部的sockets。

   int localSocket=socket(PF_LOCAL,SOCK_STREAM,0);

   这本地的socket端口地址仅仅只要一个名字组成。它并没有一个IP地址或者一个端口号。本地的socket名字通过以下两种命名空间来创建:

     Abstract 命名空间在本地socket通信端口模块被维护。这socket名字以一个NULL字符为前缀用于绑定着socket名字。

    Filesystem命名空间通过这文件系统作为一个特别的socket文件被维护。这socket名字被直接传递到了这sockaddr_un结构体来绑定着socket名字到这socket。

  struct sockaddr_un address;
// Name length
const size_t nameLength = strlen(name);
// Path length is initiall equal to name length
size_t pathLength = nameLength;
// If name is not starting with a slash it is
// in the abstract namespace
bool abstractNamespace = ('/' != name[0]);
// Abstract namespace requires having the first
// byte of the path to be the zero byte, update
// the path length to include the zero byte
if (abstractNamespace)
{
pathLength++;
}
// Check the path length
if (pathLength > sizeof(address.sun_path))
{
// Throw an exception with error number
ThrowException(env, "java/io/IOException", "Name is too big.");
}
else
{
// Clear the address bytes
memset(&address, 0, sizeof(address));
address.sun_family = PF_LOCAL;
// Socket path
char* sunPath = address.sun_path;
// First byte must be zero to use the abstract namespace
if (abstractNamespace)
{
*sunPath++ = NULL;
}
// Append the local name
strcpy(sunPath, name);
// Address length
socklen_t addressLength =
Download at http://www.pin5i.com/
268 CHAPTER 10: POSIX Socket API: Local Communication
(offsetof(struct sockaddr_un, sun_path))
+ pathLength;
// Unlink if the socket name is already binded
unlink(address.sun_path);
// Bind socket
LogMessage(env, obj, "Binding to local name %s%s.",
(abstractNamespace) ? "(null)" : "",
name);
if (−1 == bind(sd, (struct sockaddr*) &address, addressLength))
{
// Throw an exception with error number
ThrowErrnoException(env, "java/io/IOException", errno);
}
}

 

Accept在本地Socket:accept

  这同样的accept函数被使用来接收到来的连接到这本地的sockt,这位于的不用和客户端端口地址是accept返回江苏soketaddr_un类型的。

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值