簡體   English   中英

htons和ntohs無法正常工作? C UNIX網絡

[英]htons and ntohs not working? C UNIX networking

我是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,反之亦然。 知道有什么問題嗎?

一個主要的問題是,你失去原來的a ,你在客戶端輸入,所以當你在后面的循環條件使用它,它可能不是你所期望的。

另一個主要問題是,例如htons中的s表示short ,而在現代PC系統上, short通常為16位,而int為32位。

嘗試改用htonlntohl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM