C++ (Cpp) socket Examples

C++ (Cpp) socket - 30 examples found. These are the top rated real world C++ (Cpp) examples of socket extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
// Read the packet from the client
void AuthSocket::OnRead()
{
    uint8 _cmd;
    while (1)
    {
        if (!socket().recv_soft((char *)&_cmd, 1))
            return;

        size_t i;

        // Circle through known commands and call the correct command handler
        for (i = 0; i < AUTH_TOTAL_COMMANDS; ++i)
        {
            if ((uint8)table[i].cmd == _cmd && (table[i].status == STATUS_CONNECTED || (_authed && table[i].status == STATUS_AUTHED)))
            {
                sLog->outStaticDebug("[Auth] got data for cmd %u recv length %u", (uint32)_cmd, (uint32)socket().recv_len());

                if (!(*this.*table[i].handler)())
                {
                    sLog->outStaticDebug("Command handler failed for cmd %u recv length %u", (uint32)_cmd, (uint32)socket().recv_len());
                    return;
                }
                break;
            }
        }

        // Report unknown packets in the error log
        if (i == AUTH_TOTAL_COMMANDS)
        {
            sLog->outError("[Auth] got unknown packet from '%s'", socket().get_remote_address().c_str());
            socket().shutdown();
            return;
        }
    }
}
int netloader_activate(void) {
    struct sockaddr_in serv_addr;
    // create udp socket for broadcast ping
    netloader_udpfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (netloader_udpfd < 0)
    {
        netloader_socket_error("udp socket", errno );
        return -1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(NETLOADER_PORT);

    if(bind(netloader_udpfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
        netloader_socket_error("bind udp socket", errno );
        return -1;
    }

    if (set_socket_nonblocking(netloader_udpfd) == -1)
    {
        netloader_socket_error("listen fcntl", errno);
        return -1;
    }

    // create listening socket on all addresses on NETLOADER_PORT

    netloader_listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(netloader_listenfd < 0)
    {
        netloader_socket_error("socket", errno );
        return -1;
    }

    int rc = bind(netloader_listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    if(rc != 0)
    {
        netloader_socket_error("bind", errno);
        return -1;
    }

    if (set_socket_nonblocking(netloader_listenfd) == -1)
    {
        netloader_socket_error("listen fcntl", errno);
        return -1;
    }

    rc = listen(netloader_listenfd, 10);
    if(rc != 0)
    {
        netloader_socket_error("listen", errno);
        return -1;
    }

    return 0;
}
Example #3
0
// Cancel patch transfer
bool AuthSocket::_HandleXferCancel()
{
    sLog->outStaticDebug("Entering _HandleXferCancel");

    // Close and delete the socket
    socket().recv_skip(1);                                         //clear input buffer
    socket().shutdown();

    return true;
}
Example #4
0
void sockz(void *t) {

    struct sockaddr_in dest;
    struct pthread_args *my_data;
    pthread_t close_them_all;

    char *h,
         *p_tmp,
         *slp_tmp;

    int p,
        con,
        err,
        desc,
        slp;

    my_data = (struct pthread_args *) t;

    h = my_data->host_pthread;
    p_tmp = my_data->port_pthread;
    slp_tmp = my_data->slp_pthread;

    p = atoi(p_tmp);
    slp = atoi(slp_tmp);

    desc = socket(AF_INET,SOCK_STREAM,0);

    if((desc = socket(AF_INET,SOCK_STREAM,0)) < 0) {
        perror("sockz: Can`t create socket\n");
        exit(-1);
    }

    dest.sin_family = AF_INET;
    dest.sin_port = htons(p);
    dest.sin_addr.s_addr = inet_addr(h);
    bzero(&(dest.sin_zero),8);

    con = connect(desc,(struct sockaddr *)&dest,sizeof(dest));

    if(con < 0) {
        printf("\nsockz: Can`t connect to %s:%d\n",h,p);
        close(desc);
        exit(-1);
    }

    thread_data[0].sock_pointer = desc;
    thread_data[0].slp_pthread = slp_tmp;

    if((err = pthread_create(&close_them_all,NULL,(void*)&close_func,\
                             (void*)&thread_data[0]) == -1)) {
        printf("sockz: Can`t create thread\n");
        exit(-1);
    }

}
Example #5
0
int
main(int argc, char *argv[])
{
    struct sockaddr_un un;
    pid_t pid;
    int s;

    s = socket(PF_LOCAL, SOCK_DGRAM, 0);
    if (s == -1)
        errx(-1, "socket");
    memset(&un, 0, sizeof(un));
    un.sun_family = AF_LOCAL;
    unlink(TEST_SOCKET);
    strcpy(un.sun_path, TEST_SOCKET);
    if (bind(s, (struct sockaddr *)&un, sizeof(un)) == -1)
        errx(-1, "bind");
    pid = fork();
    if (pid == -1)
        errx(-1, "fork");
    if (pid == 0) {
        int conn;
        char buf[] = "AAAAAAAAA";

        close(s);
        conn = socket(AF_LOCAL, SOCK_DGRAM, 0);
        if (conn == -1)
            errx(-1,"socket");
        if (sendto(conn, buf, sizeof(buf), 0, (struct sockaddr *)&un,
                   sizeof(un)) != sizeof(buf))
            errx(-1,"sendto");
        close(conn);
        _exit(0);
    }

    sleep(5);

    /* Make sure the data is there when we try to receive it. */
    if (recvfrom(s, (void *)-1, 1, 0, NULL, NULL) != -1)
        errx(-1,"recvfrom succeeded when failure expected");

    (void)signal(SIGALRM, handle_sigalrm);
    if (setjmp(myjmpbuf) == 0) {
        /*
         * This recvfrom will panic an unpatched system, and block
         * a patched one.
         */
        alarm(5);
        (void)recvfrom(s, (void *)-1, 1, 0, NULL, NULL);
    }

    /* We should reach here via longjmp() and all should be well. */

    return (0);
}
Example #6
0
int openClientFd() {
    struct addrinfo hints;
    struct addrinfo *list;
    struct addrinfo *cur;

    int fd;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
    getaddrinfo(hostname, port, &hints, &list);

    for(cur=list; cur!=NULL; cur=cur->ai_next) {
        if((fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol)) < 0) {
            continue;
        }

        if(connect(fd, cur->ai_addr, cur->ai_addrlen) != -1) {
            break;
        }

        close(fd);
    }

    freeaddrinfo(list);
    if(cur == NULL) {
        return -1;
    } else {
        return fd;
    }
}