当从服务器请求时间时,我得到一个Segmentation故障(核心转储)。我如何解决这个问题?

问题描述 投票:0回答:1

我正在编写一个程序,客户端向服务器发送一个时间请求。我还想发送我的名字,并让服务器回响它。到目前为止,程序会回应我的名字,但是我在应该显示时间的地方得到了一个Segmentation故障(core dumped)。我附上了客户端和服务器的代码以及终端的截图。

服务器代码

#include <stdio.h>      /* I/O functions */
#include <string.h>     /* string functions */
#include <stdlib.h>     /* C standard functions */
#include <sys/socket.h> /* socket functions */
#include <sys/types.h>  /* library of basic types */
#include <netinet/in.h> /* library of Internet address functions */
#include <arpa/inet.h>  /* Internet operations */
#include <time.h>       /* time functions */

#define PORT 8080       /* server port # */
#define BUFFSIZE 200    /* buffer size */

int main()
{
    int sockfd;
    int addrlen;
    char buffer[BUFFSIZE];
    struct sockaddr_in server;
    struct sockaddr_in client;
    time_t current_time;

    /* Populate socket data structures with IP address and port number */
    memset((char *) &server, 0, sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);

    /* Create a UDP socket; returns -1 on failure */
    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if (sockfd == -1) {
        printf("Socket error\n");
        exit(1); /* Exit on error */
    }

    /* Bind the socket address */
    if ((bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr_in))) == -1) {
        printf("Server bind error\n");
        exit (1); /* Exit on error */
    }

    /* Status message */
    printf("The server is listening on port: %d\n", PORT);

printf("Waiting for client request...\n");

printf("Press CTRL + C to exit\n");



while(1) {

    addrlen = sizeof(struct sockaddr_in);

    recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);

    current_time = time(NULL);

    memcpy(buffer + strlen(buffer) + 1, &current_time, sizeof(current_time));

    sendto(sockfd, buffer, strlen(buffer) + 1 + sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);

}

exit(0);

} /* End of time server program */

客户代码

#include <stdio.h>      /* I/O functions */
#include <string.h>         /* string functions */
#include <stdlib.h>         /* C standard functions */
#include <sys/socket.h> /* socket functions */
#include <sys/types.h>  /* library of basic types */
#include <netinet/in.h> /* library of Internet address functions */
#include <arpa/inet.h>      /* Internet operations */
#include <time.h>           /* time functions */

#define BUFFSIZE 200    /* buffer size */

int main(int argc, char *argv[])
{
    int sockfd;
    int addrlen;
    char buffer[BUFFSIZE] = "GET TIME\r\n";
    struct sockaddr_in server;
    struct sockaddr_in client;
    char *servIP = argv[1];             // Server IP address from command line
    int servPort = atoi(argv[2]);       // Server port number from command line
    char *name = argv[3];
    time_t current_time;

    /* Check that two arguments were passed on the command line */
    if (argc != 4) {
      printf("Usage: udp-time-client [IP address] [Port] [Name] \n");
      exit(1);
    }

    /* Populate server socket data structure with IP address and port number */
    memset((char *) &server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(servPort);
    server.sin_addr.s_addr = inet_addr(servIP); 

    /* Populate client socket data structure with IP address and port number */
    memset((void *)&client, '\0', sizeof(client));
    client.sin_family = AF_INET;
    client.sin_port = htons(servPort);
    client.sin_addr.s_addr = inet_addr(servIP); 

    /* Create a UDP socket; returns -1 on failure */
    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if (sockfd == -1) {
        printf("Socket error\n");
        exit(1);
    }

    /* Status message */
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);



/* Send the time request to the server */

addrlen = sizeof(struct sockaddr_in);

strcpy(buffer, name);

sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);

printf("Request sent to server\n");

/* Receive the time request from server */

recvfrom(sockfd, (char *) buffer, sizeof(buffer), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);

/* Print the name received from the server */

printf("\n The name received from the server:%s\n", buffer);

memcpy((void *)current_time, buffer + strlen(buffer) + 1, sizeof(current_time));

/* Print the time received from the server */

printf("\n The time received from the server:%s\n", ctime(&current_time));



exit(0);

} /* End of time client program */

终端截图 如你所见,我的名字被回传,但我在应该是时间的地方得到了错误的信息。

c udp client-server
1个回答
0
投票
memcpy((void *)current_time, ...);

应该是"..."。

memcpy(&current_time, ...);
© www.soinside.com 2019 - 2024. All rights reserved.