Android NDK: From Elementary to Expert Episode 11

Now let’s talk about POSIX socket API, this example application will provide the following:

  • A simple user interface for defining the parameters necessary to configure the sockets.
  • Service logic for a simple echo service repeating the received bytes back to the sender.
  • Boilerplate native code snippets to facilitate socket programming for Android in native layer.
  • A connection-oriented socket communication example.
  • A connectionless socket communication example.
  • A local socket communication example.

As Android is based on Linux, you can take further references on Unix Network Programming.

Create a socket: Socket

A socket can be created using the socket function.

int socket(int domain, int type, int protocol);

The socket function requires the following arguments to be provided in order to create a new socket:

  • Domain specifies the socket domain where the communication will take place and selects the protocol family that will be used. At the time of this writing, the following protocol families are supported on Android platform:

    • PF_LOCAL: Host-internal communication protocol family. This protocol family enables applications that are running physically on the same device to use the Socket APIs to communicate with each other.
    • PF_INET: Internet version 4 protocol family. This protocol family enables applications to communicate with applications that are running elsewhere on the network.
  • Type specifies the semantics of the communication. The following major socket types are supported:
    • SOCK_STREAM: Stream socket type provides connection-oriented communication using the TCP protocol.
    • SOCK_DGRAM: Datagram socket type provides connectionless communication using the UDP protocol.
    • Protocol specifies the protocol that will be used. For most protocol families and types, there is only one possible protocol that can be used. In order to pick the default protocol, this argument can be set to zero.

A socket can be bound to an address using the bind function.

int bind(int socketDescriptor, const struct sockaddr* address, socklen_t addressLength);

Result:
socket function returns the associated socket descriptor; -1 and the errno global variable is set to the appropriate error.

Binding the Socket to an Address: bind

The bind function requires the following arguments in order to bind the socket to an address:

  • The socket descriptor specifies the socket instance that will be bound to the given address.
  • The address specifies the protocol address where the socket will be bound.
  • The address length specifies the size of the protocol address structure that is passed to the function.

Depending on the protocol family, a different flavor of protocol address gets used. For PF_INET protocol family, the sockaddr_in structure is used to specify the protocol address. The definition of sockaddr_in structure is shown in Listing 1.

Listing 1. The sockaddr_in address Structure
struct sockaddr_in {
    sa_family_t sin_family;
    unsigned short int sin_port;
    struct in_addr sin_addr;
}

Result:
If the socket is properly bound, the bind function returns zero; otherwise, it returns -1 and the errno global variable is set to the appropriate error.

Network Byte Ordering

Different machine architectures use different conventions for ordering and representing data at the hardware level. This is known as the machine byte ordering, or endianness. For example:

  • Big-endian byte ordering stores the most significant byte first.
  • Little-endian byte ordering stores the least significant byte first.

The socket library provides a set of convenience functions to enable native applications to transparently handle the byte ordering conversions. These functions are declared through the sys/endian.h header file.

#include <sys/endian.h>

The following convenience functions are provided through this header file:

  • htons function converts an unsigned short from host machine byte ordering to network byte ordering.
  • ntohs function does the reverse of htons by converting an unsigned short from network byte ordering to host machine byte ordering.
  • htonl function converts an unsigned integer from host machine byte ordering to network byte ordering.
  • ntohl function does the reverse of htonl by converting an unsigned integer from network byte ordering to host machine byte ordering.

Result:
no result.

Listen for Incoming Connections: listen

Listening on a socket is achieved through the listen function.

int listen(int socketDescriptor, int backlog);

The listen function requires the following arguments to be provided in order to start listening for incoming connections on the given socket:

  • The socket descriptor specifies the socket instance that the application wants to start listening for incoming connections.
  • The backlog specifies the size of the queue to hold the pending incoming connections. If the application is busy serving a client, other incoming connections get queued up to the number of pending connections specified by the backlog. When the backlog limit is reached, other incoming connections get refused.

If the function is successful, it returns zero; otherwise, it returns -1 and the errno global variable is set to the appropriate error.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值