Boost: ASIO library application in RedHat Enterprise 5 (2)

Source: Internet
Author: User
Tags what header

Before getting familiar with ASIO, familiarize yourself with the BSD socket C/S model.

Client:

#include "stdio.h"#include "stdlib.h"#include "netinet/in.h"#include "sys/socket.h"#include "sys/types.h"#include "sys/wait.h"#include "arpa/inet.h"int _clientsock;struct sockaddr_in c_addr;int main(){        _clientsock = socket(AF_INET,SOCK_STREAM,0);        c_addr.sin_family=AF_INET;        c_addr.sin_port=htons(6001);        c_addr.sin_addr.s_addr=inet_addr("127.0.0.1");        connect(_clientsock,(struct sockaddr*)&c_addr,sizeof(struct sockaddr));        char buffer[128]={'\0'};        recv(_clientsock,buffer,128,0);        printf(buffer);        //close(_clientsock);        getchar();        return 0;}

Server:

#include "stdio.h"#include "stdlib.h"#include "sys/types.h"#include "netinet/in.h"#include "sys/socket.h"#include "sys/wait.h"#include "string.h"#include "arpa/inet.h"int _listensock;int _clientsock;struct sockaddr_in s_addr,c_addr;int main(){        _listensock = socket(AF_INET,SOCK_STREAM,0);        s_addr.sin_family=AF_INET;        s_addr.sin_port=htons(6001);        s_addr.sin_addr.s_addr=INADDR_ANY;        bind(_listensock,(struct sockaddr*)&s_addr,sizeof(struct sockaddr));        listen(_listensock,4);        while(1)        {                int l = sizeof(struct sockaddr_in);                _clientsock = accept(_listensock,(struct sockaddr*)&c_addr,(socklen_t*)&l);                printf("recv a client %s\n",inet_ntoa(c_addr.sin_addr));                if(1)                {                        send(_clientsock,"helloworld",26,0);                }                //close(_clientsock);        }        return 0;}

It is not clear what header file is required for close. Cainiao.

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.