交流电插座停留在读从发送86到64时 [英] C Socket stuck in read when sending from x86 to x64

查看:94
本文介绍了交流电插座停留在读从发送86到64时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写类项目一个微小的电子邮件服务器/客户端。当我同一台机器上运行的客户端和服务器,一切工作正常。当我运行一个x86机器和客户的x64机器上的服务器,我被困在读。当我运行在x64机器和客户端x86计算机上,请阅读服务器完成,但是当我打印字符串,它的空白。下面是我用读功能

I'm writing a tiny email server / client for class project. When I run the client and server on same machine, everything works fine. When I run the server on a x86 machine and client on x64 machine, I am stuck in read. When I run the server on a x64 machine and client on x86 machine, read is complete, but when I print the string, its blank. Following is the function I use to read

/*----------------------------------------------------------------*/
int readn(int sd, char *buf, int n) {
    printf("readn via utils. %d, %s, %d\n", sd, buf, n);
    int toberead;
    char * ptr;

    toberead = n;
    ptr = buf;
    while (toberead > 0) {
        int byteread;
        fprintf(stderr, "toberead: %d, byteread: %d\n",toberead, byteread );
        byteread = read(sd, ptr, toberead);
        fprintf(stderr, "toberead: %d, byteread: %d\n",toberead, byteread );

        if (byteread <= 0) {
            fprintf(stderr, "byteread val: %d",byteread);
            if (byteread == -1)
                perror("read");
            raise (6);
            return (0);
        }

        toberead -= byteread;
        ptr += byteread;
    }

    fprintf(stderr, "Finished readn. %s\n", buf);
    return (1);
}

使用或对readn影响的其他实用功能。

Other utility functions that use or have impact on readn

/*----------------------------------------------------------------*/

Packet *recvpkt(int sd)
{
    printf("Recvpkt via utils.\n");
    Packet *pkt;

    /* allocate space for the pkt */
    pkt = (Packet *) calloc(1, sizeof(Packet));
    if (!pkt) {
        fprintf(stderr, "error : unable to calloc\n");
        return(NULL);
    }

    /* read the message type */
    if (!readn(sd, (char *) &pkt->type, sizeof(pkt->type))) {
        free(pkt);
        return(NULL);
    }

    /* read the message length */
    if (!readn(sd, (char *) &pkt->lent, sizeof(pkt->lent))) {
        free(pkt);
        return(NULL);
    }
    pkt->lent = ntohl(pkt->lent);

    /* allocate space for message text */
    if (pkt->lent > 0) {
        pkt->text = (char *) malloc(pkt->lent);
        if (!pkt) {
            fprintf(stderr, "error : unable to malloc\n");
            return(NULL);
        }

        /* read the message text */
        if (!readn(sd, pkt->text, pkt->lent)) {
            freepkt(pkt);
            return(NULL);
        }
    }

    fprintf(stderr, "Reading packet complete succesfully.\n");

    /* done reading */
    return(pkt);
}

int sendpkt(int sd, char typ, long len, char *buf)
{
    fprintf(stderr, "Send packet via utils.\n");
    char tmp[8];
    long siz;

    /* write type and lent */
    bcopy(&typ, tmp, sizeof(typ));
    siz = htonl(len);
    bcopy((char *) &siz, tmp+sizeof(typ), sizeof(len));
    write(sd, tmp, sizeof(typ) + sizeof(len));

    /* write message text */
    if (len > 0)
        write(sd, buf, len);
    return(1);
}

void freepkt(Packet *pkt)
{
    fprintf(stderr, "Freeing packet.\n");
    free(pkt->text);
    free(pkt);
}

在第一种情况下上面的输出(86到64)低于

Output of the above in first case (x86 to x64) is below.

readn via utils. 3, , 1
toberead: 1, byteread: 0
toberead: 1, byteread: 1
Finished readn. 
readn via utils. 3, , 8
toberead: 8, byteread: 1
toberead: 8, byteread: 8
Finished readn. 
readn via utils. 3, , 57
toberead: 57, byteread: 58
toberead: 57, byteread: 53
toberead: 4, byteread: 53

函数的输出从64到86是低于

Output of the function from x64 to x86 is below

readn via utils. 3, , 1
toberead: 1, byteread: -1079631112
toberead: 1, byteread: 1
Finished readn. 
readn via utils. 3, , 4
toberead: 4, byteread: 1
toberead: 4, byteread: 4
Finished readn. 
readn via utils. 3, , 57
toberead: 57, byteread: -1079631112
toberead: 57, byteread: 57
Finished readn. 
Reading packet complete succesfully.
>> 

数据应该后要打印>>

Data is supposed to be printed after >>

让我知道,如果我的问题是清楚的,或者任何其他信息。我花了2天完全试图解决这一问题,但徒劳无功。

Let me know if my question is clear or if any other information. I spent 2 full days trying to fix this but in vain.

更新:函数调用readn更新

Update: Function that calls readn is updated.

推荐答案

在x64上,你正在阅读一个字节,然后8个字节(再有更多的字节)。结果
在x86上,你正在阅读一个字节,然后4个字节(当时一些字节)。结果
这是一个关于你的程序的x86和x64版本之间的不匹配的线索。

On x64, you are reading 1 byte then 8 bytes (then some more bytes).
On x86, you are reading 1 byte then 4 bytes (then some more bytes).
This is a clue about the mismatch between the x86 and x64 versions of your program.

也许你正在调用 sendn 是这样的:

Perhaps you are calling sendn like this:

long l;
l = /* something */;
sendn(sd, &l, sizeof l);

是32位宽32位Linux和64位宽64位Linux。

longs are 32 bits wide on 32-bit Linux, and 64 bits wide on 64-bit Linux.

这篇关于交流电插座停留在读从发送86到64时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆