Linux High Performance Network programming reading notes socket data read and write

Source: Internet
Author: User

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 void
buf,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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.