c

Definition

socket (C)

#include <sys/socket.h>
 
int socket(int domain, int type, int protocol)

socket() creates an endpoint for communication and returns a file descriptor that refers to that endpoint.

Includes: socket.h

Examples

Example: HTTP Client

See: getaddrinfo, freeaddrinfo

int main(void) {
    struct addrinfo hints = {0};
    hints.ai_family = AF_UNSPEC;     // Allow IPv4 or IPv6
    hints.ai_socktype = SOCK_STREAM; // TCP
 
    struct addrinfo *res;
    if (getaddrinfo(argv[1], "80", &hints, &res) != 0) return 2;
	
	int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (s < 0) {
        freeaddrinfo(res);
        return 3;
    }
	
	// ...
}