socket()
函式原型
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);說明
傳回值
範例
參考
Last updated
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);Last updated
struct addrinfo hints, *res;
int sockfd;
// 首先,使用 getaddrinfo() 載入位址結構:
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET、AF_INET6 或 AF_UNSPEC
hints.ai_socktype = SOCK_STREAM; // SOCK_STREAM 或 SOCK_DGRAM
getaddrinfo("www.example.com", "3490", &hints, &res);
// 使用 getaddrinfo() 取得的資訊來建立 socket
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);