Linux network programming related functions

Source: Internet
Author: User

Network

struct SOCKADDR
{
sa_falimy_t sa_family; Protocol family
Char sa_data[14]; Protocol Family Data
};

struct SOCKADDR_IN
{
U8 Sin_len; Length of sockaddr_in, 16
U8 sin_family; Protocol family
U16 Sin_port; Port
struct IN_ADDR sin_add; IP address, 32-bit
Char Sin_zero[8]//unused
}

int socket (int family, int type, int protocol)
Parameters:
Family: Protocol family, Common Af_inet (IPV4)
Type: Socket type, common sock_stream (byte stream)
Protocol: Protocol type, typically 0 (TCP)
return value:
FD: Socket descriptor function Call succeeded: Non-negative integer failure: 1

int bind (int socked, const struct SOCKADDR *my_addr, socklen_t Addrlen)
Parameters:
SOCKFD: Socket Descriptor
my_addr:aockaddr pointer, IP and port included
addrlen:sizeof (struct sockaddr)
return value:
0: Success
-1: Failure

int listen (int socked, int backlog)
Parameters:
socked: Socket Descriptor
Backlog: The maximum number of connections queued
return value:
0: Success
-1: Failure

int accept (int socked, struct sockaddr *addr, socklen_t *addrlen)
Parameters:
socked: Socket Descriptor
Addr: Client sock information
Addrlen: Client sock structure length
return value:
FD: Socket descriptor for new client links

int connect (int socked, struct sockaddr *addr, int addrlen)
Parameters:
socked: Socket Descriptor
Addr:sock Information
Addrlen:sock length
return value:
0: Success
-1: Failure

int write (int socked, char* data, int size)
Parameters:
socked: Socket Descriptor
Data:
Size: Data length
return value:
Size: Successful write length

int read (int socked, char* data, int size)
Parameters:
socked: Socket Descriptor
Data:
Size: Maximum data length
return value:
Size: Read length successfully

int select (int Nfds, fd_set* Readfds, fd_set* Writefds, fd_set* exceptfds, struct timeval* timeout)
Parameters:
NFDS: Maximum socket descriptor +1
Readfds: A collection of socket descriptors that need to monitor data read operations
Writefds: A collection of socket descriptors that need to monitor data write operations
Exceptfds: A collection of socket descriptors that need to be monitored for exceptions to occur
Timeout: Set maximum wait time
return value:
-1: Error occurred
0: Timeout
positive integers: Monitored for the selected watch content
Tool functions:
Fd_zero (): Cleanup Descriptor Collection
Fd_set (): Add a new descriptor to the collection
FD_CLR (): Remove a descriptor from the collection
Fd_isset (): Determines whether a descriptor is in the collection

int poll (struct polled* FDS, nfds_t nfds, int timeout)
Parameters:
FDS: Socket descriptor, listener event collection, event collection actually occurring
Nfds: ...
Timeout: Longest wait event


int epoll_create (int size);
Parameters:
Size: Maximum number of listeners
return value:
Epfd:epoll Descriptor
int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
Role:
Registering events
Parameters:
Epfd:epoll Descriptor
OP: Operation type (add, modify, delete)
FD: Socket Descriptor
Event: Specific Events
int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout);
Parameters:
EPFD:p Oll Descriptor
Events: Event collection that occurs
Maxevents: Maximum number of events
Timeout: Maximum wait event

Linux network programming related functions

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.