TCP Packet and unpacking algorithms

Source: Internet
Author: User
Tags server port

/************************************* file name: SERVER.CTCP package and unpacking implementation algorithm author: ma hai qq:284358503email: [Email protected]*/#include<stdlib.h>#include<sys/types.h>#include<stdio.h>#include<sys/socket.h>#include<linux/inch.h>#include<string.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#defineBuf_size 1024*5//#define TCP_PACK_DEBUG 1  intMain () {intnbufuselen=0;//the length used in the buffer    intnrevoncelen=0;//the length read at a time    intnpacklen=0;//the length of each packet, including the head and two length and tail bytes    intBufsize=buf_size; unsignedCharBuf[buf_size];//the cache of Read      intI=0; intSFP,NFP;/*define two descriptors*/    structsockaddr_in S_add,c_add; intsin_size; unsigned Shortportnum=6000;/*Server Port Usage*/printf ("hello,welcome to my server!\r\n"); SFP= Socket (Af_inet, Sock_stream,0); if(-1==SFP) {printf ("socket fail! \ r \ n"); return-1; } printf ("Socket OK!\r\n"); /*populate the server port address information so that this address and port are used to listen*/bzero (&s_add,sizeof(structsockaddr_in)); S_add.sin_family=af_inet; S_add.sin_addr.s_addr=htonl (Inaddr_any);/*here the address uses full 0, i.e. all*/S_add.sin_port=htons (Portnum); /*bind port with bind*/    if(-1= = Bind (SFP, (structSOCKADDR *) (&s_add),sizeof(structSOCKADDR)))  {printf ("bind fail!\r\n"); return-1; } printf ("bind OK!\r\n"); /*start listening on the appropriate port*/    if(-1= = Listen (SFP,5) {printf ("Listen fail!\r\n"); return-1; } printf ("Listen ok\r\n"); //While (1)    {          //Char buf[1027]; //int Readlen;sin_size=sizeof(structsockaddr_in); /*The accept service side uses the function, when the call enters the blocking state, waits for the user to make the connection, when does not have the client to connect, the program stops here, does not see the subsequent printing, when has the client to connect, the program executes once, then loops again here to continue waits.            The second parameter of the accept here is used to obtain the port and address information for the client. */NFP= Accept (SFP, (structSOCKADDR *) (&c_add), &sin_size); if(-1==NFP) {printf ("Accept fail!\r\n"); return-1; } printf ("Accept Ok!\r\nserver start get connect from% #x:% #x \ r \ n", Ntohl (C_ADD.SIN_ADDR.S_ADDR), Ntohs (C_add.sin_port)); /*here, you can use write to send information to the client, or you may try to use other functions to implement*/        if(-1= = Write (NFP,"hello,welcome to my server \ r \ n", +) {printf ("Write fail!\r\n"); return-1; }  //header 0xf9 Two length byte data tail 0xf8         while(1{#ifdef Tcp_pack_debug {printf ("bufSize =%d, Nbufuselen =%d\n", Bufsize,nbufuselen); }#endif            if(bufsize>Nbufuselen) {Nrevoncelen=read (nfp,buf+nbufuselen,bufsize-Nbufuselen); Nbufuselen=nbufuselen+Nrevoncelen; #ifdef tcp_pack_debug {printf ("Nbufuselen =%d>3\n", Nbufuselen); }#endif                /*printf ("Nrevoncelen data:\n"); for (i=0; i<nrevoncelen; i++) {printf ("0x%x", Buf[i]                                &0XFF);                } printf ("\ n"); */              }            Else{printf ("buf if full\n"); }                 while(nbufuselen>3)//More than 3 bytes used            {                  /*printf ("Nbufuselen data:\n"); for (i=0; i<nbufuselen; i++) {printf ("0x%x", Buf[i]&0xff                            );                } printf ("\ n"); */#ifdef tcp_pack_debug {printf ("buf[0] = 0x%x \ n", buf[0]&0xFF); }#endif                if((buf[0]&0xFF)==0xf9)//Head{Npacklen= (buf[1]* the) +buf[2]; #ifdef tcp_pack_debug {printf ("nbufuselen=%d, npacklen=%d\n", Nbufuselen,npacklen); }#endif                      if(Nbufuselen>=npacklen)//greater than and ={#ifdef Tcp_pack_debug {printf ("buf[npacklen-1] = 0x%x \ n", buf[npacklen-1]&0xFF); }#endif                          if((buf[npacklen-1]&0xFF)==0xF8)//Tail                        {                            //find a packet of data, all the data moved forward Npacklen bytes#ifdef tcp_pack_debug {printf ("head 0x%x length%d tail 0x%x \ n", buf[0]&0xFF, npacklen,buf[npacklen-1]&0xFF); }#endif                               for(i=0; i<nbufuselen-npacklen; i++) {Buf[i]=buf[npacklen+i]; } Nbufuselen=nbufuselen-npacklen;//The bytes used are getting smaller.//continue; //don't read it.                        }                        Else{#ifdef tcp_pack_debug printf ("buf[%d-1]=0x%x, (Buf[0]!=0xf8) \ n", Npacklen, (buf[npacklen-1]!=0xF8));//this should not be done .                             for(i=0; i<5; i++) {printf ("0x%x",buf[i]&0xFF); } printf ("\ n");#endifprintf ("(buf[npacklen-1]&0xff)!=0xf8 \ n"); Nbufuselen=0;//clear the wrong package//this should not be done .                                 Break; }                    }                    Else{#ifdef tcp_pack_debug printf ("nbufuselen<=npacklen\n");#endif                         Break; }                }                Else{printf ("(buf[0]!=0xf9) \ n");//this should not be done .nbufuselen=0;//clear the wrong package//this should not be done .                     Break; }            }              /*if ((0xff&buf[0])!=0xf2) {printf ("Readle                        n =%d buf 0x%x 0x%x 0x%x \ n ", readlen,0xff&buf[0],0xff&buf[1],0xff&buf[2]); } if (readlen!=1027) printf ("Readlen =%d buf 0x%x 0x%x 0x%x \ n", R              EADLEN,0XFF&BUF[0],0XFF&BUF[1],0XFF&BUF[2]); */        }        //Close (NFP);    }    //Close (SFP);    return 0;}

TCP Packet and unpacking algorithms

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.