Socket api-c/s Mode: Service write, client read. Io mode: Blocking

Source: Internet
Author: User
Tags htons

Scene:

1) The client obtains information from the server.

2) blocking mode.

So:

1) service side, step, socket, Addr,bind,listen, loop{accept,write,close (auto socket)},close server socket.

1.1. The service-side address must be bound to provide the service.

After 1.2.listen, each client connect causes the end of the listen queue to join the server.

1.3.accpet, when the customer address is removed from the Listen queue's team header, the kernel is established and a new automatic address socket is created on the server, and the client connects.

1.4, we can send the data to the client by using the kernel to build the socket.

1.5. Remember that each time you accept,write, you must close this socket because it is blocking mode. The client is blocked when there is no data. That is, the thread hangs, and the system wakes up the thread. No data is blocked again, hangs.

Therefore, if the service side, do not shut down, even if there is no data, the customer will still block in this statement, Statusflag=read (socketclientfd,buff,10);

Not like the man 2 read. No data will return 0.

2) Client, step: Socket,connect, loop (read)

2.1 The client establishes the socket directly. The address is provided by default by the kernel.

2.2. Peer, you must fill in the address of the service. To connect.

2.3 Loop reads a certain amount of data. Because the server sends the data, it will actively close. So when the client is in read, it can get 0 status characters when there is no data.

Defect: The service side, cyclic read Listen queue, very resource-intensive. Best of all, the kernel notifies the program when the queue is plugged in.

: The server uses a background thread to process the customer link. But it does not show that the thread is closed (after the main thread is closed, the background thread will get an error when the socket is closed, exit directly)

Knowledge Points:

1) Inet_pton (af_inet, "127.0.0.1", &serveraddr.sin_addr); The converted format is 4 bytes and is stored sequentially, for example, this is 0x7f,0,0,1

2) serveraddr.sin_port=htons (3003); Because the system and the network have different order of values, the port is 2 bytes, so the function must be converted to the big-endian word order.

3) address should not be converted, because it is 4 bytes, and each byte is a separate numeric value, there will not be more than one byte combined into a numeric problem. At first, the old thought of the headache, should not be converted.

The client and the service side use Inet_pton to be good.

Temporary not analyzed point:

As for serveraddr.sin_addr.s_addr=htonl (Inaddr_any); There is no analysis of why this is written.

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>using namespaceStd;typedefstructsockaddr_in SA;voidAccpetthread (intserverfd);intMain () {//socket->addr->bind->listen->accept (get Time)    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); Serveraddr.sin_family=af_inet; Serveraddr.sin_port=htons (3003); //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); return 0;}voidAccpetthread (intSERVERFD){     for(;;) {        intClientfd=accept (SERVERFD,0,0); Charbuff[]="hi,i AM Server"; Write (Clientfd,buff,sizeof(Buff));    Close (CLIENTFD); }}

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); Serveraddr.sin_port=htons (3003); Statusflag=connect (SOCKETCLIENTFD, (sockaddr*) &serveraddr,sizeof(SERVERADDR)); if(statusflag==-1) {perror ("Connect ()"); return-1; }    Charbuff[ One];  for(;;) {Statusflag=read (Socketclientfd,buff,Ten); if(statusflag>0) {buff[Ten]=' /'; 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 write, client 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.