1

c を使用した UNIX ネットワーク プログラミングの学習を始めたばかりで、小さな TCP サーバー クライアント プログラムを作成しました。

Fedora では問題なく動作しますが、私の MacBook では接続に問題があります。

ターミナルでは問題なくコンパイルできますが、「サーバー」プログラムを実行してから「クライアント」プログラムを実行すると、「サーバー」が「クライアント」から「接続」コマンドを取得できなかったようです。コードとメッセージは次のとおりです。

サーバー.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

#define MYPORT 1500
#define BACKLOG 5
#define MAXDATASIZE 100

int main()
{
    int servSock, cliProc;
    socklen_t sin_size;
    char buf[MAXDATASIZE], msg[MAXDATASIZE];
    struct sockaddr_in my_addr, income_addr;

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(MYPORT);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if ((servSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        printf("Socket Error: %d\n", errno);
    else
        printf("Server Socket %d created\n", servSock);

    if (bind(servSock, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
        printf("Bind Error: %d\n", errno);
    else
        printf("Server Bind created\n");

    listen(servSock, BACKLOG);
    printf("Server is waitting for connection...\n");

    sin_size = sizeof(struct sockaddr_in);
    if ((cliProc = accept(servSock, (struct sockaddr *)&income_addr, &sin_size)) == -1)
        printf("Accept Error: %d\n", errno);
    else
    {
        printf("Server accepted connection from %s\n", inet_ntoa(income_addr.sin_addr));
    }

    sprintf(msg, "Welcome to Server, you addr is %s", inet_ntoa(income_addr.sin_addr));
    send(cliProc, msg, strlen(msg), 0);

    if (recv(cliProc, buf, MAXDATASIZE, 0) == -1)
    {
        printf("Recv Error: %d\n", errno);
    }
    else
    {
        printf("Server received %s from %s\n", buf, inet_ntoa(income_addr.sin_addr));
    }

    close(cliProc);
    close(servSock);
    printf("Server Sockets closed\n");

    return 0;
}

client.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

#define MAXDATASIZE 100
#define PORT 1500

int main(int argc, char *argv[])
{
    int cliSock, numbytes;
    char buf[MAXDATASIZE], msg[MAXDATASIZE];

    if (argc != 2)
    {
        printf("%d, Usage: Client Hostname\n", argc);
        exit(1);
    }

    struct hostent *he;

    if ((he = gethostbyname(argv[1])) == NULL)
    {
        printf("Couldn't get hostname\n");
        exit(1);
    }

    struct sockaddr_in dest_addr;

    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(PORT);
    dest_addr.sin_addr = *((struct in_addr *)he->h_addr);

    if ((cliSock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        printf("Socket Error: %d\n", errno);
    else
        printf("Client Socket %d created\n", cliSock);

    if (connect(cliSock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == 1)
        printf("Connect Error: %d\n", errno);
    else
    {
        printf("Client Connection created\n");
    }

    numbytes = recv(cliSock, buf, MAXDATASIZE, 0);
    buf[numbytes] = '\0';
    printf("Received Message: %s\n", buf);

    sprintf(msg, "4 8 15 16 23 42");
    send(cliSock, msg, MAXDATASIZE, 0);
    printf("Client sent %s to %s\n", msg, inet_ntoa(dest_addr.sin_addr));

    close(cliSock);
    printf("Client Sockets closed\n");

    return 0;
}

サーバー メッセージ:

Server Socket 3 created
Server Bind created
Server is waitting for connection...

クライアント メッセージ:

Client Socket 3 created
Client Connection created
Received Message:

どんな助けでも感謝します。:)

4

1 に答える 1

1

a)サーバーに検証を追加して、listen成功したかどうかを確認します。失敗している可能性が高いです。次の方法で原因を見つけることができます。

fprintf(stderr, "Error: %s\n", strerror(errno));

b)クライアントで、以下の条件が間違った値に対してチェックされています。をチェックする必要があり!= 0ます。connect :を引用しますIf the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.

if (connect(cliSock,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr)) == 1)
    printf("Connect Error: %d\n", errno);
else
    printf("Client Connection created\n");

c)あなたのようなケースでは、通常、telnetまたはnetcatなどの動作することがわかっているクライアントとの接続をテストすることをお勧めします。これにより、検証領域を減らすことができます。接続が成功しない場合、問題はサーバーにあります。それ以外の場合は、クライアントに問題があります。もちろん、両方に問題がある可能性もありますが、一度に 1 つのバグを修正する必要があります。

于 2013-01-04T17:45:46.130 に答える