代码改变世界

Socket

  Jeff  阅读(744)  评论(0编辑  收藏  举报
创建一个socket.
1
2
3
4
5
6
7
int socket
    (
    int domain,    /* address family (AF_xxx)     */
    int type,      /* socket type (SOCK_xxx)      */
    /*UDP: SOCK_DGRAM; TCP:SOCK_STREAM */
    int protocol   /* socket protocol (usually 0) */
    )
setsockopt - set socket options:
1
setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval));
The value at is an integer (type `int') that specifies the size of the socket-level send buffer to be allocated. When stream, datagram or sequential packet sockets are created, each transport protocol reserves a set amount of space at the socket level for use when the sockets are attached to a protocol. For TCP, the default size of the send buffer is 8192 bytes. For UDP, the default size of the send buffer is 9216 bytes. For COMP, it is 64kbytes. Socket-level buffers are allocated dynamically from the mbuf pool.
1
setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval));
1
2
3
4
5
6
7
8
setsockopt
    (
    int s,              /* target socket */
    int level,          /* protocol level of option */
    int optname,        /* option name */
    char *optval,       /* pointer to option value */
    int optlen          /* option length */
    )
bind:bind a name to a socket This routine associates a network address (also referred to as its "name") with a specified socket so that other processes can connect or send to it. When a socket is created with socket(), it belongs to an address family but has no assigned name.
1
2
3
4
5
6
bind
    (
    int s,                      /* socket descriptor */
    struct sockaddr *name,      /* name to be bound */
    int namelen                 /* length of name */
    )
getsockname - get a socket name This routine gets the current name for the specified socket s. The parameter namelen should be initialized to indicate the amount of space referenced by name. On return, the name of the socket is copied to name and the actual size of the socket name is copied to .
1
2
3
4
5
6
7
getsockname
    (
    int s,                      /* socket descriptor */
    struct sockaddr *name,      /* where to return name */
    int *namelen                /* space available in name, later */
                                /* filled in with actual name size */
    )
recvfrom - receive a message from a socket This routine receives a message from a datagram socket regardless of whether it is connected. If from is non-zero, the address of the sender's socket is copied to it. The value-result parameter pFromLen should be initialized to the size of the from buffer. On return, pFromLen contains the actual size of the address stored in from.
1
2
3
4
5
6
7
8
9
10
recvfrom
    (
    FAST int             s,         /* socket to receive from */
    FAST char            *buf,      /* pointer to data buffer */
    FAST int             bufLen,    /* length of buffer */
    FAST int             flags,     /* flags to underlying protocols */
    FAST struct sockaddr *from,     /* where to copy sender's addr */
    FAST int             *pFromLen  /* value/result length of <from> */
    )
</from>
sendto - send a message to a socket This routine sends a message to the datagram socket named by to The socket s is received by the receiver as the sending socket.
1
2
3
4
5
6
7
8
9
10
int sendto
    (
    FAST int             s,             /* socket to send data to */
    FAST caddr_t         buf,           /* pointer to data buffer */
    FAST int             bufLen,        /* length of buffer */
    FAST int             flags,         /* flags to underlying protocols */
    FAST struct sockaddr *to,           /* recipient's address */
    FAST int             tolen          /* length of <to> sockaddr */
    )
</to>
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示