通过C中的消息队列发送文件出错了?

时间:2014-01-14 00:33:51

标签: c linux windows message-queue

我已经为我的文件服务器创建了一个消息队列(在linux上运行),当我通过客户端将文件(从Windows客户端)上传到服务器时,似乎一切顺利。上传文件后,虽然我在服务器端获得了所有这些模糊的符号。

我在客户端获得了以下代码

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stddef.h>
#include "mailbox.h"                                                            
#define MAXCLIENTS 5
#define PORTNR 5002
#define MAXUSERS 1024

/*
int inet_aton(const char *cp, struct in_addr *inp);
char *inet_ntoa(struct in_addr in);
void *memset(void *s, int c, size_t n);
int close(int fd);
 */
//Prototyping van de functies
void *childThread(void *ad);
void uploadFile(int nsockfd);
void downloadFile(int nsockfd);
void establishConnection();
int login(char *username, char *password);

int get_queue_ds( int qid, struct msqid_ds *qbuf);
int change_queue_mode(int qid, char *mode);

//Upload files= 0, Download files= 1
int serverState = 0;

int numberLoggedInUsers = 0;

struct sockaddr_in client; // Struct for Server addr 

struct message req;
struct msqid_ds mq_id = {0};
int clientUpload;
ssize_t msgLen;
char *username;
char *password;

int main(int argc, char** argv) {
    // create message queue key
    key_t key;
    if((key = ftok("/home/MtFS/Iteraties/ftokDing", 13)) < 0) {
        perror("ftok");
        exit(1);
    }

    // create queue, if not succesfull, remove old queue
    // and try to make a new one.
    while ((clientUpload = msgget(key, 0666 | IPC_CREAT| IPC_EXCL)) < 0) { //| S_IRGRP | S_IWUSR
        perror("msgget");

        // delete message queue if it exists
        if (msgctl(clientUpload, IPC_RMID, &mq_id) == -1) {
            perror("msgctl1");
            exit(1);
        }
    }

    change_queue_mode(clientUpload, "0666");

/*
    if (msgctl(clientUpload, IPC_STAT, &mq_id) == -1) {
        perror("msgctl2");
        exit(1);
    }

    if (msgctl(clientUpload, IPC_SET, &mq_id) == -1) {
        perror("msgctl3");
        exit(1);
    }
*/

    establishConnection();

    return 0;
}

int get_queue_ds(int qid, struct msqid_ds *qbuf) {
    if (msgctl(qid, IPC_STAT, qbuf) == -1) {
        perror("msgctl IPC_STAT");
        exit(1);
    }

    return 0;
}

int change_queue_mode(int qid, char *mode) {
    struct msqid_ds tmpbuf;

    /* Retrieve a current copy of the internal data structure */
    get_queue_ds(qid, &tmpbuf);

    /* Change the permissions using an old trick */
    sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);

    /* Update the internal data structure */
    if (msgctl(qid, IPC_SET, &tmpbuf) == -1) {
        perror("msgctl IPC_SET");
        exit(1);
    }

    return (0);
}
void establishConnection() {
    pthread_t child;                                                            //Thread ID of created thread
    int sockfd;                                                                 //Integer for socket
    int nsockfd;                                                                //Integer for client socket
    socklen_t sizeAddr;                                                      //Length of socket
    struct sockaddr_in addr;                                                    //Struct for client addr
    int optValue = 1;                                                           //Int for setsockoptions
    char ipAdres[32] = "192.168.80.2";                                          //IP-adres of server
    sizeAddr = sizeof (struct sockaddr_in);

    // create socket and errorhandling    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("[socket()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("Socket created succesfully.\n\n");
    }

    // Fill socket with portno and ip address
    addr.sin_family = AF_INET;                                                  // Protocol Family
    addr.sin_port = htons(PORTNR);                                             // Portnumber
    inet_aton(ipAdres, &addr.sin_addr);                                         // Local IP- adres
    bzero(&(addr.sin_zero), 8);                                                 // empty rest of struct
    // int setsockopt (int fd, int level, int optname, const void *optval, socklen_t optlen)
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optValue, sizeof (int)) == -1) {
        perror("[setsockopt()]");
        exit(1);
    }

    // Fil socket with portnr and ip adress, also implemented error handling
    if (bind(sockfd, (struct sockaddr*) &addr, sizeof (struct sockaddr)) == -1) {
        perror("[bind()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("Portnr %d is succesfully connected %s to.\n\n", PORTNR, ipAdres);
    }

    // Listen to incoming connections and errorhandling
    if (listen(sockfd, MAXCLIENTS) == -1) {
        perror("[listen()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("Listen to port %d successfull.\n\n", PORTNR);
    }

    //Connectionloop to process connection requests from clients
    while (1) {

        //Accept incoming clients with error handling.
        if ((nsockfd = accept(sockfd, (struct sockaddr *) &client, &sizeAddr)) == -1) {
            perror("[accept()]");
            exit(1);
        } else {
            //create child thread
            pthread_create(&child, NULL, childThread, (void *) nsockfd);
            /*
                        // wait untill other child is ready
                        pthread_join(child, &status);
             */
        }
    }
}
void *childThread(void *nsockfd) {
    int sizeReceivedFile = 0;
    char receiveBuffer[PACKETSIZE]; //Buffer voor de ontvangen bestanden
    //char sendBuffer[PACKETSIZE]; //Buffer voor de te zenden bestanden
    //int sizeSendFile = 0; //Grootte van het te zenden bestand
    //char yolocol[PACKETSIZE];
    char *clientRequest;           //Char pointer voor het type request client, permissie en bestandsnaam
    int loginStatus = 0; // 0 = uitgelogd, 1 = ingelogd
    char *loggedInAs;

    printf("================================================================\n\n");
    printf("Connected with a client on IP-Adres: %s.\n\n", inet_ntoa(client.sin_addr));

    bzero(receiveBuffer, PACKETSIZE);


    while ((sizeReceivedFile = recv((int) nsockfd, receiveBuffer, PACKETSIZE, 0)) > 0) {
        // receive from client
        printf("Ontvangen buffer: %s\n",receiveBuffer);
        if (sizeReceivedFile == 0) {
            break;
        }
        // flags
        // retreive flag with strtok
        clientRequest = strtok(receiveBuffer, "#");
        printf("packet type: %s\n", clientRequest);

        // 2 = list
        // 3 = download
        // 4 = upload
        // 5 = login
        // 6 = logout

        if (strcmp(clientRequest, "2") == 0) {
            printf("execute list on current directory!\n");
        } else if (strcmp(clientRequest, "3") == 0) {
            downloadFile((int) nsockfd);
        } else if (strcmp(clientRequest, "4") == 0) {
            uploadFile((int) nsockfd);
        } else if (strcmp(clientRequest, "5") == 0){

            username = strtok(NULL,"#");
            password = strtok(NULL,"#");

            printf("Username = %s \n password = %s \n",username,password);

            int test;
            if((test= login(username,password))== 1){
                printf("login success, %i\n", test);
                loginStatus = 1;
            }
            else{
                printf("Inloggen mislukt, %i\n", test);
                loginStatus = 0;
            }

        } else if (strcmp(clientRequest, "6")== 0) {
            loginStatus = 0;
            printf("%s logged out\n", loggedInAs);
            loggedInAs = "";
        }
    }
    return 0;
}

void uploadFile(int nsockfd) {
    /*
        printf("execute download!\n");
            fileToDownload = strtok(NULL,"#");
            printf("%s",fileToDownload);


    int sizeReceivedFile = 0;

    // if relcv() returns 0 then the connection is gone
    while (sizeReceivedFile != 0) {
        //Upload of files
        if (serverState == 0) {
            sizeReceivedFile = recv((int) nsockfd, req.pakket.buffer, PACKETSIZE, 0);
            if (sizeReceivedFile < 0) {
                perror("[receive()]");
                exit(0);
            } else if (sizeReceivedFile == 0) {
                printf("The client has dropped the connection \n");
                close((int) nsockfd);
                pthread_exit(NULL);
            }

            // put the packet in the mailbox
            req.mtype = RESP_MT_DATA;                                           // has to be positive
            req.pakket.clientID = clientUpload;
            if (msgsnd(clientUpload, &req, PACKETSIZE, 0) == -1) {
                perror("msgsnd");
            }
        }
    }
    req.mtype = RESP_MT_END;
    msgsnd(clientUpload, &req, 0, 0);
    close((int) nsockfd);

    printf("================================================================\n\n");
    printf("Connection with client has been lost. Server is waiting for new clients clients...\n\n");
}

void downloadFile(int nsockfd) {
    /*
               printf("execute download!\n");
                   fileToDownload = strtok(NULL,"#");
                   printf("%s",fileToDownload);

     */
    char sendBuffer[PACKETSIZE]; 
    int sizeSendFile = 0; 


    if (send((int) nsockfd, sendBuffer, sizeSendFile, 0) < 0) {
        perror("[send()]");
        //exit(1);
    }
    bzero(sendBuffer, PACKETSIZE);

}

这是服务器端。我做了一个处理连接的过程,它将所有在我的一个自定义协议标志中称为“upload”的传入数据包传输到一个消息队列。这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stddef.h>
#include "mailbox.h" // self made header file

#define MAXCLIENTS 5
#define PORT 5002
#define MAXUSERS 1024

//Prototyping
void *childThread(void *ad);
void uploadFile(int nSockfd);
void buildConnection();

int get_queue_ds( int qid, struct msqid_ds *qbuf);
int change_queue_mode(int qid, char *mode);

// Upload files= 0, Download files= 1
int serverState = 0;

struct sockaddr_in client;

struct bericht req;
struct msqid_ds mq_id = {0};
int messageQueue;
ssize_t msgLen;

int main(int argc, char** argv) {
    // message queue key aanmaken
    key_t key;
    if((key = ftok("/home/file", 13)) < 0) {
        perror("ftok");
        exit(1);
    }

    // queue aanmaken, als dit niet lukt de eventueel oude queue verwijderen
    // en queue opnieuw proberen aan te maken.
    while ((messageQueue = msgget(key, 0666 | IPC_CREAT| IPC_EXCL)) < 0) {
        perror("msgget");

        // message queue verwijderen als deze al bestaat
        if (msgctl(messageQueue, IPC_RMID, &mq_id) == -1) {
            perror("msgctl1");
            exit(1);
        }
    }

    change_queue_mode(messageQueue, "0666");

    buildConnection();

    return 0;
}

int get_queue_ds(int qid, struct msqid_ds *qbuf) {
    if (msgctl(qid, IPC_STAT, qbuf) == -1) {
        perror("msgctl IPC_STAT");
        exit(1);
    }

    return 0;
}

int change_queue_mode(int qid, char *mode) {
    struct msqid_ds tmpbuf;

    // Retrieve a current copy of the internal data structure
    get_queue_ds(qid, &tmpbuf);

    // Change the permissions using an old trick
    sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);

    // Update the internal data structure
    if (msgctl(qid, IPC_SET, &tmpbuf) == -1) {
        perror("msgctl IPC_SET");
        exit(1);
    }

    return (0);
}

void buildConnection() {
    pthread_t child;
    int sockfd;
    int nSockfd;
    socklen_t sockaddrSize;
    struct sockaddr_in addr;
    int optValue = 1;
    char ipAdres[32] = "192.168.80.2";
    sockaddrSize = sizeof (struct sockaddr_in);

    // create socket
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("[socket()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("Socket is succesfully created.\n\n");
    }

    // fill dat socket
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    inet_aton(ipAdres, &addr.sin_addr);
    bzero(&(addr.sin_zero), 8);

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optValue, sizeof (int)) == -1) {
        perror("[setsockopt()]");
        exit(1);
    }

    if (bind(sockfd, (struct sockaddr*) &addr, sizeof (struct sockaddr)) == -1) {
        perror("[bind()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("bind succesful");
    }

    if (listen(sockfd, MAXCLIENTS) == -1) {
        perror("[listen()]");
        exit(1);
    } else {
        printf("================================================================\n\n");
        printf("Listening on port %d\n\n", PORT);
    }

    // Connection loop
    while (1) {
        // accept incoming clients
        if ((nSockfd = accept(sockfd, (struct sockaddr *) &client, &sockaddrSize)) == -1) {
            perror("[accept()]");
            exit(1);
        } else {
            pthread_create(&child, NULL, childThread, (void *) nSockfd);
        }
    }
}

void *childThread(void *nSockfd) {
    int sizeOfRecvFile = 0;
    char recvBuffer[PACKETSIZE];
    char *clientCommand; // request type

    printf("================================================================\n\n");
    printf("connected to client with IP: %s.\n\n", inet_ntoa(client.sin_addr));

    bzero(recvBuffer, PACKETSIZE);

    // get dem datas
    while ((sizeOfRecvFile = recv((int) nSockfd, recvBuffer, PACKETSIZE, 0)) > 0) {
        if (sizeOfRecvFile == 0) {
            break;
        }
        printf("received buffer: %s\n", recvBuffer);

        // handle protocol flag
        // chop protocol into pieces to check packet data and flags
        clientCommand = strtok(recvBuffer, "#");
        printf("packet type: %s\n", clientCommand);

        // if clientCommand == 4
        // incoming file!
        if (strcmp(clientCommand, "4") == 0) {
            uploadFile((int) nSockfd);
        }
    }
    return 0;
}

void uploadFile(int nSockfd) {
    int sizeOfRecvFile = 0;

    // if recv() is 0 close connection
    while (sizeOfRecvFile != 0) {
        if (serverStaat == 0) {
            sizeOfRecvFile = recv((int) nSockfd, req.pakket.buffer, PACKETSIZE, 0);
            if (sizeOfRecvFile < 0) {
                perror("[receive()]");
                exit(0);
            } else if (sizeOfRecvFile == 0) {
                printf("Client disconnected\n");
                close((int) nSockfd);
                pthread_exit(NULL);
            }

            // send packet to message queue
            req.mtype = RESP_MT_DATA;
            req.pakket.clientID = messageQueue;
            if (msgsnd(messageQueue, &req, PACKETSIZE, 0) == -1) {
                perror("msgsnd");
            }
        }
    }
    req.mtype = RESP_MT_END;
    msgsnd(messageQueue, &req, 0, 0);
    close((int) nSockfd);

    printf("================================================================\n\n");
    printf("Disconnected, now waiting for other clients...\n\n");
}

上述程序使用自定义头文件:

#include <sys/types.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <stddef.h>
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

#define PACKETSIZE 65535

struct message {
    long mtype;
    struct packet {
        int clientID;
        char buffer[PACKETSIZE];
    } packet;
};

#define REQ_MSG_SIZE (offsetof(struct message.pakket, buffer) - \
                      offsetof(struct message.pakket, clientID) - PACKETSIZE)

struct responseMsg {                                                            // Responses (server to client)
    long mtype;                                                                 // One of RESP_MT_* values below
    char data[PACKETSIZE];                                                      // File content / response message
};

// Types for response messages sent from server to client
#define RESP_MT_FAILURE 1                                                       // File couldn't be opened
#define RESP_MT_DATA 2                                                          // Message contains file data
#define RESP_MT_END 3                                                           // File data complete

我还制作了一个将上传的文件写入hdd的过程。此过程从连接过程中创建的消息队列中获取数据。代码:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include "/home/CommProces/mailbox.h"

#define POORTNR 5002
#define MAXCLIENTS 5

// prototyping
void writeFiles();

struct message resp;
int uploadMessage;
ssize_t msgLen;

int main () {
    key_t key;
    if(key = ftok("/home/CommProces/ftokDing", 13) < 0) {
        perror("ftok");
        exit(1);
    }

    uploadMessage = msgget(key, 0666);
    if (uploadMessage == -1) {
        perror("msgget");
        exit(1);
    }

    while(1) {
        writeFiles();
    }
}

void writeFiles() {
    char recvBuffer[PACKETSIZE];
    char *rights, *pathname, *filename, *temp;
    char *pathOfRecvFile;  // received files will go here

    FILE* theFile;
    bzero(recvBuffer, PACKETSIZE);
    int numMsgs, totBytes;
    int sizeOfRecvFile = 0;
    int nBytesToDisk = 0;

    totBytes = msgLen; // Count first message

    for (numMsgs = 1; resp.mtype == RESP_MT_DATA; numMsgs++) {
        msgLen = msgrcv(uploadMessage, &resp, PACKETSIZE, 0, 0);
        if (msgLen == -1) {
            perror("msgrcv");
            totBytes += msgLen;
        }
        *recvBuffer = *resp.pakket.buffer;
        //temp = strtok(recvBuffer,"#");
        rights = strtok(NULL,"#");
        if(strcmp(rights, "private") == 0) {
            temp = strtok(NULL,"#");
            pathname = strcat("/home/MtFS/UploadedFiles/private/", temp);
        } else {
            pathname = "/home/MtFS/UploadedFiles/public";
        }
        filename = strcat("/", strtok(NULL,"#"));
        pathOfRecvFile = strcat(filename, pathname);

        theFile = fopen(pathOfRecvFile, "wb");
        if(theFile == NULL) {
            printf("[Open_File] unable to create file %s\n", pathOfRecvFile);
        } else {
            nBytesToDisk = fwrite(recvBuffer, sizeof(char), sizeOfRecvFile, theFile);
            if(nBytesToDisk < sizeOfRecvFile) {
                perror("fwrite");
            }
            printf("=============================================================================================================================\n\n");
            printf("Files received and placed on HDD\n\n");
            bzero(recvBuffer, PACKETSIZE);
        }
        if (resp.mtype == RESP_MT_FAILURE) {
            printf("mtype = fail");
        } else if(resp.mtype == RESP_MT_END) {
            printf("mtype = end of data");
            fclose(theFile);
        }
    }
}

我一直在使用调试器筛选断点,但我无法确定导致问题的原因:(

1 个答案:

答案 0 :(得分:1)

对于初学者,msgrcv()的第3个参数给出了消息有效负载的大小。

所以这一行

msgLen = msgrcv(uploadMessage, &resp, PACKETSIZE, 0, 0);

应该是

msgLen = msgrcv(uploadMessage, &resp, sizeof(resp)-sizeof(resp.mtype), 0, 0);

msgLen = msgrcv(uploadMessage, &resp, sizeof(resp.packet), 0, 0);

同样在第一个参数设置为NULL的情况下调用strtok()也没有意义。最初需要调用第一个agrment指向一些0 - 终止char - 数组。


另外^ 2:尝试连接到字符串文字uinsg strcat(),如下所示:

pathname = strcat("/home/MtFS/UploadedFiles/private/", temp);

调用未定义的行为。

要修复此make pathname缓冲区而不是指针:

char pathname[PATHMAX] = "";
...

    if(strcmp(rights, "private") == 0) {
        temp = strtok(NULL,"#");
        strcpy(pathname, "/home/MtFS/UploadedFiles/private/");
        strcat(pathname, temp);
    } else {
        strcpy(pathname, "/home/MtFS/UploadedFiles/public");
    }

您发布的代码描述的是一个非平凡的系统,涉及tcp-to-mq代理(客户端)和mq-server。我强烈建议单独调试这些组件。