【问题标题】:htons and ntohs not working? C UNIX networkinghtons 和 ntohs 不工作? C UNIX 网络
【发布时间】:2015-10-15 06:43:48
【问题描述】:

我是 C 网络编程的新手,我开始尝试将从键盘读取的数组发送到服务器。问题是,每当我从键盘读取数组的长度时,它都会增加 2/3 并且不再做任何正确的事情。例如:

我给出的长度或数组:2 客户端将从键盘读取 4 或 5 个元素,如果之后尝试打印它们,它们没有我给它们的值。他们得到一些完全不同的数字。

这是我的客户的样子:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    int sockfd;
    struct sockaddr_in server;
    /*char msg[1000], serv_reply[2000];*/


    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Error in creating the stupid sock client ");
        exit(errno);
    }

    puts("Socket created in client ");
    memset(&server, 0, sizeof(server));
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(9255);

    if ( connect(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0) {
        perror("Connection failed ");
        exit(errno);
    }

    puts("Yey! Connected");

    printf("Let's try \n" );
    printf("Nr of elem = ");
    **int a;
    scanf("%d", &a);
    a = htons(a);
    if ( send(sockfd, &a, sizeof(a), 0 ) < 0 ) {
        perror("CLient sent error");
        exit(errno);
    }
    int i, elem;
    for( i =0; i<a; i++) {
        printf("Enter a number: ");
        scanf("%d", &elem);
        elem = htons(elem);
        printf("%d", elem);
        if ( send(sockfd, &elem, sizeof(elem), 0) < 0) {
            perror("Couldn't send the damn element ");
            exit(errno);
        }
    }**
    int somth;
    recv(sockfd, &somth, sizeof(somth), 0);
    somth = ntohs(somth);
    printf("WE GOT IT %d ", somth);
    close(sockfd);
}

这是我的服务器:

#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MY_PORT 9999
#define MAXBUF 1024

int main (int argc, char* argv[]) {
    int sockfd;
struct sockaddr_in server, client;
int c, l;

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Error creating the stupid socket ");
    exit(errno);
}

memset(&server, 0, sizeof(server));
server.sin_port = htons(9255);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;

if ( bind(sockfd, (struct sockaddr *) &server, sizeof(server)) < 0) {
    perror("Error in socket binding ");
    exit(errno);
}

if ( listen (sockfd, 20) < 0) {
    perror("Erro listening to the dumb sock ");
    exit(errno);
}
printf("WE'RE ON!! " );
l = sizeof(client);
memset(&client, 0, l);

while(1) {
    int a;


    c = accept(sockfd, (struct sockaddr *) &client, &l);
        if (c == -1) {
            perror("Error accepting connection");
            continue;
        } else {     
            printf("Cool, we have a new client! \n");
        }


    if (recv( c, &a, sizeof(a), MSG_WAITALL) < 0) {
        perror("Error reading in server ");
        exit(errno);
    }

    a = ntohs(a);

    int i, elem,sum;
    for ( i = 0; i<a; i++) {
        if ( recv(c, &elem, sizeof(elem), MSG_WAITALL)< 0) {
            perror("Error reading elems ");
            exit(errno);
        }
        elem = ntohs(elem);
        sum = sum + elem;
    }
    sum= ntohs(sum);

    if ( send(c, &sum, sizeof(sum), 0) < 0) {
        perror("Error sending to client ");
        exit(errno);
    }

    close(c);
}
}

我尝试将 htons 切换为 ntohs 和相反的问题。知道有什么问题吗?

【问题讨论】:

    标签: c unix


    【解决方案1】:

    一个主要问题是您丢失了您在客户端中输入的原始a,因此当您稍后在循环条件中使用它时,它可能不是您所期望的。

    另一个主要问题是 s 在例如htons 代表 short,在现代 PC 系统上,short 通常是 16 位,而 int 是 32 位。

    尝试改用htonlntohl

    【讨论】:

    • 不仅如此,还要确保-Wall 在你的编译器标志中(可能还有-Wextra),这样你就更有可能收到关于类型不匹配的警告。
    • 我总是这样编译:gcc -Wall -o name name.c 我把所有的东西都改成了nthol/htonl,问题仍然存在......它可能是什么?
    • @Charlotte45 阅读我回答的第一段。
    • 好的,修复了随机计数问题。是的,a 丢失了,但我在应用 htonl 之前用另一个变量取 a 值来修复它。我读到的数字仍然没有正确发送。例如:我发送 1,我得到 235235
    • @Charlotte45 你在服务器上做ntohl之前还是之后检查?而且您还记得更改服务器代码以使用长版本吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多