我的 UDP 客户端服务器出了什么问题?

发布于 2024-08-13 07:25:33 字数 3656 浏览 3 评论 0原文

我有一个 UDP 服务器,它应该等待客户端连接到它,并向其发送一个文件名字符串。现在我只想它将文件名回显给客户端。这是我的代码

服务器

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

#define MAXBUFLEN 1024

// Usage: ./server PORT
int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in server;
    size_t clientSize;
    struct sockaddr_storage client;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);
    int numbytes;

    printf("Port: %d\n", portno);

    // Create UDP Socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("Can't create socket");
        exit(-1);
    }

    // Configure socket
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET; // Use IPv4
    server.sin_addr.s_addr = INADDR_ANY; // My IP
    server.sin_port = htons(portno); // Server Port

    // Bind socket
    if ((bind(sockfd, (struct sockaddr *) &server, sizeof(server))) == -1) {
        close(sockfd);
        perror("Can't bind");
    }

    while (1) {
        printf("Waiting for data...\n");

        // Receive data from Client
        clientSize = sizeof(client);
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1,0,
                (struct sockaddr *) &client, &clientSize);

        buf[numbytes] = '\0';

        printf("client sent: %s\n", buf);

        // Rely to client
        sendto(sockfd, buf, MAXBUFLEN-1, 0,
                (struct sockaddr *) &client, &clientSize);
    }

    printf("Closing");
    close(sockfd);

    return 0;
}

客户端

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

#define MAXBUFLEN 1024

//Usage: ./client PORT IP FILE
int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server;
//  struct sockaddr_storage client;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);
    char *serverIP = argv[2];
    char *filename = argv[3];
    int numbytes;

    printf("Port: %d, IP:%s, File:%s\n", portno, serverIP, filename);

    // Create UDP Socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("Can't create socket");
        exit(-1);
    }

    // The server IP and Port
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET; // Use IPv4
    server.sin_port = htons(portno); // Server Port
    struct hostent *hp = gethostbyname(serverIP);
    memcpy((char*)&server.sin_addr, (char*)hp->h_name, hp->h_length);

    printf("Sent %s\n", filename);

    // No need to bind, just send request for file
    int serverSize = sizeof(server);
    sendto(sockfd, argv[3], strlen(argv[3]), 0,
            (struct sockaddr *) &server, serverSize);

    printf("Waiting for reply\n");

    recvfrom(sockfd, buf, MAXBUFLEN-1, 0,
            (struct sockaddr *) &server, serverSize);

    buf[numbytes] = '\0';

    printf("server sent: %s\n", buf);

    close(sockfd);
}

到目前为止,服务器只打印

Waiting for data...

一次......所以它永远不会收到请求。

客户端打印出:

./client 8083 127.0.0.1 hello
Port: 8083, IP:127.0.0.1, File:hello
Sent hello
Waiting for reply
server sent: 

请帮忙,我是 C 和 UDP 套接字世界的新手。

哦,是的,我已经阅读了 Beej 的指南...请不要只是将其作为答案发布。

I have a UDP Server that should sit and wait for a client to connect to it, and send it a string of a filename. Right now I just want it to echo back the filename to the client. Here is my code

Server

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

#define MAXBUFLEN 1024

// Usage: ./server PORT
int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in server;
    size_t clientSize;
    struct sockaddr_storage client;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);
    int numbytes;

    printf("Port: %d\n", portno);

    // Create UDP Socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("Can't create socket");
        exit(-1);
    }

    // Configure socket
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET; // Use IPv4
    server.sin_addr.s_addr = INADDR_ANY; // My IP
    server.sin_port = htons(portno); // Server Port

    // Bind socket
    if ((bind(sockfd, (struct sockaddr *) &server, sizeof(server))) == -1) {
        close(sockfd);
        perror("Can't bind");
    }

    while (1) {
        printf("Waiting for data...\n");

        // Receive data from Client
        clientSize = sizeof(client);
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1,0,
                (struct sockaddr *) &client, &clientSize);

        buf[numbytes] = '\0';

        printf("client sent: %s\n", buf);

        // Rely to client
        sendto(sockfd, buf, MAXBUFLEN-1, 0,
                (struct sockaddr *) &client, &clientSize);
    }

    printf("Closing");
    close(sockfd);

    return 0;
}

Client

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

#define MAXBUFLEN 1024

//Usage: ./client PORT IP FILE
int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server;
//  struct sockaddr_storage client;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);
    char *serverIP = argv[2];
    char *filename = argv[3];
    int numbytes;

    printf("Port: %d, IP:%s, File:%s\n", portno, serverIP, filename);

    // Create UDP Socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("Can't create socket");
        exit(-1);
    }

    // The server IP and Port
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET; // Use IPv4
    server.sin_port = htons(portno); // Server Port
    struct hostent *hp = gethostbyname(serverIP);
    memcpy((char*)&server.sin_addr, (char*)hp->h_name, hp->h_length);

    printf("Sent %s\n", filename);

    // No need to bind, just send request for file
    int serverSize = sizeof(server);
    sendto(sockfd, argv[3], strlen(argv[3]), 0,
            (struct sockaddr *) &server, serverSize);

    printf("Waiting for reply\n");

    recvfrom(sockfd, buf, MAXBUFLEN-1, 0,
            (struct sockaddr *) &server, serverSize);

    buf[numbytes] = '\0';

    printf("server sent: %s\n", buf);

    close(sockfd);
}

So far the Server just prints out

Waiting for data...

only once... so its never getting a request.

and the Client prints out:

./client 8083 127.0.0.1 hello
Port: 8083, IP:127.0.0.1, File:hello
Sent hello
Waiting for reply
server sent: 

Please help, I'm new to the world of C and UDP Sockets.

Oh yeah, and I already read Beej's Guide... please don't just post that as an answer.

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

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

发布评论

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

评论(4

内心激荡 2024-08-20 07:25:33

首先,您的服务器对 sendto 的调用存在错误。 clientSize 应该是一个值,而不是一个指针。另外,正如 Tom 所说,numbytes 应该设置为 recvfrom 的结果。

大问题可能是您将 hp->h_name 复制到服务器的 sin_addr 元素中。 h_name 是主机的名称,而不是 IP 地址。尝试在您的客户端中替换


    memcpy((char*)&server.sin_addr, (char*)hp->h_name, hp->h_length);


    memcpy((char*)&server.sin_addr, (char*)hp->h_addr_list[0], hp->h_length);

In the first place, there is a bug in the call to sendto of your server. clientSize should be a value, not a pointer. Also, as Tom said, numbytes should be set to the result of recvfrom.

The big problem is likely that you are copying hp->h_name into the sin_addr element of the server. h_name is the name of the host, not the IP address. Try replacing


    memcpy((char*)&server.sin_addr, (char*)hp->h_name, hp->h_length);

with


    memcpy((char*)&server.sin_addr, (char*)hp->h_addr_list[0], hp->h_length);

in your client.

海螺姑娘 2024-08-20 07:25:33

您客户端中的 numbytes 未初始化也未使用!再次检查您的代码...

希望这有帮助,
此致,
汤姆.

numbytes in your client is not initialised nor is it used! Check again in your code....

Hope this helps,
Best regards,
Tom.

国粹 2024-08-20 07:25:33

修复了错误!谢谢汤米。这就是我现在正在运行的。

服务器:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define MAXBUFLEN 1024

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server, client;
    int serverSize, clientSize, numbytes;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);

    if (argc < 2) {
        fprintf(stderr, "ERROR, no port provided\n");
        exit(0);
    }

    printf("Running on Port: %d\n", portno);

    // Create UDP Socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        error("Can't create socket");
    }

    // Configure Server Info
    serverSize = sizeof(server);
    bzero(&server, serverSize);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(portno);

    // Bind
    if (bind(sockfd, (struct sockaddr *) &server, serverSize) < 0) {
        error("binding");
    }

    clientSize = sizeof(struct sockaddr_in);

    while (1) {
        printf("Waiting for Client Request...\n");
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN, 0,
                (struct sockaddr *) &client, &clientSize);
        if (numbytes < 0) {
            error("recvfrom");
        }
        buf[numbytes] = '\0';

        printf("Received request for File: %s\n", buf);
        numbytes = sendto(sockfd, buf, sizeof(buf), 0,
                (struct sockaddr *) &client, clientSize);
        if (numbytes < 0) {
            error("sendto");
        }
        sendFile(sockfd, client);
    }
}

sendFile(int sockfd, struct sockaddr_in client) {

}

客户端:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

#define MAXBUFLEN 1024

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server, client;
    int serverSize;
    struct hostent *hp;
    char buf[MAXBUFLEN];
    int numbytes;
    char *serverIP = argv[1];
    int portno = atoi(argv[2]);
    char *filename = argv[3];
    int done = 1;

    if (argc != 4) {
        printf("Usage: SERVER PORT FILENAME\n");
        exit(1);
    }

    // Create Socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        error("Can't create socket");
    }

    // Configure Server Info
    server.sin_family = AF_INET;
    hp = gethostbyname(serverIP);
    if (hp == 0)
        error("Unknown host");
    bcopy((char *) hp->h_addr, (char *) &server.sin_addr, hp->h_length);
    server.sin_port = htons(portno);
    serverSize = sizeof(struct sockaddr_in);

    // Send Message
    strcat(buf, filename);
    numbytes = sendto(sockfd, buf, strlen(buf), 0, &server, serverSize);
    if (numbytes < 0)
        error("Sendto");
    bzero(buf, MAXBUFLEN);

    // Build file then set Done = 0
    while (done) {
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN, 0, &client, &serverSize);
        if (numbytes < 0)
            error("recvfrom");

        done = 0;
    }

    printf("Received the packet: %s\n", buf);
}

我还在这里找到了一个很好的例子: http://www.linuxhowtos.org/ C_C++/socket.htm

感谢大家的帮助!

Fixed the bug! Thank You Tommy. Here is what I am running right now.

Server:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define MAXBUFLEN 1024

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server, client;
    int serverSize, clientSize, numbytes;
    char buf[MAXBUFLEN];
    int portno = atoi(argv[1]);

    if (argc < 2) {
        fprintf(stderr, "ERROR, no port provided\n");
        exit(0);
    }

    printf("Running on Port: %d\n", portno);

    // Create UDP Socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        error("Can't create socket");
    }

    // Configure Server Info
    serverSize = sizeof(server);
    bzero(&server, serverSize);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(portno);

    // Bind
    if (bind(sockfd, (struct sockaddr *) &server, serverSize) < 0) {
        error("binding");
    }

    clientSize = sizeof(struct sockaddr_in);

    while (1) {
        printf("Waiting for Client Request...\n");
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN, 0,
                (struct sockaddr *) &client, &clientSize);
        if (numbytes < 0) {
            error("recvfrom");
        }
        buf[numbytes] = '\0';

        printf("Received request for File: %s\n", buf);
        numbytes = sendto(sockfd, buf, sizeof(buf), 0,
                (struct sockaddr *) &client, clientSize);
        if (numbytes < 0) {
            error("sendto");
        }
        sendFile(sockfd, client);
    }
}

sendFile(int sockfd, struct sockaddr_in client) {

}

Client:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

#define MAXBUFLEN 1024

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in server, client;
    int serverSize;
    struct hostent *hp;
    char buf[MAXBUFLEN];
    int numbytes;
    char *serverIP = argv[1];
    int portno = atoi(argv[2]);
    char *filename = argv[3];
    int done = 1;

    if (argc != 4) {
        printf("Usage: SERVER PORT FILENAME\n");
        exit(1);
    }

    // Create Socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        error("Can't create socket");
    }

    // Configure Server Info
    server.sin_family = AF_INET;
    hp = gethostbyname(serverIP);
    if (hp == 0)
        error("Unknown host");
    bcopy((char *) hp->h_addr, (char *) &server.sin_addr, hp->h_length);
    server.sin_port = htons(portno);
    serverSize = sizeof(struct sockaddr_in);

    // Send Message
    strcat(buf, filename);
    numbytes = sendto(sockfd, buf, strlen(buf), 0, &server, serverSize);
    if (numbytes < 0)
        error("Sendto");
    bzero(buf, MAXBUFLEN);

    // Build file then set Done = 0
    while (done) {
        numbytes = recvfrom(sockfd, buf, MAXBUFLEN, 0, &client, &serverSize);
        if (numbytes < 0)
            error("recvfrom");

        done = 0;
    }

    printf("Received the packet: %s\n", buf);
}

I also found a great example here: http://www.linuxhowtos.org/C_C++/socket.htm

Thanks for everyone's help!

吾家有女初长成 2024-08-20 07:25:33

愚蠢的问题,但你的防火墙是否阻止了端口 8083。

鲍勃。

Silly question, but is your firewall blocking port 8083.

Bob.

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