sys/socket.h

来源:互联网 发布:网站源码基地 编辑:程序博客网 时间:2024/05/16 19:50

#include <sys/socket.h>


1、构建socket


/* Create a new socket of type TYPE in domain DOMAIN, using
   protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
   Returns a file descriptor for the new socket, or -1 for errors. */
extern int socket (int __domain, int __type, int __protocol) __THROW;

_domain常见的两个取值
#define PF_UNIX PF_LOCAL /* POSIX name for PF_LOCAL. */
#define PF_INET 2 /* IP protocol family. */

_type常见取值为:
SOCK_STREAM
SOCK_DGRAM

eg:
int listenfd;
listenfd = socket(PF_INET, SOCK_STREAM, 0);


2、获取socket选项

/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
   into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
   actual length. Returns 0 on success, -1 for errors. */
extern int getsockopt (int __fd, int __level, int __optname,
         void *__restrict __optval,
         socklen_t *__restrict __optlen) __THROW;


3、设置socket选项

/* Set socket FD's option OPTNAME at protocol level LEVEL
   to *OPTVAL (which is OPTLEN bytes long).
   Returns 0 on success, -1 for errors. */
extern int setsockopt (int __fd, int __level, int __optname,
         __const void *__optval, socklen_t __optlen) __THROW;


eg:
int listenfd;        //listenfd is a socket fd
int reuse_addr = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(int));


4、bind

/* Give the socket FD the local address ADDR (which is LEN bytes long). */
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
     __THROW;


eg:
int listenfd;
struct sockaddr_in serveraddr;
bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));


5、监听指定socket

/* Prepare to accept connections on socket FD.
   N connection requests will be queued before further requests are refused.
   Returns 0 on success, -1 for errors. */
extern int listen (int __fd, int __n) __THROW;

eg:
#define LISTENQ 20
int listenfd;
listen(listenfd,LISTENQ); 


6、accept

/* Await a connection on socket FD.
   When a connection arrives, open a new socket to communicate with it,
   set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
   peer and *ADDR_LEN to the address's actual length, and return the
   new socket's descriptor, or -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern int accept (int __fd, __SOCKADDR_ARG __addr,
     socklen_t *__restrict __addr_len);


eg:
int listenfd;
int connfd;
struct sockaddr_in clientaddr;
size_t clientaddr_len;
clientaddr_len=sizeof(clientaddr);
connfd = accept(listenfd,(struct sockaddr*)&clientaddr,&clientaddr_len);
if(connfd < 0){
    perror("accept");


7、连接socket

/* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
   For connectionless socket types, just set the default address to send to
   and the only address from which to accept transmissions.
   Return 0 on success, -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);


8、发送消息


/* Send N bytes of BUF to socket FD. Returns the number sent or -1.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);


9、接收消息

/* Read N bytes into BUF from socket FD.
   Returns the number read or -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);


/* Create a new socket of type TYPE in domain DOMAIN, using
   protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
   Returns a file descriptor for the new socket, or -1 for errors. */
extern int socket (int __domain, int __type, int __protocol) __THROW;

_domain常见的两个取值
#define PF_UNIX PF_LOCAL /* POSIX name for PF_LOCAL. */
#define PF_INET 2 /* IP protocol family. */

_type常见取值为:
SOCK_STREAM
SOCK_DGRAM

eg:
int listenfd;
listenfd = socket(PF_INET, SOCK_STREAM, 0);

======================================================================

/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
   into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
   actual length. Returns 0 on success, -1 for errors. */
extern int getsockopt (int __fd, int __level, int __optname,
         void *__restrict __optval,
         socklen_t *__restrict __optlen) __THROW;


======================================================================

/* Set socket FD's option OPTNAME at protocol level LEVEL
   to *OPTVAL (which is OPTLEN bytes long).
   Returns 0 on success, -1 for errors. */
extern int setsockopt (int __fd, int __level, int __optname,
         __const void *__optval, socklen_t __optlen) __THROW;


eg:
int listenfd;        //listenfd is a socket fd
int reuse_addr = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(int));


======================================================================

/* Give the socket FD the local address ADDR (which is LEN bytes long). */
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
     __THROW;


eg:
int listenfd;
struct sockaddr_in serveraddr;
bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));


======================================================================

/* Prepare to accept connections on socket FD.
   N connection requests will be queued before further requests are refused.
   Returns 0 on success, -1 for errors. */
extern int listen (int __fd, int __n) __THROW;

eg:
#define LISTENQ 20
int listenfd;
listen(listenfd,LISTENQ); 


======================================================================

/* Await a connection on socket FD.
   When a connection arrives, open a new socket to communicate with it,
   set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
   peer and *ADDR_LEN to the address's actual length, and return the
   new socket's descriptor, or -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern int accept (int __fd, __SOCKADDR_ARG __addr,
     socklen_t *__restrict __addr_len);


eg:
int listenfd;
int connfd;
struct sockaddr_in clientaddr;
size_t clientaddr_len;
clientaddr_len=sizeof(clientaddr);
connfd = accept(listenfd,(struct sockaddr*)&clientaddr,&clientaddr_len);
if(connfd < 0){
    perror("accept");


======================================================================

/* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
   For connectionless socket types, just set the default address to send to
   and the only address from which to accept transmissions.
   Return 0 on success, -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);


======================================================================


/* Send N bytes of BUF to socket FD. Returns the number sent or -1.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);


======================================================================

/* Read N bytes into BUF from socket FD.
   Returns the number read or -1 for errors.
   This function is a cancellation point and therefore not marked with
   __THROW. */
extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
1 0