Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of socket programming function

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Socket ()

# include

# include

Int socket (int domain,int type,int protocol)

Return value: a file descriptor was successfully returned (note that this socket cannot be used to communicate with users, but can only be used for connection requests from listen and accept clients). If failed,-1 is returned.

Parameters:

Domain

AF_INET IPv4 Internet protocols

Type

SOCK_STREAM can provide sequencing, reliable two-way, byte stream

SOCK_DGRAM supports datagrams (a fixed maximum length for connectionless, unreliable messages)

Protocol

Can be specified as 0

Bind ()

# include

# include

Int bind (int sockfd, const struct sockaddr * addr, socklen_t addrlen)

Return value: 0 for success and-1 for failure

Description:

When you create a socket, it exists in a namespace (home address), but no address is assigned to it. Bind () assigns the specified address to the file descriptor mentioned in addr's socket sockfd. Addrlen, in bytes, indicates the address structure through the addr. Traditionally, this operation is called "assigning a name to a socket."

# define _ _ SOCKADDR_COMMON (sa_prefix)\

Sa_family_t sa_prefix##family

Struct sockaddr_in

{

_ _ SOCKADDR_COMMON (sin_)

In_port_t sin_port; / * Port number. , /

Struct in_addr sin_addr; / * Internet address. , /

}

Typedef uint32_t in_addr_t

Struct in_addr

{

In_addr_t s_addr

}

Listen ()

# include

# include

Int listen (int sockfd, int backlog)

Return value: 0 for success and-1 for failure

Parameters:

Backlog

The to-do list parameter defines that the maximum length of the queue waiting for a connection is that sockfd may grow. If a connection request arrives at a full queue, the client may receive an error indicating ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored and try again after the connection is successful.

Accept ()

# include

# include

Int accept (int sockfd, struct sockaddr * addr, socklen_t * addrlen)

Description:

The accept () function creates and returns a new socket client_sock for communicating with the client.

Suppose there are three clients connected to the server. Then there are four sockets on the server side: the first is the socket returned by socket () for listening; the other three are different sockets returned by calling accept () three times.

Send ()

# include

# include

Ssize_t send (int sockfd, const void * buf, size_t len, int flags)

Return value:

> 0 data sent successfully

The length of the sockfd send buffer. The function returns SOCKET_ERROR

If the remaining length of the lensockfd send buffer, send waits for the protocol to finish sending the data on the send buffer, and if len

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report