What is a socket?
Sockets originate from UNIX, and one of Unix/linux's basic philosophies is that "all files" can be manipulated using the "open open–> Read and write write/read–> close" mode. In fact, the socket is an implementation of the pattern, the socket is a special kind of file, some socket function is the operation of it (read/write Io, open, close)
Socket function is used for network communication, network communications generally refers to the process of communication between different hosts, such as QQ on my computer and QQ on your computer to achieve communication, is the process of sending data between.
A process is identified locally with a PID, in which the network layer "IP address " of the TCP/IP protocol uniquely identifies the host in the network, and the Transport layer's protocol + port uniquely identifies the application (process) in the host. (IP address, protocol, port) can identify the process of the network, process communication in the network can use this flag to interact with other processes
Basic steps for Socket server programming:
1, creating sockets, using the socket function
2, bind socket: Bind
3, listening kit Word: Listen
4,accept, receive client connection, 3 handshake occurs at this stage, this function returns a new socket
5, processing business
Socket client:
1, create socket: socket
2, Connection server: Connect
3, processing business
--------------------------------------------------------------------------------------------------------------- -------------------
Server Source:
1#include <stdio.h>2#include <sys/types.h>3#include <sys/socket.h>4#include <stdlib.h>5#include <string.h>6#include <arpa/inet.h>7#include <unistd.h>8 9 intMainintargcChar*argv[])Ten { One intSOCKFD =-1; A intBindres =-1; - intListenres =-1; - theSOCKFD = socket (af_inet, Sock_stream,0 ); - if( -1==sockfd) { -Perror ("Sock created" ); -Exit (-1 ); + } - + structsockaddr_in server; Amemset (&server,0,sizeof(structsockaddr_in)); atserver.sin_family =af_inet; -Server.sin_port =6666; -SERVER.SIN_ADDR.S_ADDR =htonl (inaddr_any); - -Bindres = Bind (SOCKFD, (structsockaddr*) &server,sizeof(server)); - if( -1==bindres) { inPerror ("sock Bind" ); -Exit (-1 ); to } + -Listenres =Listen (SOCKFD, somaxconn); the if( -1==listenres) { *Perror ("Sock Listen" ); $Exit (-1 );Panax Notoginseng } - the structsockaddr_in peerserver; + intACCEPTFD =-1; Asocklen_t len =sizeof(peerserver); theACCEPTFD = Accept (SOCKFD, (structsockaddr*) &peerserver, &len); + if( -1==acceptfd) { -Perror ("Sock Accept" ); $Exit (-1 ); $ } - - Charrecvbuf[1024x768]; the while(1 ) { -memset (Recvbuf,0,sizeof(RECVBUF));Wuyi intRecvbytes = Read (ACCEPTFD, Recvbuf,sizeof(RECVBUF)); the fputs (Recvbuf, stdout); - Write (Acceptfd,recvbuf, recvbytes); Wu } - About Close (SOCKFD); $ Close (ACCEPTFD); - - return 0; -}View Code
Client Source:
1#include <stdio.h>2#include <sys/types.h>3#include <sys/socket.h>4#include <stdlib.h>5#include <string.h>6#include <arpa/inet.h>7#include <unistd.h>8 9 intMainintargcChar*argv[])Ten { One intSOCKFD =-1; A -SOCKFD = socket (af_inet, Sock_stream,0 ); - if( -1==sockfd) { thePerror ("Sock created" ); -Exit (-1 ); - } - + structsockaddr_in server; -memset (&server,0,sizeof(structsockaddr_in)); +server.sin_family =af_inet; AServer.sin_port =6666; atSERVER.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1" ); - - intres =-1; -res = connect (SOCKFD, (structsockaddr*) &server,sizeof(server)); - if( -1==Res) { -Perror ("Sock Connect" ); inExit (-1 ); - } to + Charsendbuf[1024x768] = {0 }; - Charrecvbuf[1024x768] = {0 }; the while(Fgets (SendBuf,sizeof(SENDBUF), stdin)! =NULL) { *Write (SOCKFD, SendBuf,sizeof(SendBuf)); $Read (SOCKFD, Recvbuf,sizeof(RECVBUF));Panax Notoginseng fputs (Recvbuf, stdout); -memset (SendBuf,0,sizeof(SendBuf)); thememset (Recvbuf,0,sizeof(RECVBUF)); + } A the Close (SOCKFD); + - return 0; $}View CodeNote:
1, the parameters of the socket function: When protocol is 0 o'clock, the default protocol corresponding to type types will be selected automatically
2. What is the meaning of setting sin_addr to Inaddr_any "?
The conversion is 0.0.0.0, refers to the meaning of the machine, that is, all the IP, that is, because some of the machine more than one NIC, multi-network card, this is the meaning of all network card IP address. For example, a computer has 3 network cards, respectively connected to three networks, then this computer has 3 IP addresses, if an application needs to listen to a port, then he should listen to which network card address port? If binding a specific IP address, you can only listen to the IP address you set up the network card port, the other two network card can not listen to the port, if I need three network cards are listening, then need to bind 3 IP, it is equal to the need to manage 3 sockets for data exchange, so it is not very cumbersome? So appear inaddr_any, you only need to bind inaddr_any, manage a socket on the line, regardless of the data is from which Nic come over, as long as the binding port number came over the data, can receive
Linux socket Programming: Easy client and server