Data pool + User table

Source: Internet
Author: User

data_pool.h#include<iostream> #include <vector> #include  <semaphore.h> #include "comm.h" using namespace std; #define &NBSP;_CAP_&NBSP;256CLASS&NBSP;DATA_POOL{&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC:         data_pool (INT&NBSP;SIZE=_CAP_);         void data_put (string& _in);         Void data_get (string& _out);         ~data_pool ();     private:        vector<string> pool;         int get_index;         int put_index;        int capacity;         sem_t put_sem;        sem_t  get_sem;};/ /data_pool.cpp#iNclude "Data_pool.h" Data_pool::d ata_pool (int size): Get_index (0), Put_index (0), Capacity (size), pool (size) {      sem_init (&put_sem, 0, capacity);      sem_ Init (&get_sem, 0, 0);} Void data_pool::d ata_put (string& _in) {    sem_wait (&put_sem);//p     pool[put_index++]=_in;    put_index%=capacity;     Sem_post (&get_sem);//v}void data_pool::d ata_get (string& _out) {    sem_ Wait (&get_sem);//p    pool[get_index++]=_out;    get_index%= Capacity;    sem_post (&put_sem);//v}data_pool::~data_pool () {     Sem_destroy (&put_sem);     sem_destroy (&get_sem);} udp_server.h#include<iostream> #include <sys/types.h> #include <sys/socket.h> #include < Netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <string> #include <stdlib.h># include<map> #include "comm.h" #include "data_pool.h" #define  _DEFAULT_IP_   "127.0.0.1" #define  _DEFAULT_PORT_ 8080class udp_server{    public:         udp_server (String _ip=_default_ip_,short _port=_default_port_);         void init ();         void  add_user (string& ip,struct sockaddr_in& client);         int send_data (struct sockaddr_in& client,socklen_t size,string& &NBSP;&NBSP;MSG);         int recv_data ();         int broadcast ();         ~udp_server ();     private:        string ip;         short port;        int sock;         data_pool pool;        map<string, struct sockaddr_in> user_online;};/ /udp_client.cpp#include "Udp_server.h" Udp_server::udp_server (string _ip,short _port): IP (_IP), Port (_ Port), sock ( -1) {}void udp_server::init () {    sock=socket (af_inet,sock_dgram,0);     if (sock<0)     {        print_ Log (Strerror (errno), __function__,__line__);         exit (1);     }    struct sockaddr_in local;     Local.sin_family=af_inet;    local.sin_port=htons (port);    &Nbsp;if (ip== "any")         local.sin_addr.s_addr=htonl (INADDR_ANY); &NBSP;&NBSP;&NBSP;&NBSP;ELSE&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOCAL.SIN_ADDR.S_ADDR=INET_ADDR ( Ip.c_str ());     if (Bind (sock, (struct sockaddr*) &local,sizeof (local) <0)      {        print_log (Strerror (errno), __FUNCTION__,__ LINE__);         exit (2);     }}void udp_server :: Add_user (string& ip,struct sockaddr_in& client) {    map<string , Struct sockaddr_in>::iterator iter=user_online.find (IP);     if (Iter==user_ Online.end ())     {        user_online.insert (pair <string,struct sockaddr_in> (ip,client));    }    else     &nbsP;   return;} Int udp_server::send_data (struct sockaddr_in& client,socklen_t size,string&  msg) {    ssize_t _s=sendto (Sock,msg.c_str (), Msg.size (), 0, (struct sockaddr*) & client,size);     if (_s<0)     {         print_log (Strerror (errno), __function__,__line__);    }     cout<< "Send" <<endl;    return _s;} Int udp_server::recv_data () {    struct sockaddr_in remote;     socklen_t len=sizeof (remote);    char buf[_size_];     ssize_t _s=recvfrom (sock,buf,sizeof (BUF) -1,0, (struct sockaddr*) &remote,&len);     if (_s>0)     {        buf[_s]= ' &NBSP;&NBSP;&NBSP;&NBSP;&N ';bsp;   cout<< "client: " <<buf<<endl;         string out=buf;        pool.data_put (out);         string key_ip=inet_ntoa (REMOTE.SIN_ADDR);         add_user (key_ip,remote);     }    else  if (_s==0)     {        print_log ("Client  is close ", __function__,__line__);    }    else      {        print_log (Strerror (errno), __FUNCTION__,__ line__);     }    return _s;} Int udp_server::broadcast () {    string msg;    pool.data_get (msg);     map<string,struct sockaddr_in&Gt;::iterator iter=user_online.begin ();     for (; Iter!=user_online.end (); ++iter)      {        send_data (Iter->second,sizeof (iter-> Second), msg);     }    return 0;} Udp_server::~udp_server () {    if (sock>0)     {         close (sock);     }}//makefileroot_path=$ (SHELL&NBSP;PWD) server=$ (Root_path)/serverclient=$ (Root_path)/clientcomm=$ (Root_path)/commdata_pool=$ (ROOT_PATH)/data_ poolserver_bin=udp_serverclient_bin=udp_clientinclude=-i$ (COMM) include+=-i$ (data_pool) CC=g++FLAGS=LDFLAGS=- lpthread#-staticser_src=$ (shell  ls -r $ (SERVER)  | grep -E  ' *.cpp ') SER _src+=$ (shell ls -r $ (COMM)  | grep -E  ' *.cpp ') ser_src+=$ (shell ls  -r $ (data_pool)  | grep -E  ' *.cpp ') ser_obj=$ (SER_SRC:.CPP=.O) cli_src=$ (shell  ls -r $ (CLIENT)  | grep -E  ' *. CPP ') cli_src+=$ (shell ls -r $ (COMM)  | grep -E  ' *.cpp ') cli_obj=$ (cli_src:. CPP=.O). phony:allall:$ (Server_bin)  $ (client_bin) $ (Server_bin): $ (ser_obj)     $ (CC)  -o  [email protected] $^ $ (ldflags) $ (Client_bin): $ (cli_obj)     $ (CC)  -o [email protected] $^ $ (ldflags)%.o:$ (COMM)/%.cpp    $ (CC)  -c $< $ (INCLUDE)%.o:$ (data_pool)/%.cpp    $ (CC)  -c $<  $ (include)%.o:$ (SERVER)/%.cpp    $ (CC)  -c $< $ (include)%.o:$ ( CLIENT)/%.cpp    $ (CC)  -c $< $ (INCLUDE). phony:cleanclean:    rm -f *.o $ (Server_bin)   $ (CLIENT_BIN)


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1795902

Data pool + User table

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.