Create:
#include <sys/types.h>
#include <sys/socket.h>
int socket (int domain, int types, int protocol);
Domain
Af_unix, af_local//Address family is the file name
Af_inet//ipv4
Af_inet6//ipv6
Types
1. Stream:sock_stream//Flow
2. Datagram:sock_dgram//Datagram
The descriptor returned by the socket, called with Read and write
Close system call ends socket connection
Different domain has different address formats:
Af_unix: Defined in Sys/un.h
Struck Sockaddr_un {
sa_family_t sun_family;
Char sun_path[];
};
Af_inet: Defined in Netinet/in.h
struct SOCKADDR_IN {
short int sin_family;
unsigned short int sin_port;
struct IN_ADDR sin_addr;
}; which
Sruct in_addr {
unsigned long int s_addr;
};
Naming: To use a well-created socket, you need to name it.
#include <sys/socket.h>
int bind (int socket, const struct SOCKADDR *address, size_t Address_len); succes return 0.
To establish a socket queue: save an unhandled request
#include <sys/socket.h>
int listen (int socket, int backlog); The maximum number of backlog bits saved successfully returned 0
Receive connection:
After the server program establishes and names the socket, it can wait for the client to establish a connection to the socket via the Accept system call.
#include <sys/socket.h>
int accept (int socket, STRUCCT sockaddr *sddress, size_t *address_len);
15th Chapter Sockets