Esempio n. 1
0
int main(int argc, char *argv[])
{

    int ret_status;
    int i, res;
    char local_ip[IP4_MAX_LEN], remote_ip[IP4_MAX_LEN];
    uint16_t local_port = PORT_NUM, remote_port;
    uint16_t send_port, recv_port, fax_rem_port;
    uint32_t fax_rem_ip;
    char buffer[512];
    char tiff_filename[64], recv_filename[64];
    int buflen;
    int len;
    int ses_count = 0;
    int sock;
    char call_id_str[FAX_MAX_SESSION_CNT];
    sig_message_t *msg_req = NULL;
    sig_message_t *msg_resp = NULL;
    fax_session_t *f_session[FAX_MAX_SESSION_CNT][2];
    pthread_t      f_thread[FAX_MAX_SESSION_CNT][2];
    struct stat st;

    if (argc < 5) {
        printf("Not enough arguments: [REMOTE_HOST] [REMOTE_PORT] [SESSION_COUNT] [TIFF_FILE_TO_SEND]\n");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    ses_count = strtoul(argv[3], NULL, 10);

    if (ses_count > FAX_MAX_SESSION_CNT) {
        printf("Too many sessions! No more then %d supported\n", FAX_MAX_SESSION_CNT);
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strncpy(tiff_filename, argv[4], sizeof(tiff_filename));
    if (stat(tiff_filename, &st)) {
        printf("Tiff file error ('%s'): %s\n", tiff_filename, strerror(errno));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(local_ip, getLocalIP("eth0"));

    printf("Local IP is '%s:%d'\n", local_ip, local_port);

    if ((sock = socket_init(local_ip, local_port)) < 0) {
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if ((res = getHostAddr(argv[1], argv[2], &remote_addr))) {
        printf("getaddrinfo() error: %s\n", gai_strerror(res));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }


    strcpy(remote_ip, inet_ntoa(remote_addr.sin_addr));
    remote_port = ntohs(remote_addr.sin_port);

    printf("Remote IP is '%s:%d'\n", remote_ip, remote_port);

    for(i = 0; i < ses_count; i++)
    {
        sprintf(call_id_str, "0000000%d", i + 1);

        send_port = FAX_SEND_PORT_START + i;
        recv_port = FAX_RECV_PORT_START + i;

        f_session[i][0] = f_session[i][1] = NULL;

        sig_msgCreateSetup(call_id_str,
                           ntohl(inet_addr(local_ip)), send_port,
                           ntohl(inet_addr(local_ip)), recv_port,
                           FAX_MODE_GW_GW, (sig_message_setup_t **)(&msg_req));


        sig_msgCompose(msg_req, buffer, sizeof(buffer));

        buflen = strlen(buffer);
        printf("buffer_len: %d buffer: '%.*s'", buflen, buflen, buffer);

        if ((len = sendPacket((uint8_t *)buffer, buflen)) != -1) {
            printf("Data sent to %s:%d: len=%d buf='%s'\n",
                   inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port),
                   buflen, buffer);
        } else {
            perror("sendPacket() failed");
            ret_status = EXIT_FAILURE;
            goto _exit;
        }

        if ((len = recvPacket((uint8_t *)buffer, sizeof(buffer))) != -1) {
            printf("Data received form %s:%d len=%d buf='%s'\n",
                   inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port),
                   len, buffer);
        } else {
            perror("recvfrom() failed");
            ret_status = EXIT_FAILURE;
        }

        sig_msgParse(buffer, &msg_resp);
        sig_msgPrint(msg_resp, buffer, sizeof(buffer));

        printf("MSG Response: %s\n", buffer);

        fax_rem_ip = ((sig_message_ok_t *)msg_resp)->ip;
        fax_rem_port = ((sig_message_ok_t *)msg_resp)->port;

        /* Sending thread */
        f_thread[i][0] = start_fax_session(SEND, call_id_str, send_port,
                                           fax_rem_ip, fax_rem_port,
                                           tiff_filename,
                                           &(f_session[i][0]));

        /* If we do without fork() - received filed will be distorted,
         * don't know why.
         *
         * Most of all - spandsp bug, not sure
         */
        if(!fork())
        {
            if(msg_resp->type != FAX_MSG_OK) exit(1);


            sprintf(recv_filename, "received_fax_%02d.tif", i);

            /* Receiving thread */
            f_thread[i][1] = start_fax_session(RECV, call_id_str, recv_port, 0,
                                               0, recv_filename,
                                               &f_session[i][0]);

            pthread_join(f_thread[i][1], NULL);
            if(f_session[i][1]) free(f_session[i][1]);

            exit(0);
        }

        sig_msgDestroy(msg_req);
        sig_msgDestroy(msg_resp);

        msg_req = NULL;
        msg_resp = NULL;
    }

    for(i = 0; i < ses_count; i++)
    {
        pthread_join(f_thread[i][0], NULL);
    }

    for(i = 0; i < ses_count; i++)
    {
        if(f_session[i][0]) free(f_session[i][0]);
    }

_exit:
    if (sock > -1) {
        shutdown(sock, SHUT_RDWR);
        close(sock);
    }

    return ret_status;
}
Esempio n. 2
0
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpclient = new TcpClient(this);
    movie = new QMovie(":/new/prefix1/image/fan_m40x40.gif");
    timer = new QTimer(this);
    ui->fan_vl->setMovie(movie);
    connect(tcpclient,SIGNAL(diplayMessage(QString)),this,SLOT(diplayMessage(QString)));
    connect(tcpclient,SIGNAL(displayValue(int,QString,QString)),this,SLOT(displayValue(int,QString,QString)));
    connect(this,SIGNAL(sendMessage(int,int,QString)),tcpclient,SLOT(sendMessage(int,int,QString)));
    connect(this,SIGNAL(newConnect()),tcpclient,SLOT(newConnect()));
    connect(timer, SIGNAL(timeout()), this, SLOT(qtimeSlot()));
    timer->start(20000);
    ui->label_ip->setText(getLocalIP());
    /*全局变量初始化*/
    textBrowser_flag = true;
    led_flag = true;
    fan_flag = true;
    led_show_flag = true;
    /*按键事件效果*/
    ui->pushButton_sensor->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/sensor.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/sensor.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/sensor_OutLight.png);}");
    ui->pushButton_debug->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/debug.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/debug.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/debug_OutLight.png);}");
    ui->pushButton_left->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/left.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/left.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/left_OutLight.png);}");
    ui->pushButton_right->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/right.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/right.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/right_OutLight.png);}");
    ui->pushButton_connect->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/push_down.png);}"
    "QPushButton{color: rgb(255, 255, 255);}");
    ui->pushButton_getip->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/push_down.png);}"
    "QPushButton{color: rgb(255, 255, 255);}");
    ui->pushButton_suspend->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/push_down.png);}"
    "QPushButton{color: rgb(255, 255, 255);}");
    ui->pushButton_clear->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:hover{border-image: url(:/new/prefix1/image/push_up.png);}"
    "QPushButton:pressed{border-image: url(:/new/prefix1/image/push_down.png);}"
    "QPushButton{color: rgb(255, 255, 255);}");

    ui->label_31->hide();
    ui->led_id->hide();
    ui->led_tp->hide();
    ui->led_vl->hide();
    ui->pushButton_led->hide();
    ui->widget2->setGeometry(410,0,375,311);

    int i;
    for(i = 0; i < 13; i ++)
        num_flag[i]=0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
    int sock = -1;
    int ret_status = 0;
    struct sockaddr_in local_addr;
    struct sockaddr_in remote_addr;
    int reuse = 0;
    int syncnt = 1;
    int i, res;
    char local_ip[IP4_MAX_LEN], remote_ip[IP4_MAX_LEN];
    unsigned local_port = PORT_NUM, remote_port;
    char buf[1024] = {0};
    int len;

    if (argc < 3) {
        printf("Not enough arguments: [REMOTE_HOST] [REMOTE_PORT]\n");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(local_ip, getLocalIP("eth0"));

    printf("Local IP is '%s:%d'\n", local_ip, local_port);

    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = inet_addr(local_ip);
    local_addr.sin_port = htons(local_port);

    if ((res = getHostAddr(argv[1], argv[2], &remote_addr))) {
        printf("getaddrinfo() error: %s\n", gai_strerror(res));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(remote_ip, inet_ntoa(remote_addr.sin_addr));
    remote_port = ntohs(remote_addr.sin_port);

    printf("Remote IP is '%s:%d'\n", remote_ip, remote_port);

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

//    fcntl(sock, F_SETFL, O_NONBLOCK);

    signal(SIGINT, &sigint_handler);

    gl_sock = sock;

    printf("Client socket created. fd: %d\n", sock);

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
        perror("setsockopt() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (setsockopt(sock, IPPROTO_TCP, TCP_SYNCNT, &syncnt, sizeof(syncnt)) < 0) {
        perror("setsockopt() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
        perror("bind() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) < 0) {
        perror("connect() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(buf, "Test message from client.");

    for(i = 0; i < 5; i++)
    {
        if ((len = send(sock, buf, strlen(buf), 0)) != -1) {
            printf("Data sent to %s:%d: len=%d buf='%s'\n", remote_ip, remote_port,
                   len, buf);
        } else {
            perror("send() failed");
            ret_status = EXIT_FAILURE;
            goto _exit;
        }
        sleep(2);
    }

_exit:
    if (sock > -1) {
        shutdown(sock, SHUT_RDWR);
        close(sock);
    }

    return ret_status;
}
Esempio n. 4
0
void Widget::on_pushButton_getip_clicked()
{
    ui->label_ip->setText(getLocalIP());
}