#include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol);
The socket system call returns a descriptor that is in many ways similar to a low-level file descriptor. When the socket has been connected to another end-point socket, you can use the read and write system calls with the descriptor to send and receive data on the socket. The close system call is used to end a socket connection.
Socket Addresses
Each socket domain requires its own address format. For an AF_UNIX socket, the address is described by a structure, sockaddr_un , defined in the sys/un.h include file.
struct sockaddr_un{ sa_family_t sun_family;/*AF_UNIX*/ char sun_path[]; /*pathname*/ };
In the AF_INET domain, the address is specified using a structure called sockaddr_in , defined in netinet/in.h , which contains at least these members:
struct sockaddr_in{ short int sin_family; /*AF_INET*/ unsigned short int sin_port; /*port number*/ struct in_addr sin_addr; /*Internet address */ };
The IP address structure, in_addr , is defined as follows:
struct in_addr{ unsigned long int s_addr; };
The for bytes of an IP address constitute a single 32-bit value; An AF_INET socket is fully described by its domain, IP address, and port number. From an application's point of view, all sockets act like file descriptors and are addressed by a unique integer value.
Naming a socket
#include <sys/socket.h> int bind(int socket, const struct sockaddr *address, size_t address_len);
Creating a socket queue
#include <sys/socket.h> int listen(int socket, int backlog);
Accepting connections:
#include <sys/socket.h> int accept(int socket, struct sockaddr *address, size_t *address_len);
The accept system call returns when a client program attempts to connect to the socket specified by the parameter socket . The client is the first pending connection from that socket's queue. The accept function creates a new socket to communicate with the client and returns its descriptor. The new socket will have the same type as the server listen socket.
The socket must have previously been named by a call to bind and had a connection queue allocated by listen . The address of calling client will be placed in the sockaddr structure pointed by address . A null pointer may be used here if the client address isn't of interest.
The address_len parameter specifies the length of the client structure.Before calling accept , addresss_len must be set to the expected address length. On return, address_len will be set to the actural length of the calling client's address structure.
Requesting connections :
#include <sys/socket.h> int connect(int socket, const struct sockaddr *addresss, size_t address_len);
Closing a socket:
You can just terminate a socket connection at the server and client by calling close , just as you would for low-level file descriptors.
例子:
client.c:
#include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> int main(){ int sockfd; int len; struct sockaddr_in address; int result; char ch = 'i'; sockfd = socket(AF_INET, SOCK_STREAM, 0); address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address.sin_port = htons(9527); len = sizeof(address); result = connect(sockfd, (struct sockaddr *)&address, len); if (result == -1){ perror("Oops, client2:"); exit(1); } write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("Char from server: %c./n", ch); close(sockfd); exit(EXIT_SUCCESS); }
server.c
#include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> int main(){ int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9527); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); listen(server_sockfd, 5); while(1){ char ch; printf("server waiting/n"); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); } }
The function, inet_addr, convert the text representation of an IP address into a form suitable for socket addressing.
Host and network byte ordering:
Client and server programs must convert their internal integer representation to the network ordering before transmission.
#include <netient/in.h> unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigned long int netlong); unsigned short int ntohs(unsigned short int netshort);
hronl : host to network long
So in the server.c:
server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9527);
and in the client.c:
address.sin_port = htons(9527);
353

被折叠的 条评论
为什么被折叠?



