Socket socket TCP Apisocket concept
- Socket, also known as "socket", is a network of interprocess communication data channel of an endpoint, or called a handle. IP address + port number you can uniquely identify a socket.
- The TCP/IP protocol family includes the transport layer (TCP/UDP), the network layer (ICMP/IP/IGMP), and the link layer (ARP/RARP). The application layer typically uses the socket address, which is the IP address + port number, to determine the peer of the communication. And socket is the TCP/IP protocol family and application layer between the interface layer, can be said to the upper layer provides a TCP/IP protocol family package, do not care about the lower level of implementation.
- Applications typically use higher-level protocol libraries to program, and sockets are more categorized in the underlying driver programming. But it's always good to be familiar with sockets.
Socket address Structure
Socket basic TCP API
Connect function
Bind function
Listen function
Accept function
Close function
- The default behavior of the close one TCP socket is to return the socket after it is marked as closed. But it triggered four wave-waving processes.
int close(int sockfd);
Transferring data
- A buffer is usually required and then sent using the recv, send function
- Read and write are also available on the *nix system
int recv(int sockfd, void *buf, size_t len, int flags);int send(int sockfd, const void *buf, size_t len, int flags);
TCP Communication client and server-side client processes
- Connect to a server with connect and start transferring data
Server-side processes
- You need to bind the network interface (BIND) First, then enter the listening state (listen), and finally take out a connected socket from the queue, that is, obtain a new connection (accept), and then start transferring data
The source code is as follows:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h>int Main () {struct sockaddr_in local; int s; int SL; int RC; Char buf[1000]; local.sin_family = af_inet; Local.sin_port = htons (7500); LOCAL.SIN_ADDR.S_ADDR = htonl (Inaddr_any); s = socket (af_inet, sock_stream, 0); if (s < 0) {perror ("Socket call failed"); Exit (1); rc = bind (s, struct sockaddr *) &local, sizeof (local)); if (RC < 0) {perror ("bind call failed"); Exit (1); rc = Listen (s, 5); if (RC < 0) {perror ("Listen call failed"); Exit (1); } SL = Accept (s, null, NULL); if (SL < 0) {perror ("Accept call failed"); Exit (1); rc = recv (SL, buf, 10, 0); if (RC < 0) {perror ("recv call failed"); Exit (1); } printf ("%s\n", buf); rc = Send (SL, "good", 10, 0); if (RC < 0) {perror ("Send call failed"); EXIT (1); } exit (0);}
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h># Include<arpa/inet.h> #include <stdio.h>int main () {struct sockaddr_in peer; int s; int RC; Char buf[100]; peer.sin_family = af_inet; Peer.sin_port = htons (7500); PEER.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); s = socket (af_inet, sock_stream, 0); if (s < 0) {perror ("Socket call failed"); Exit (1); rc = Connect (s, (struct sockaddr *) &peer, sizeof (peer)); if (RC) {perror ("Connect call failed"); Exit (1); rc = Send (S, "Hello", 10, 0); if (RC <= 0) {perror ("Send call failed"); Exit (1); rc = recv (S, buf, 10, 0); if (RC <= 0) {perror ("recv call failed"); } else printf ("%s\n", buf); Exit (0);}
Reprint please specify FOCUSTC, blog address for HTTP://BLOG.CSDN.NET/CAOZHK, the original link for Click Open