⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pipmsgsv.cxx

📁 使用stl技术,(还没看,是听说的)
💻 CXX
字号:
/*
 *
 *  C++ Portable Types Library (PTypes)
 *  Version 1.8.3  Released 25-Aug-2003
 *
 *  Copyright (c) 2001, 2002, 2003 Hovik Melikyan
 *
 *  http://www.melikyan.com/ptypes/
 *  http://ptypes.sourceforge.net/
 *
 */

#ifdef WIN32
#  include <winsock2.h>
#else
#  include <sys/time.h>
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#  include <unistd.h>
#  include <time.h>
#endif


#include "pinet.h"


PTYPES_BEGIN


//
// ipmsgserver: IPv4 UDP socket server
//


ipmsgserver::ipmsgserver()
    : ipsvbase(SOCK_DGRAM), ippeerinfo(), handle(invhandle)
{
}


ipmsgserver::~ipmsgserver()
{
    close();
}


void ipmsgserver::dobind(ipbindinfo* b)
{
    sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(ushort(b->get_port()));
    sa.sin_addr.s_addr = b->get_ip();
    if (::bind(b->handle, (sockaddr*)&sa, sizeof(sa)) != 0)
        error(*b, usockerrno(), "Couldn't bind address");
}


void ipmsgserver::close()
{
    if (!active)
        return;
    ipsvbase::close();
    handle = invhandle;
    ippeerinfo::clear();
}


bool ipmsgserver::poll(int i, int timeout)
{
    if (!active)
        open();
    return dopoll(&i, timeout);
}


int ipmsgserver::receive(char* buf, int count)
{
    if (!active)
        open();
    ippeerinfo::clear();

    // determine which socket has pending data
    int i = -1;
    if (!dopoll(&i, -1))
        error(*this, EINVAL, "Couldn't read");
    ipbindinfo* b = (ipbindinfo*)addrlist[i];
    handle = b->handle;

    // read data
    sockaddr_in sa;
    psocklen len = sizeof(sa);
    int result = ::recvfrom(handle, buf, count, 0, (sockaddr*)&sa, &len);
    if (result < 0)
        error(*b, usockerrno(), "Couldn't read");

    // set up peer ip and port
    ip = sa.sin_addr.s_addr;
    port = ntohs(sa.sin_port);
    return result;
}


string ipmsgserver::receive(int max)
{
    string result;
    setlength(result, max);
    int numread = receive(pchar(pconst(result)), max);
    setlength(result, numread);
    return result;
}


void ipmsgserver::send(const char* buf, int count)
{
    if (!active || handle == invhandle || ip == ipnone)
        error(*this, EINVAL, "Couldn't write");  // must receive() first

    sendto(buf, count, get_ip(), get_port());
}


void ipmsgserver::sendto(const char* buf, int count, ipaddress ip, int port)
{
    if (!active || handle == invhandle || ip == ipnone)
        error(*this, EINVAL, "Couldn't write");  // must receive() first

    sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(ushort(port));
    sa.sin_addr.s_addr = ip;
    if (::sendto(handle, buf, count, 0, (sockaddr*)&sa, sizeof(sa)) < 0)
        error(*this, usockerrno(), "Couldn't write");
}


PTYPES_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -