C++socket Network Programming (cross-platform) combat HTTP Server (IV)

Source: Internet
Author: User
Tags ack

TCP Client


Directly with the DLL dynamic library of the last package, Linux uses so files. First generate the compilation.

#include "XTCP.h" int main (int argc,char*argv[]) {XTCP client;    GetChar (); return 0;}


TCP three-time handshake protocol detailed

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/9C/14/wKioL1lsIbKBhVaxAAED76FIjZA346.png-wh_500x0-wm_ 3-wmp_4-s_1060193470.png "title=")]cq_$4]tq4kdgyojox65ql.png "alt=" wkiol1lsibkbhvaxaaed76fijza346.png-wh_50 "/ >


The server creates, binds, and listen starts listening.

The client also creates a socket to call connect via this socket. He's a blocked

function, he is going to open the initiative, he will first send a SYN J (the protocol header will contain) the past, and then the server received this

After the package, he will send a SYN K, back ack j+1 back, then the client sends an ACK k+1 past, then the Connect function returns, and the server receives the accept return.


Why do you have to shake three times? Two times okay?

In order to ensure that the data can be transmitted correctly, and can be recovered, but the server, received J

In the certainty that this is not the right J, (for some special reasons J has changed, the server will not know.) I will still send a j+1 past, so that is, I sent the server j+1, you also have to return a k+1 to me, determine whether the value is correct. I'm sure I can.

Send the data to you normally, you can send the data to me normally.

These serial numbers, all subsequent packages are added downwards, respectively, to identify the connection,

(for example, a DDoS denial of service attack ) is not given to the server in the Accept (Ack k+1)

Then constantly send the connection signal, the server will be waiting for you (accept blocking), which will cause

The server is all blocked.



XTCP Library Connect function encapsulation

BOOL Xtcp::connect (const char *IP, unsigned short port) {//If the socket is not created if (m_sock <= 0) createsocket ();//  The connection requires this struct sockaddr_in saddr;saddr.sin_family = Af_inet;saddr.sin_port = htons (port); Local byte-order-to-network byte-Order saddr.sin_addr.s_addr = inet_addr (IP); if (Connect (M_sock, (sockaddr*) &saddr, sizeof (SADDR))! = 0) { printf ("Connect%s:%d failed!:%s\n", Ip,port,strerror (errno)); return false;} printf ("Connect%s:%d success!\n", IP, port); return true;}


The client only needs to call the above connect on the line, do not need to bind and other operations

#include "XTCP.h"


int main (int argc,char*argv[]) {xtcp client;client. Connect ("192.168.1.125", 8046); GetChar (); return 0;}

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9C/16/wKioL1lsK_SAMOFWAAOGBdl_xX4363.png-wh_500x0-wm_ 3-wmp_4-s_2788885782.png "title="%~l$ $WA (A} (8o61~f) 9[w@9.png "alt=" wkiol1lsk_samofwaaogbdl_xx4363.png-wh_50 "/ >


Linux is also very simple.

Makefile

-std=c++11 using c++11

-I.. is the path to the header file

-lpthread is Linux multithreading

-lsocket is used to the so library

Client:client.cpp g++ $+-o [email protected]-std=c++11-i. /.. /xsocket/xsocket-lpthread-lxsocket

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9C/17/wKioL1lsLqSAwMFVAAIOqiMvxSc686.png-wh_500x0-wm_ 3-wmp_4-s_4143518504.png "title=" Gg52u8qv6pu%x1pj_kibele.png "alt=" wkiol1lslqsawmfvaaioqimvxsc686.png-wh_50 "/ >


This makes it easy to connect and then send and receive data, each

Call SEND,RECV. You can try it.



TCP Programming Summary

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/9C/18/wKiom1lsMeqh9skTAAEttYAQPJg179.png-wh_500x0-wm_ 3-wmp_4-s_92539122.png "title=" in ' [) m26w32fe7rnr}dviyh.png "alt=" Wkiom1lsmeqh9sktaaettyaqpjg179.png-wh_50 "/>






This article is from the "12148490" blog, please be sure to keep this source http://12158490.blog.51cto.com/12148490/1948159

C++socket Network Programming (cross-platform) combat HTTP Server (IV)

Related Article

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.