C -UDP客户端服务器无效参数



我有一台服务器,该服务器应该在从客户端接收消息后向客户端发送信息(Echo Server(。以下是产生一个errno 22的代码,我将其视为"无效的参数"。我试图了解哪个参数无效,因为我的客户发送了一条带有相同参数的消息

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
//#include <sys/time.h>
int main(int argc, char *argv[]) {
    // port to start the server on
    int SERVER_PORT = 8877;
    struct timeval server_start, client_start;
    // socket address used for the server
    struct sockaddr_in server_address;
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    // htons: host to network short: transforms a value in host byte
    // ordering format to a short value in network byte ordering format
    server_address.sin_port = htons(SERVER_PORT);
    // htons: host to network long: same as htons but to long
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    // create a UDP socket, creation returns -1 on failure
    int sock;
    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("could not create socketn");
        return 1;
    }
    // bind it to listen to the incoming connections on the created server
    // address, will return -1 on error
    if ((bind(sock, (struct sockaddr *)&server_address,
              sizeof(server_address))) < 0) {
        printf("could not bind socketn");
        return 1;
    }

    // socket address used to store client address
    struct sockaddr_in client_address;
    int client_address_len = 0;

    // run indefinitely
    while (true) {
        char buffer[500];
        printf("problem here n");
        int len=0;
        // read content into buffer from an incoming client
        if (len = recvfrom(sock, &client_start, sizeof(client_start), 0,(struct sockaddr *)&client_address,&client_address_len)<0){
               printf("failed: %dn", errno);
               return 1;
         }
        // inet_ntoa prints user friendly representation of the
        // ip address
        //buffer[len] = '';

        gettimeofday(&server_start);

        int send = 0;
        // send same content back to the client ("echo")
        if(send = sendto(sock, &server_start, sizeof(server_start),0,(struct sockaddr *)&client_address,
               &client_address_len)<0){
             printf("failed: %dn", errno);
             return 1;
        };

    }
    return 0;
}

我试图理解哪个参数无效

没有参数无效。您对错误测试的误报。

if (len = recvfrom(sock, &client_start, sizeof(client_start), 0,(struct sockaddr *)&client_address,&client_address_len)<0){
if(send = sendto(sock, &server_start, sizeof(server_start),0,(struct sockaddr *)&client_address,
       &client_address_len)<0){

通常的问题。操作员优先。尝试以下操作:

if ((len = recvfrom(sock, &client_start, sizeof(client_start), 0,(struct sockaddr *)&client_address,&client_address_len))<0){
if((send = sendto(sock, &server_start, sizeof(server_start),0,(struct sockaddr *)&client_address,
       &client_address_len))<0){

最新更新