Socket API-C/S Mode: Service read/write, client write read. Io mode: Blocking

Source: Internet
Author: User
Tags htons

Server: Socket->address->bind->listen->loop{accpet->read->write->close (auto socket)}->close (Listen socket)
1) because it is blocking mode, the client does not shut down when the server is blocked in the Read function,
2) Disconnect after the client sends. is an ordered piece of data. The service driven by the kernel through a sorted slice. must receive the data.
And not because close data first come to think that there is no data. Therefore, the client's behavior must be understood. That is, the write and close functions.
They are all sending packets. There are sequential, SEQ's. Including connect also sends a packet. Only work is handled by the kernel.
Connect,write,close. If it is written sequentially. The SEQ number of the sent package is continuously increasing.

Clients: Socket->connect->wirte->loop (Read)->close (client socket)
1) because it is blocking mode. Read on the server is blocked. So the client write and then read. The server is bound to receive the data, and then the next to send the data.
2) and the client is also blocking mode. So read is sure to wait for the data sent from the server. And wait until the server side of the close information. Then complete the receive data step.


Knowledge Points:

Char a[]= "" Calculates the character size and joins the Terminator
The read function reads in any symbol, including the end symbol.
snprintf (&buff,n, "", X), the actual character size is only n-1. The end symbol is automatically added.

Strlen to calculate the actual number of characters. The end symbol is not included.
1) So write with strlen to determine the actual character size.
2) Read the time. Received with an n-size char array, and explicitly the size of read is n-1. Easy to fill the characters can also be added to the end of the symbol itself.
3) before each read, the char array must be emptied to facilitate the next write.


Non-processed knowledge points:
1) because it is blocking mode. The server cannot determine that the client is no longer sending data. Because the client cannot shut down. It also waits for data to be received.
And the client does not shut down. Server, will always block in the Read function.
So this experiment is, the customer fixed send the relatively small data, the service side uses the large buffer one time to receive the customer data.
Regardless of the service end of a single reception.
Can I join the end border flag on my own? Right. It seems to have been mentioned in the book. It should be handled like this.
2) If the server does not finish reading customer data at one time. There's data inside the socket. This time send, is the Send normal? Is it because the socket has 2 buffers or is the previous buffer empty?

Server

#include <iostream>#include<sys/socket.h>//{socket_type.h}:socket,af_inet,sock_stream,pf_inet#include <netdb.h>//{<netinet/in.h>}:ipproto_tcp#include <sys/errno.h>#include<string.h>#include<stdio.h>//perror#include <fcntl.h>#include<unistd.h>//Close.#include <time.h>#include<thread>#include<arpa/inet.h>//Inet_ptonusing namespaceStd;typedefstructsockaddr_in SA;voidAccpetthread (intserverfd);intMain () {//socket->addr->bind->listen->accept (read, write)    intSERVERFD; intIntflag;    SA serveraddr; Bzero (&AMP;SERVERADDR,sizeof(SERVERADDR)); SERVERFD=socket (AF_INET,SOCK_STREAM,IPPROTO_IP); if(serverfd==-1) {perror ("Create ()"); return-1; }    //serveraddr.sin_addr.s_addr=htonl (inaddr_any);Inet_pton (Af_inet,"127.0.0.1",&serveraddr.sin_addr); Serveraddr.sin_family=af_inet; Serveraddr.sin_port=htons (3004); //Serveraddr.sin_zero??Intflag=bind (SERVERFD, (sockaddr*) &serveraddr,sizeof(sockaddr)); if(intflag==-1) {perror ("bind ()"); return-1; } Listen (SERVERFD,Ten);//max queue?Thread A=thread (ACCPETTHREAD,SERVERFD);    A.detach (); intcmd; cout<<"Exist:input"<<Endl;  for(;;) {cin>>cmd; if(cmd== the)        {             Break; }} close (SERVERFD);//after close, the address cannot be used. To wait for 2ml    return 0;}//voidAccpetthread (intserverfd) {     for(;;) {        intServertempfd=accept (SERVERFD,0,0); //Read        Charreadbuf[Wuyi]; intSizeread= Read (Servertempfd,readbuf, -); Readbuf[sizeread]=' /';//to avoid overflow, insert the end symbol.        Charwritebuff[ -]; Bzero (Writebuff, -);//to avoid overflow, write all end symbols.        intTempsize= snprintf (Writebuff, About,"%s:%d", Readbuf,strlen (READBUF)); cout<<writebuff<<Endl; cout<<strlen (Writebuff) <<endl;//Why is size 100? Is it not the eradication decision ?Write (Servertempfd,writebuff,strlen (Writebuff));    Close (SERVERTEMPFD); }}

Client

#include <iostream>#include<sys/socket.h>//{socket_type.h}:socket,af_inet,sock_stream,pf_inet#include <netdb.h>//{<netinet/in.h>}:ipproto_tcp#include <sys/errno.h>#include<string.h>#include<stdio.h>//perror#include <fcntl.h>#include<unistd.h>//Close.#include <time.h>#include<netinet/inch.h>#include<arpa/inet.h>//Inet_ptonusing namespacestd;intMain () {//Socket->connect->read.    intsocketclientfd; intStatusflag; SOCKETCLIENTFD=socket (PF_INET,SOCK_STREAM,IPPROTO_IP); if(socketclientfd==-1) {perror ("socket ()"); return-1; }    structsockaddr_in serveraddr; Bzero (&AMP;SERVERADDR,sizeof(SERVERADDR)); Serveraddr.sin_family=af_inet; Inet_pton (Af_inet,"127.0.0.1",&serveraddr.sin_addr); //printf ("%0x,%0x,%0x,%0x", ((char*) &serveraddr.sin_addr) [0], ((char*) &serveraddr.sin_addr) [1], ((char* ) &serveraddr.sin_addr) [2], ((char*) &serveraddr.sin_addr) [3]);Serveraddr.sin_port=htons (3004); Statusflag=connect (SOCKETCLIENTFD, (sockaddr*) &serveraddr,sizeof(SERVERADDR)); if(statusflag==-1) {perror ("Connect ()"); return-1; }    Charbuff[ One]; Charwritechar[ -]="hi,i am client.";//The end symbol is bound to be added. Char is 1 larger than the actual character .Statusflag= Write (Socketclientfd,writechar,strlen (Writechar));//only characters are sent when writing. Does not send a terminator. So use strlen.    if(statusflag==-1) {perror ("Write ()"); return-1; }     for(;;) {        //bzero (buff,11);//each time you want to clear, more secure. Although the back has been buff[statusflag]= ' ";Statusflag=read (Socketclientfd,buff,Ten);//any symbols are read in here. Include the end symbol. So when writing, do not send the end symbol. It is therefore required to use strlen to determine the actual character.         if(statusflag>0) {Buff[statusflag]=' /'; cout<<buff<<Flush; }        Else if(statusflag==0) {cout<<Endl;  Break; }        Else{perror ("Read ()"); return-1; }} cout<<"Eixist:input."<<Endl; intcmd;  while(1) {cin>>cmd; if(cmd== the) {close (SOCKETCLIENTFD);  Break; }    }    return 0;}

Socket API-C/S Mode: Service read/write, client write read. Io mode: Blocking

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.