设为首页 加入收藏

TOP

小小聊天室
2012-12-02 23:04:14 来源: 作者: 【 】 浏览:341
Tags:小小 聊天室
View Code
//============================================================================
// Name        : Serv.cpp
// Author      : archy
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++(www.cppentry.com), Ansi-style
//============================================================================

#include <iostream>
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/INET_Addr.h"
#include "ace/Addr.h"
#include "pthread.h"
#include <map>

using namespace std;

std::map<int,ACE_SOCK_Stream *> clientMap;

void *process_client(void* peer)
{

    ACE_TCHAR buf[64] = {0};
    int length = 0;

    while((length = static_cast<ACE_SOCK_Stream *>(peer)->recv(buf,64)) != -1)
    {

        buf[length] = '\0';

        for(std::map<int,ACE_SOCK_Stream *>::iterator iter = clientMap.begin();
                iter != clientMap.end();
                    iter ++)
        {
            if(iter->second == peer)
            {
                continue;
            }
            iter->second->send_n(buf,length);
        }
        printf("recv string %s\n",buf[4]);
    }
    static_cast<ACE_SOCK_Stream *>(peer)->close();
    return 0;
}

int main()
{
    ACE_INET_Addr port_to_listen(50000,ACE_LOCALHOST);
    ACE_SOCK_Acceptor acceptor;
    ACE_INET_Addr peer_addr;

    if(-1 == acceptor.open(port_to_listen,1))
    {
        printf("error accept open\n");
        return -1;
    }

    ACE_SOCK_Stream *peer;

    while(true)
    {
        peer = new ACE_SOCK_Stream();
        if(-1 == acceptor.accept((*peer),&peer_addr))
        {
            printf("error acceptor accept\n");
            return -1;
        }
        else
        {
            pthread_t pid;
            ACE_TCHAR addr[64] = {0};
            peer_addr.addr_to_string(addr,64);
            printf("connection from %s\n",addr);
            pthread_create(&pid,0,process_client,static_cast<void *>(peer));
            clientMap[(int)peer_addr.get_port_number()] = peer;
        }
    }

    return 0;
}

 

 

View Code
//============================================================================
// Name        : Cli.cpp
// Author      : archy
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++(www.cppentry.com), Ansi-style
//============================================================================

#include <iostream>
#include "ace/INET_Addr.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "pthread.h"
using namespace std;

void* list_recv(void* peer)
{
    char buf[64] = {0};
    int length = 0;
    while((length = static_cast<ACE_SOCK_Stream *>(peer)->recv(buf,64)) != -1)
    {
        buf[length] = '\0';
        printf("rev string %s\n",buf);
    }
    return 0;
}

int main()
{
    ACE_INET_Addr ser_addr(50000,ACE_LOCALHOST);
    ACE_SOCK_Connector connector;
    ACE_SOCK_Stream *peer = new(std::nothrow) ACE_SOCK_Stream();
    if(peer == NULL)
    {
        return -1;
    }
    if(connector.connect(*peer,ser_addr) == -1)
    {
        cout <<" connect error! "<< endl;
        return -1;
    }


    char buf[64] = "start";
    pthread_t pid;
    pthread_create(&pid,0,list_recv,static_cast<void *>(peer));
    while(gets(buf))
    {
        if(strlen(buf) == 0)
        {
            continue;
        }
        (*peer).send_n(buf,strlen(buf));
        //peer.recv(buf,64);
    }
    peer->close();
    printf("rev string %s\n",buf);
    return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇服务器设计笔记(1)-----消息的封装 下一篇为什么线程安全的ACE容器不支持[]..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: