使用 C++ 中的套接字传输文件应用程序到Java应用程序

发布于 2024-10-18 17:57:19 字数 2210 浏览 4 评论 0原文

我正在尝试使用套接字将文件从 C++ 应用程序传输到 Java 应用程序。我用 C++ 编写了这个简单的代码来发送文件:

int main() {
    int sock;
    struct sockaddr_in sa;
    char* memblock;

    /* Create socket on which to send. */
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0) {
        printf("opening datagram socket");
        exit(1);
    }

    /* Construct name of socket to send to. */
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = htonl(0x7F000001);
    sa.sin_port = htons(4444);

    /* Send messages. */
    FILE *file;
    unsigned long fileLength = 0;

    file = fopen(FILE_PATH, "rb");
    if (!file) {
        printf("Error opening the file.\n");
        return 1;
    }

    // Get file length.
    fseek(file, 0, SEEK_END);
    fileLength = ftell(file);
    printf("File length is: %d.\n", fileLength);
    fseek(file, 0, SEEK_SET);

    memblock = (char*)malloc(sizeof(char)*fileLength);
    if (memblock == NULL) {fputs ("Memory error",stderr); exit(2);}

    // copy the file into the buffer:
    int result = fread(memblock, 1, fileLength, file);
    if (result != fileLength) {fputs ("Reading error", stderr); exit(3);}

    int pos = 0;
    char* pMemblock = memblock;
    while (pos < fileLength) {
        int sent = sendto(sock, pMemblock, 80, 0, (struct sockaddr*)&sa, sizeof sa);
        if (sent < 0)
            printf("Error sending datagram message.\n");
        else printf("Sent %d.\n", sent);
        pos += 80;
        pMemblock += 80;
    }

    delete memblock;
    fclose(file);

    close(sock);
    return 0;
}

在 Java 端,我是这样写下收到的数据的:

while (true) {
                receiveData = new byte[300];
                receivedPacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivedPacket);
                try {
                    fou = new FileOutputStream(<filename>, true);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                fou.write(receivedPacket.getData(), 0, 80);
                fou.close();
            }

结果是我收到的文件在某种程度上是正确的,但并不完整。我的代码中 Java 或 C++ 部分有什么问题吗? 谢谢!

I'm trying to transfer a file using sockets from a C++ application to a Java application. I wrote this simple code in C++ to send the file:

int main() {
    int sock;
    struct sockaddr_in sa;
    char* memblock;

    /* Create socket on which to send. */
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0) {
        printf("opening datagram socket");
        exit(1);
    }

    /* Construct name of socket to send to. */
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = htonl(0x7F000001);
    sa.sin_port = htons(4444);

    /* Send messages. */
    FILE *file;
    unsigned long fileLength = 0;

    file = fopen(FILE_PATH, "rb");
    if (!file) {
        printf("Error opening the file.\n");
        return 1;
    }

    // Get file length.
    fseek(file, 0, SEEK_END);
    fileLength = ftell(file);
    printf("File length is: %d.\n", fileLength);
    fseek(file, 0, SEEK_SET);

    memblock = (char*)malloc(sizeof(char)*fileLength);
    if (memblock == NULL) {fputs ("Memory error",stderr); exit(2);}

    // copy the file into the buffer:
    int result = fread(memblock, 1, fileLength, file);
    if (result != fileLength) {fputs ("Reading error", stderr); exit(3);}

    int pos = 0;
    char* pMemblock = memblock;
    while (pos < fileLength) {
        int sent = sendto(sock, pMemblock, 80, 0, (struct sockaddr*)&sa, sizeof sa);
        if (sent < 0)
            printf("Error sending datagram message.\n");
        else printf("Sent %d.\n", sent);
        pos += 80;
        pMemblock += 80;
    }

    delete memblock;
    fclose(file);

    close(sock);
    return 0;
}

On the Java side this is how I write down data received:

while (true) {
                receiveData = new byte[300];
                receivedPacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivedPacket);
                try {
                    fou = new FileOutputStream(<filename>, true);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                fou.write(receivedPacket.getData(), 0, 80);
                fou.close();
            }

The result is that the file I receive is correct to a certain point, but it is not complete. Is there anything wrong in my code wither in the Java or in the C++ part?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2024-10-25 17:57:19
  • UDP 不保证所有数据报都会到达。
  • UDP 不保证所有数据报只会到达一次。
  • UDP 不保证到达的数据报将按照您发送的顺序排列。

请参阅此处

如果您仅仅因为发送者和接收者位于同一台计算机上就认为这些规则不适用,那么您就错误地认为这些规则不适用。

没有任何形式的额外可靠性和排序的 UDP 对于您想要做的事情来说根本就是错误的选择。

您应该使用 TCP(或其他可靠的传输)而不是 UDP。

  • UDP doesn't guarantee that all datagrams will arrive.
  • UDP doesn't guarantee that all datagrams will arrive only once.
  • UDP doesn't guarantee that the datagrams that do arrive will be in the sequence that you sent them.

See here.

You are wrong to assume that these rules do not apply just because your sender and receiver are on the same machine.

UDP without any form of additional reliability and sequencing is simply the wrong choice for what you want to do.

You should use TCP (or another reliable transport) rather than UDP.

红玫瑰 2024-10-25 17:57:19

只需查看您的代码,问题可能是,您编写的语句是错误的:

fou.write(receivedPacket.getData(), 0, 80);

应该是:

fou.write(receivedPacket.getData(), 0, receivedPacket.getLength());

另一个注意事项:您是否在 while 循环中有意关闭 FileOutputStream (因此每次执行循环时?(这是并不是真的错,但速度较慢)

Just by looking over your code, the problem probably is, that you're write statement is wrong:

fou.write(receivedPacket.getData(), 0, 80);

which should be:

fou.write(receivedPacket.getData(), 0, receivedPacket.getLength());

Another note: are you closing the FileOutputStream intentionally within the while loop (therefore every time you execute the loop? (this is not really wrong but slower)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文