Some functions of network programming

Source: Internet
Author: User

#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes: (Create a set of interfaces)
int socket (int family,//protocol type, typically af_inet

int type,//socket type: Sock_stream, Sock_dgram, Sock_raw, Sock_packet, Sock_seqpacket

int protocol//is used to specify the transport protocol number used by the socket, usually set to 0, which means the default

); If no error occurs, the socket () returns a descriptive word referencing the new set of interfaces; Otherwise, the Invalid_socket error is returned

#include <sys/types.h>

#include <sys/socket.h>

Function prototype: (Local bundle (host address/port number) for a nested interface by assigning a local name to an unnamed socket interface)

int bind (int sockfd,// Socket Descriptor

struct sockaddr *my_addr,// is a pointer to a SOCKADDR type that contains information such as the native IP address and port number

int Addrlen// is often set to sizeof (struct sockaddr)

           ); returned: Successfully returned 0, failed to return-1

#include <sys/socket.h>

Function Prototypes: (Create a socket and listen for the requested connection.) Applies only to socket interfaces that support connections, such as sock_stream types)

The socket descriptor returned by the int listen (int sockfd,//socket () system call

int backlog//Specifies the maximum number of requests in the request queue, and incoming connection requests will wait in the queue for accept () them.

); Returns 0 if no error occurs, otherwise returns-1

#include <sys/types.h>

#include <sys/socket.h>

Function Prototypes:

int connect (int sockfd, struct sockaddr *serv_addr, int addrlen)
SOCKFD: Socket descriptor for destination server
SERV_ADDR: A pointer containing the IP address and port number of the destination machine
addrlen:sizeof (struct sockaddr)

#include <sys/types.h>
#inlcude <sys/socket.h>
Function Prototypes:
int accept (int sockfd, void *addr, int addrlen)
SOCKFD: Is the socket descriptor being monitored
Addr: Usually a pointer to the SOCKADDR_IN variable that holds the information for the host that made the connection request service
addrlen:sizeof (struct sockaddr_in)

#include <sys/socket.h>
Function Prototypes:
int send (int sockfd, const void *msg, int len, int flags);
SOCKFD: The socket descriptor used to transfer data
Msg: A pointer to send data
flags:0

#include <sys/types.h>
#include <sys/socket.h>
Function Prototypes:
int recv (int sockfd, void *buf, int len, unsigned int flags)
SOCKFD: Socket descriptor for receiving data
BUF: Buffer for storing data
Len: The length of the buffer
flags:0

Read () write ()
int read (int fd, char *buf, int len)
int write (int fd, char *buf, int len)

Shutdown ()
Close (SOCKFD)
int shutdown (int sockfd, int how)

#include <sys/socket.h>

ssize_t recvfrom (int sockfd, void *buf, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen);

ssize_t sendto (int sockfd, const void *buff, size_t nbytes, int flags, const struct SOCKADDR *to, socklen_t *addrlen);

Success returns the number of bytes read and write, with an error of-1.

The first parameter sockfd: descriptor;

Second parameter buff: A pointer to a buffer read in or written out;

The third parameter nbytes: Read and write the number of bytes;

The fourth parameter flags: Invoke operation mode;

The fifth parameter from/to: A socket address structure that points to the protocol address (such as IP address and port number) of the datagram sender or receiver;

The sixth parameter addrlen:from/to the size of the parameter.

int getsockname (int sockfd, struct sockaddr *localaddr, socklen_t *addrlen); Gets the local protocol address associated with a socket

int getpeername (int sockfd, struct sockaddr *localaddr, socklen_t *addrlen); Gets the field protocol address associated with a socket

#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>int inet_aton (const char *  string, struct in_addr*addr);  The string returns 1 effectively, otherwise returns 0INT Inet_pton (int af, const char *src, void *DST);  The "dotted decimal" string, "binary integer" parameter af is af_inet or af_inet6 char* inet_ntoa (struct in_addr in);  Returns a dotted decimal string of const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t CNT); struct IN_ADDR {in_addr_t s_addr;} ; The struct in_addr is used to represent a 32-bit IPV4 address. in_addr_t is typically a 32-bit unsigned int, whose byte order is the network order, that is, the unsigned integer takes the big endian byte. Each of these 8 bits represents a numeric value in an IP address bit.

#include <sys/select.h>

int select (int maxfdpl, fd_set *readset, Fd_set *writeset, fd_set *exceptset, const struct timeval *timeout); MAXFDPL: is a Integer value refers to the range of all file descriptors in the collection, that is, the maximum value of all file descriptors plus 1. Readset: (optional) pointer to a set of interfaces waiting for readability checks. Writeset: (optional) pointer, pointing to a set of interfaces waiting for a writable check. Exceptset: (optional) pointer, pointing to a set of interfaces waiting for error checking. Timeout:select () The maximum wait time, or null for a blocking operation. function return: If there is a ready descriptor, then the number is 0, the error is 1 (such as capturing to the interrupt signal).

#include <sys/epoll.h>int epoll_create (int size); Create a Epoll handle (epoll dedicated file descriptor), which is used to tell the kernel how large the number of listeners is. This parameter differs from the first parameter in select (), giving the value of the fd+1 that is the maximum listener.  int epoll_ctl (int epfd, int op, int fd, struct epoll_event event); This function is used to control events on a file descriptor, to register events, to modify events, and to delete events. The first parameter EPFD: is the return value of the Epoll_create (), the second argument op: Represents the action (action to be taken): Epoll_ctl_add: Registering a new FD to EPFD; Epoll_ctl_mod: Modifying a listener event for an already registered FD ; Epoll_ctl_del: Remove an FD from EPFD; the third parameter is FD: the one that needs to be monitored; The fourth parameter: is to tell the kernel what to listen for; struct epoll_event {__uint32_t events;/* Epoll  Events */epoll_data_t data; /* User data variable */};int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout); Waits for the event to occur (to poll for an I/O event), similar to a select () call. The first parameter EPFD: the Epoll private file descriptor generated by epoll_create; the second parameter events: The collection used to get the event from the kernel, and the third parameter, maxevents: The maximum number of events that can be processed at a time (the value cannot be greater than the creation Epoll_ Size for Create (), fourth parameter timeout: is the timeout (in milliseconds, 0 returns immediately, 1 never times out, or is permanently blocked until an event is generated). The function returns the number of events that need to be processed, returns 0 to indicate that it has timed out, and returns 1 for error.

#include <signal.h>

Signal (parameter 1, parameter 2);

Parameter 1: Signal processing. The system's signal is generally defined as a macro.

Parameter 2: How it is handled (whether the system is default or ignored or captured).

For example signal (SIGINT, sig_ing);

Sig_ing represents ignoring SIGINT signals, SIGINT signals are generated by Interruptkey, usually ctrl +c or delete. The process that is sent to all foreground group.

Some functions of network programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.