zoukankan      html  css  js  c++  java
  • 【Linux 网络编程】常用TCP/IP网络编程函数

    (1)函数socket

     1 /****************************************************************
     2 ** 功能:创建一个套接字用于通信
     3 ** 参数:domain  指定通信协议族
     4 **       type   指定socket类型,流式套接字   SOCK_STREAM 
     5 **                             数据报套接字 SOCKDGRAM
     6 **                             原始套接字   SOCKRAW
     7 **        protocol 协议类型 (习惯上填写0)
     8 ** 返回值:成功返回非负整数,它与文件描述符类似,我们把它称为
     9 **         套接字描述符,简称套接字。失败返回-1;
    10 ****************************************************************/
    12 #include <sys/types.h>
    13 #include <sys/socket.h>
    14 
    15 int socket(int domain, int type, int protocol);

    (2)函数bind

     1 /*******************************************************************************
     2 ** 功能:绑定一个本地地址到套接字
     3 ** 参数:sockfd socket函数要返回的套接字
     4 **      addr 要绑定的地址
     5 **      addrlen 地址长度
     6 ** 返回值:成功返回0,失败返回-1
     7 *******************************************************************************/
     8 #include <sys/types.h>
     9 #include <sys/socket.h>
    10 
    11 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

    (3)函数listen

     1 /*****************************************************************************
     2 ** 功能:将套接字用于监听进入的连接
     3 ** 参数:   sockfd  socket函数返回的套接字
     4 **         backlog  规定内核为此套接字排队的最大的连接个数
     5 ** 返回值: 成功返回 0  失败返回 -1
     6 ** 
     7 ** (1)一般来说,listen函数应该在调用socket和bind函数之后,调用accept函数之前调用
     8 ** (2)对于给定的监听套接字接口,内核要维护两个队列
     9 **     <1>已由客户发送并到达服务器,服务器正在等待完成对应的TCP三次握手过程
    10 **     <2>已经完成连接的队列
    11 **
    12 ******************************************************************************/
    13 #include <sys/types.h>
    14 #include <sys/sock.h>
    15 
    16 int listen(int sockfd, int backlog);

    (4)函数accept

     1 /*********************************************************************************
     2 ** 功能:从已经完成连接队列返回第一个连接,如果已经完成连接队列为空,则阻塞
     3 ** 参数: sockfd 服务器套接字
     4 **       addr   将返回对等待的套接字地址
     5 **       addrlen 返回对等方的套接字地址长度
     6 ** 返回值:成功返回非负整数 ,失败返回-1
     7 *********************************************************************************/
     8 #include <sys/types.h>
     9 #include <sys/scoket.h>
    10 
    11 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

    (6)函数connet

     1 /***********************************************************************************
     2 ** 功能:用于建立与指定socket的连接
     3 ** 参数:sockfd     标识一个未连接的socket
     4 **       addr       指定要连接套接字的sockaddr结构体的指针
     5 **       addrlen    sockaddr结构体的字节长度
     6 ***********************************************************************************/
     7 #include <sys/types.h>
     8 #include <sys/socket.h>
     9 
    10 int connect(int sockfd, const strcut sockaddr *addr, socklen_t addrlen);
  • 相关阅读:
    Python的历史
    python excel
    excel xdr wdr
    sql 常用命令
    selenium 配置firefox
    SQL 一直恢复状态解决方法
    sqlserver 学习
    ITCHAT用法
    健身卡属性,以及业务规则,
    安装REDIS
  • 原文地址:https://www.cnblogs.com/jamesharden/p/6528225.html
Copyright © 2011-2022 走看看