当前位置:网站首页>NDK learning notes (IX) POSIX sockect connection oriented communication

NDK learning notes (IX) POSIX sockect connection oriented communication

2022-06-11 05:34:00 Come and go

1.sockect Function introduction

__af: Specify the... That will generate communication socket Domain , And select the protocol cluster to be used .
	android Protocol clusters supported by the platform :
		PF_LOCAL: Host internal communication protocol cluster 
		PF_INET:internet The first 4 Version protocol cluster , Corresponding ipv4
__type:
	SOCK_STREAM: Provide use TCP Agreed 、 Connection oriented communication Stream socket type .
	SOCK_DGRAM: Provide use UDP Agreed 、 Connectionless communication Datagram socket type .
__protocol: Specify the protocol to be used . Select the default protocol , This parameter can be set to zero .

 If created successfully , Returns the relevant socket The descriptor ; Failure returns -1
__socketcall int socket(int __af, int __type, int __protocol);
__fd: Specifies the... That will be bound to the specified address socket example 
__addr: Specify the protocol address 
__addr_length: Specifies the size of the protocol address structure passed to the function 

 Bind address successfully , return 0, Otherwise return to -1
__socketcall int bind(int __fd, const struct sockaddr* __addr, socklen_t __addr_length);

Network byte sort
On the hardware layer , Different machine architectures use different data ordering and presentation rules , This is called machine byte ordering or byte ordering .

  • BIG-endian Byte order stores the most important bytes first .
  • Little-endian Byte order stores the least important bytes first .
    To enable machines with different byte ordering rules to communicate over the Internet ,IP take big-endian Byte sorting is declared as the official data transmission network byte sorting rule .

java Virtual machines do not execute native components , Use machine byte sorting .

  • ARM and x86 The machine structure uses little-endian Byte sort
  • MIPS The machine structure uses big-endian Byte sort

In network communication , Native code needs to do the necessary conversion between machine byte sorting and network byte sorting .
endian.h Enables native applications to transparently handle byte sort conversions .

#include <sys/endian.h>

endian.h Function introduction in :
htons() take unsigned short Convert from host byte sort to network byte sort .
ntohs() and htons The function is the opposite , take unsigned short Convert from network byte sort to host byte sort .
htonl() take unsigned integer Convert from host byte sort to network byte sort .
ntohl() and htonl The function is the opposite , take unsigned integer Convert from network byte sort to host byte sort .

Listen for incoming connections

__fd: Specify the input connections that the application wants to listen on socjet example 
__backlog: Specifies the size of the queue that holds the pending input connections .

 success , return 0, Failure to return -1
__socketcall int listen(int __fd, int __backlog);

Accept incoming connections

accept It's a blocking function .
__fd:socket example 
__addr: Address structure , Fill in the structure with connected kehu Customer agreement address , If the application does not need to change the information , It can be set to null.
__addr_length: The connection client protocol address provides a specified size of memory space . If this information is not required , It can be set to NULL.

 If  accept Ask Chen Gong , This function returns the client that will be used when interacting with the connection instance socket The descriptor ; otherwise , return -1
__socketcall int accept(int __fd, struct sockaddr* __addr, socklen_t* __addr_length);

Receive data

recv Function is a blocking function 
__fd:socket example 
__buf: Pointer to memory address , This memory is used to store data from socket Data received .
__n: Specifies the size of the buffer 
__flags: Specify the additional flags required for acceptance 

 success , Return from socket The number of bytes received there , Otherwise return to -1.
 return 0 Express socket The connection fails 
ssize_t recv(int __fd, void* __buf, size_t __n, int __flags);

send data

 Blocking function 
__fd:socket example 
__buf: Points to the memory address buffer The pointer ,
__n: Specifies the size of the buffer 
__flags: Additional signs 
 Send successfully ,send The function returns the number of bytes transferred , Otherwise return to -1. return 0 The connection fails 
ssize_t send(int __fd, const void* __buf, size_t __n, int __flags);

client

Connect

__fd:socket example 
__addr: Address 
__addr_length: Address structure length 

 success 0, Failure -1
__socketcall int connect(int __fd, const struct sockaddr* __addr, socklen_t __addr_length);

2.Socket Example

Because the amount of code is not easy to show in the blog , So it was put in gitee On .

https://gitee.com/xd_box/NDK_Socket

This example needs to be run with two simulators to see the effect .
After starting the simulator , A running EchoServerActivity Interface , A running EchoClientActivity Interface .
Connectivity simulator :

emulator-5554 Is to run EchoServerActivity Interface simulator 
adb -s emulator-5554 forward tcp:12345  tcp:12346

The operation effect is as follows
 Insert picture description here

原网站

版权声明
本文为[Come and go]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020538239548.html