Example #1
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget* widget = new QWidget();
    panelLayout = new QVBoxLayout(widget);
    ui->scrollArea->setWidget(widget);
    createUdpSocket();
    createTrayIcon();

    QRect frect = frameGeometry();
    frect.moveCenter(QDesktopWidget().availableGeometry().center());
    move(frect.topLeft());
}
void sendFileUDP(char *serverName, unsigned int serverPort, char *filePath)
{
    struct sockaddr_in serverAddress;
    int UdpSocketDescr;

    if ((UdpSocketDescr =
         createUdpSocket(serverName, serverPort, &serverAddress)) == -1) {
        fprintf(stderr, "Creation socket error\n");
        exit(EXIT_FAILURE);
    }
    // Set default address
    if (connect
        (UdpSocketDescr, (struct sockaddr *) &serverAddress,
         sizeof(serverAddress)) < 0) {
        perror("connect");
        exit(EXIT_FAILURE);
    }

    struct timeval tv;
    tv.tv_sec = 30;             // 30 Secs Timeout
    tv.tv_usec = 0;
    setsockopt(UdpSocketDescr, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv,
               sizeof(struct timeval));

    char requestBuf[replyBufSize];
    unsigned char flagBuf;

    FILE *file = fopen(filePath, "r+");
    if (file == NULL) {
        perror("Open file error");
        exit(EXIT_FAILURE);
    }

    long fileSize = GetFileSize(file);

    sprintf(requestBuf, "%s:%ld", basename(filePath), fileSize);
    int bytesTransmitted =
        send(UdpSocketDescr, requestBuf, sizeof(requestBuf), 0);
    if (bytesTransmitted < 0) {
        perror("send");
        exit(EXIT_FAILURE);
    }
    bytesTransmitted = recv(UdpSocketDescr, &flagBuf, sizeof(flagBuf), 0);
    if (bytesTransmitted == -1 || flagBuf != ACK) {
        fprintf(stderr, "No server response\n");
        exit(EXIT_FAILURE);
    }

    printf("Start sending file.\n");
    char buf[bufSize];
    long totalBytesSent = 0;
    size_t bytesRead;

    // Sending file
    while (totalBytesSent < fileSize) {

        bytesRead = fread(buf, 1, sizeof(buf), file);
        bytesTransmitted = send(UdpSocketDescr, buf, bytesRead, 0);
        if (bytesTransmitted < 0) {
            perror("Sending error\n");
            exit(EXIT_FAILURE);
        }
        totalBytesSent += bytesTransmitted;

        bytesTransmitted =
            recv(UdpSocketDescr, &flagBuf, sizeof(flagBuf), 0);
        if (bytesTransmitted == -1 || flagBuf != ACK) {
            fprintf(stderr, "No server response\n");
            exit(1);
        }
    }
    bytesTransmitted = send(UdpSocketDescr, &END, sizeof(END), 0);
    if (bytesTransmitted < 0) {
        perror("Sending error\n");
        exit(EXIT_FAILURE);
    }

    printf("Sending file completed. Total bytes sent: %ld\n",
           totalBytesSent);
}
Example #3
0
UDP_NTWK_READER_ERRORS udpNtwkReaderThreadStart(tThreadId* lpThreadId, UdpNtwkReaderArgs* pArgs, int iNumArgs)
{
    int i;
    UdpReaderArgs* psArgs;

    psArgs = malloc(sizeof(UdpReaderArgs));
    memset(psArgs,0,sizeof(UdpReaderArgs));
    if (psArgs == NULL)
        return UDP_NTWK_READER_NO_ARGS;

    memset(lpThreadId,0,sizeof(tThreadId));
    for (i=0; i < iNumArgs; i++)
    {
        switch (pArgs[i].eArgType)
        {
            case UDP_NTWK_READER_STREAM_IP:
                psArgs->iMcastIp = CAST_POINTER_TO_INT(pArgs[i].pvArgData);
                break;
            case UDP_NTWK_READER_STREAM_PORT:
                psArgs->sMcastPort = CAST_POINTER_TO_SHORT(pArgs[i].pvArgData);
                break;
            case UDP_NTWK_READER_NETWORK_INT:
                psArgs->iMcastIntIp = CAST_POINTER_TO_INT(pArgs[i].pvArgData);
                break;
            case UDP_NTWK_READER_RECIEVE_QUEUE_NAME:
                memset(psArgs->acRecieveQueueName,0,MAX_QUEUE_NAME);
                strcpy(psArgs->acRecieveQueueName,(char*)(pArgs[i].pvArgData));
                break;
            case UDP_NTWK_READER_OUTGOING_QUEUE_NAME:
                memset(psArgs->acOutgoingQueueName,0,MAX_QUEUE_NAME);
                strcpy(psArgs->acOutgoingQueueName,(char*)(pArgs[i].pvArgData));
                break;
            default:
                DEBUG_PRINT("UdpNtwkReader: Unknown argument type %d\n",pArgs->eArgType);
                break;
        }
    }

    if (createUdpSocket(&(psArgs->iSocketFd)) != UDPSOCK_NO_ERROR)
    {
        DEBUG_PRINT("UdpNtwkReader: Failed to create socket\n");
        free(psArgs);
        return UDP_NTWK_FAILED_INIT;
    }
    if (bindUdpSocket(&(psArgs->iSocketFd),psArgs->iMcastIp,psArgs->sMcastPort) != UDPSOCK_NO_ERROR)
    {
        DEBUG_PRINT("UdpNtwkReader: Failed to bind socket to multicast Ip and Port\n");
        closeUdpSocket(&(psArgs->iSocketFd),0);
        free(psArgs);
        return UDP_NTWK_FAILED_INIT;
    }
#if 0
    /*this works for root only*/
    if (bindUdpSocketToNtwkInt(&(psArgs->iSocketFd), psArgs->iMcastIntIp) != UDPSOCK_NO_ERROR)
    {
        DEBUG_PRINT("UdpNtwkReader: Failed to bind socket to physical interface\n");
        closeUdpSocket(&(psArgs->iSocketFd),0);
        free(psArgs);
        return UDP_NTWK_FAILED_INIT;
    }
#endif
    if (joinMcastUdpSocket(&(psArgs->iSocketFd),psArgs->iMcastIntIp,psArgs->iMcastIp) != UDPSOCK_NO_ERROR)
    {
        DEBUG_PRINT("UdpNtwkReader: Failed to join multicast group\n");
        closeUdpSocket(&(psArgs->iSocketFd),0);
        free(psArgs);
        return UDP_NTWK_FAILED_INIT;
    }

    psArgs->bExitThread = false;
    if (createPosixThread(&(psArgs->liThreadID),threadFunct,psArgs) != PTHREAD_NO_ERROR)
    {
        free(psArgs);
        closeUdpSocket(&(psArgs->iSocketFd),0);
        return UDP_NTWK_READER_THREAD_LAUNCH_ERROR;
    }
	memcpy(lpThreadId,&(psArgs->liThreadID),sizeof(tThreadId));
    return UDP_NTWK_READER_NO_ERROR;
}