In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to carry out ios network programming development analysis, the content is very detailed, interested friends can refer to reference, hope to help everyone.
Ios network programming development includes many frameworks and libraries, from low-level sockets to different layers of encapsulation, which can easily add network functions to programs.
(1)BSD socket. The lowest level socket, which is a Unix network development API commonly used. If you port a program from another system and the program uses BSD sockets, the network part can continue to use these APIs.
(2)CFNetwork framework 。CFNetwork is also relatively low-level and is an extension to BSD sockets. It is a C library based on BSD sockets that provides abstractions to network protocols. These abstractions make it easier for users to manipulate sockets and handle the various connections of the network. It integrates run-loops, so you don't have to implement event loops yourself using CFNetwork. CFNetwork also includes implementations of network protocols (e.g. HTTP, FTP) that can be used without knowing the details of these protocols.
(3)The Foundation framework is a library based on the Objectice-C language that provides object-oriented abstractions for the CFNetwork API.
CFURL(C)/NSURL(Objective-C) are higher-level APIs that provide an easy way to download files or other resources from Web and FTP servers. CFURL is part of CFNetwork and NSURL is part of Foundation.
CFNetServices/NSNetServices provides a way to register and discover web services using Bonjour.
In short, CFNetwork framework and Foundation framework are the two most powerful libraries, they are relatively low-level, efficient, and provide a very rich interface.
Sockets are the basic building blocks of network communication, providing endpoints for bidirectional communication between processes on different hosts. If the phone is called, the conversation can only be established when one party dials the other. Sockets programming allows programs to skip complex network protocols and structures and program platform-independent applications directly. Sockets have gradually become a common interface for network programming.
Sockets exist in their specific communication domain (i.e. protocol family), and only sockets belonging to the same protocol family can establish conversations. In general, only sockets of the same type can pass data to each other unless the communication protocol supports it. There are two main types of slave sockets: streaming sockets (TCP) and datagram sockets (UDP).
SOCK_STREAM: This class of sockets provides connection-oriented, reliable, error-free, and duplicate-free data transmission services, and the data sent is accepted sequentially. All data passed using this socket is treated as a continuous byte stream with no length limit. This is very suitable for applications with strict requirements on data stability, correctness and sending/receiving sequence. TCP protocol uses this type of interface, but its line occupancy is relatively high. Streaming socket implementations are common, such as TELNET and FTP.
Datagram socket (SOCK_DGRAM): Datagram socket provides connectionless oriented services. It sends data independently in the form of packets (packet length cannot be greater than 32KB), does not provide correctness checks, and does not guarantee the order of transmission of each packet, so there may be retransmission, loss, etc. of data, and the order of acceptance is determined by the specific route. However, the implementation of datagrams has a low network line occupancy, NFS(Network File System) uses such sockets, and UDP(User Datagram Protocol) uses such interfaces in the TCP/IP protocol family.
1.CFSocket
CFSocket is an abstract encapsulation of BSD sockets that provides almost all of the functionality of BSD sockets and integrates run-loops. CFSocket can handle any type of socket, not just streaming sockets.
(1)Creating CFSocket
Common methods are CFSocketCreate and CFSocketCreate WithNative.
The CFSocketCreate method declares the following:
CFSocketRef CFSocketCreate (
CFAllocatorRef allocator, //Specifies the memory allocator type to create the new socket, passing NULL or kCFAllocatorDefault The default allocator can be used. General default can be.
SInt32 protocolFamily, //Specifies the protocol family of the socket. PF_INET is used by default, which is usually called IPV4. Specify PF_INET6 to use IPV6 protocol.
SInt32 socketType, //socket type, SOCK_STREAM or SOCK_DGRAM.
SInt32 protocol, //The protocol used by the socket, IPPROTO_TCP or IPPROTO_UDP. This item needs to be consistent with the socket type. Set it to 0 and take the default value (if socketType is SOCK_STREAM, the default value of this item is IPPROTO_TCP, otherwise IPPROTO_UDP).
CFOptionFlags callBackTypes,//CFSocket provides a run-loop that calls back specified functions when certain events occur. This parameter uses a bitmask, so multiple types can be specified using bitwise OR operations. Apple defines some types as enumerated values as follows:
enumeration value
enum CFSocketCallBackType {
kCFSocketNoCallBack = 0,
kCFSocketReadCallBack =1,
kCFSocketAcceptCallBack =2,
kCFSocketDataCallBack = 3,
kCFSocketConnectionCallBack = 4,
kCFSocketWriteCallBack =8
};
typedef enum CFSocketCallBackTypeCFSocketCallBackType;
CFSocketCallBack callback,//Specifies a callback function that is invoked when one of the specified event types occurs. In this way, you don't have to write your own loop waiting for the connection and sending data.
const CFSocketContext *context //A data structure that stores socket-related information, which can contain custom data. The function copies out the contents, so the memory pointed to by the parameter does not have to be retained after the function is called. It can be NULL.
);
CFSocketCreateWithNative method, you can use an existing BSD socket to create CRSocket,
CFSocketRef CFSocketCreateWithNative
{
CFAllocatorRef allocator,
CFSocketNativeHandle sock,
CFOptionFlags callBackTypes,
CFSocketCallBack callout,
const CFSocketContext *context
};
2. socket function
Once CFSocket is created, you can use the series of functions it provides. The CFSocketNative function also allows you to manipulate the lower BSD sockets. The following functions are commonly used in CFSocket.
CFSocketGetNative returns the system socket, usually of type int. To obtain this socket, you can use socket operations native to Unix systems. Call the setsockopt function.
(2)CFSocketConnectToAddress is used to connect a socket to a listening socket (server).
(3)CFSocketCopyAddress returns the address of the CFSocket, so you can know which IP address the socket is listening on.
(4)CFSocketCopyPeerAddress Gets the address of the remote socket to which CFSocket is connected.
(5)CFSocketCreateRunLoopSource Creates a CFRunLoop for CFSocket.
(6)CFSocketSendData To send data, CFSocket, address (NULL is sent to the address to which the socket has been connected), data to be sent (CFDataRef type), timeout time are required. There are three types of return values: kCFSocketSuccess, kCFSocketError and kCFSocketTimeout.
3. Callback function
Function Type Definition
typedef void (*CFSocketCallBack)(
CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info
) ;
function declaration
void CallBackTest
(
CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info
) ;
Each callback function gets the following information, some of which may vary depending on the type.
(1) CFSocket corresponding to CFSocketRefs event, so that events generated by different sockets can be distinguished.
CFSocketCallBackType CallbackType identifies which event occurred.
(3)CFDataRef address CFData pointer containing the underlying sockaddr information. The remote address to which the socket can be connected can be obtained with this parameter. This parameter is provided only when the kCFSocketAcceptCallBack and kCFSocketDataCallBack events occur.
(4)const void *data points to different data depending on the callback type. If kCFSocketDataCallBack, this parameter is of type CFDataRef and contains the received data; if kCFSocketAcceptCallBack, this parameter is a pointer to CFSocketNativeHandle; if kCFSocketConnectCallBack, the connection failed in the background, this parameter is a pointer to the SInt32 error code. Otherwise this parameter is NULL.
(5)void *info The info member in CFSocketContext structure is custom data. CFSocketContext is specified when CFSocket is created.
4.CFSocketContext
It is a struct containing custom data and some callbacks. Because CFSockets notify events asynchronously via run-loops, it becomes difficult to keep track of data associated with each connection when there are many connections. CFSocketContext allows you to bind any type of data to a socket, and every callback function can get this data.
CFSocketContext
struct CFSocketContext
{
CFIndex version;//The version number of the struct, which must be 0.
void *info;//Pointer to custom data, associated at CFSocket creation time.
CFAllocatorRetainCallBack retain;//retain callback for info pointer, can be NULL.
CFAllocatorReleaseCallBack release;//release callback for info pointer, can be NULL.
CGAllocatorCopyDescriptionCallBackcopyDescription; //copydescription callback for info pointer, can be NULL.
} ;
typedef struct CFSocketContextCFSocketContext;
About how to carry on the ios network programming development analysis to share here, hope the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.