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.