Android NDK: From Elementary to Expert Episode 12

Accepting Incoming Connections: accept

The accept function is used to explicitly pull an incoming connection from the listen queue and to accept it.

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

The accept function is a blocking function. If there is no pending incoming connection request in the listen queue, it puts the calling process into a suspended state until a new incoming connection arrives. The accept function requires the following arguments to be provided in order to accept a pending incoming connection:

  • The socket descriptor specifies the socket instance that the application wants to accept a pending incoming connection on.
  • The address pointer provides an address structure that gets filled with the protocol address of the connecting client. If this information is not needed by the application, it can be set to NULL.
  • The address length pointer provides memory space for the size of the protocol address of the connecting client to be filled in. If this information is not needed, it can be set to NULL.

Result:
accept request is successful, the function returns the client socket descriptor that will be used; -1 and the errno global variable is set to appropriate error.

Receive Data from the Scoket: recv

The recv function could receive data from socket:

ssize_t recv(int socketDescriptor, void *buffer, size_t bufferLength, int flags);

recv function is also a blocking function. If there is no data that can be received from the given socket, it puts the calling process into suspended state until data becomes available. The recv function requires the following arguments to be provided in order to accept a pending incoming connection:

  • The socket descriptor specifies the socket instance that the application wants to receive data from.
  • The buffer pointer to a memory address that will be filled with the data received from the socket.
  • The buffer length specifies the size of the buffer. The recv function will only fill the buffer up to this size and return.
  • Flags specify additional flags for receiving.

Result:
If the recv function is successful, it returns the number of bytes received from the socket; otherwise, it returns -1 and the errno global variable is set to the appropriate error. If the function returns zero, it indicates that the socket is disconnected.

Sending Data to the Socket: send

Sending data to the socket is achieved through the send function.

ssize_t send(int socketDescriptor, void* buffer, size_t bufferLength,int flags);

Like the recv function, the send function is also a blocking function. If the socket is busy sending data, it puts the calling process into a suspended state until the socket becomes available to transmit the data. The send function requires the following arguments to be provided in order to accept a pending incoming connection:

  • The socket descriptor specifies the socket instance that the application wants to send data to.
  • The buffer pointer to a memory address that will be sent through the given socket.
  • The buffer length specifies the size of the buffer. The send function will only transmit the buffer up to this size and return.
  • Flags specify additional flags for sending.

Result:
send function returns the number of bytes transmitted; -1 and the errno global variable is set to the appropriate error. Like the recv, if it return 0, it shows that the connection of socket is failed.

Connect to Address: connect

Connecting a socket to a server socket by providing the protocol address is achieved through the connect function.

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

The connect function requires the following arguments to be provided in order to accept a pending incoming connection:

  • The socket descriptor specifies the socket instance that the application wants to connect to a protocol address.
  • The address specifies the protocol address that the socket will connect.
  • The address length specifies the length of the address structure provided.

If the connection attempt is successful, the connect function 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、付费专栏及课程。

余额充值