Data reading and writing are divided
TCP Data read/write, UDP data read/write, general data Read/write
1.TCP Data read/write
Definition: Socket is the connection between user space and kernel space, TCP and UDP are the first layer of the kernel.
Explanation: Read and write for files also apply to the socket
Example: System call for TCP streaming data
1.1ssize_t recv (int sockfd,void
buf,size_t len,int flags);
1.2ssize_t Send (int sockfd,const voidbuf,size_t len,int flags);
Parameter explanation:
1.3buf and Len refer to the location and size of the buffer
1.3.1flags is typically 0
Recv successfully returns the length of the data actually read to
Note: The actual read length may be less than the expected Len, so multiple calls to Recv
Recv may return 0, meaning that the communication partner has closed the connection
Recv Error Return-1 and set errno
1.4send basically ditto
Instance code to send
int main (int argc, char *argv[]) {if (argc<=2) {printf ("parameter is Error"); }//A series of previous connections if (connect (sockfd, struct sockaddr*) &server_address,sizeof (server_address) ) {<0) {printf ("Connection failed"); }else{const char* Oob_data = "abc"; Const char* Normal_data = "123"; Send (Sockfd,normal_data,strlen (normal_data), 0); Sned (Sockefd,oob_data,strlen (oob_data). MSG_OOB); Send (Sockfd,normal_data,strlen (normal_data), 0); } close (SOCKFD); return 0; }//Receive code is similar to//connection succeeded int connfd = Accept (sock, (struct sockaddr *) &client,&client_addrlengt h); Char Buffer[buf_size]; memset (buffer, ' n ', buf_size); ret = recv (connfd,buffer,buf_size-1,0); Close (CONNFD); Close (sock); return 0; ./TESTOOBRECV 192.168.1.109 54321./testoobsend 192.168.1.109 54321 tcpdump-ntx-i eth0 Port 54321 Note: Flags parameter Only the current invocation of send and recv takes effect, and you can use the Setsockket system call to permanently modify some of the socket's properties
Linux High Performance Network programming reading notes socket data read and write