【问题标题】:The last element does not apper最后一个元素没有出现
【发布时间】:2020-07-19 21:10:59
【问题描述】:

练习要求将消息重新发送回客户端。

本练习的部分代码由我们的老师提供。

我不知道为什么程序发送的最后一条消息没有出现。我不明白错误在哪里。

我已经更改了在 x 之前添加 & 的读取。现在 x 值显示正确,但最后一个值仍然丢失

当我只插入一个值时,消息丢失。 服务器一直在运行,我不知道如何解决这个问题。

服务器代码是:

#include <stdio.h>      
#include <sys/types.h>
#include <sys/socket.h>   
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h> 

/*const*/ char MESSAGE[100] = "";
char buff[100];

int main(int argc, char *argv[]) {

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus = 0;
    struct sockaddr_in simpleServer;
    
    if (argc != 2) {

        fprintf(stderr, "Usage: %s <port>\n", argv[0]);
        exit(1);

    }

    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
        fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for listening */
    simplePort = atoi(argv[1]);

    /* setup the address structure */
    /* use INADDR_ANY to bind to all local addresses  */
    memset(&simpleServer, '\0', sizeof(simpleServer)); 
    simpleServer.sin_family = AF_INET;
    simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
    simpleServer.sin_port = htons(simplePort);

    /*  bind to the address and port with our socket  */
    returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer));

    if (returnStatus == 0) {
        fprintf(stderr, "Bind completed!\n");
    }
    else {
        fprintf(stderr, "Could not bind to address!\n");
    close(simpleSocket);
    exit(1);
    }

    /* lets listen on the socket for connections      */
    returnStatus = listen(simpleSocket, 5);

    if (returnStatus == -1) {
        fprintf(stderr, "Cannot listen on socket!\n");
    close(simpleSocket);
        exit(1);
    }

    int x;
    int i=0;

    while (1)

    {

        struct sockaddr_in clientName = { 0 };
        int simpleChildSocket = 0;
        int clientNameLength = sizeof(clientName);

        /* wait here */

            simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength);

        if (simpleChildSocket == -1) {

            fprintf(stderr, "Cannot accept connections!\n");
            close(simpleSocket);
            exit(1);

        }

        /* handle the new connection request  */
        /* write out our message to the client */
        

        //read the number of messages that have to be send
        read(simpleChildSocket, &x, sizeof(x));
        printf("x value is: %d\n", x);
        
        do{
            // read the message from client and copy it in buffer 
            read(simpleChildSocket, buff, sizeof(buff));

            //copy buff in MESSAGE
            strcpy(MESSAGE, buff);

            //sending the message
            write(simpleChildSocket, MESSAGE, strlen(MESSAGE));
            
            //cleaning the buffer
            memset(&simpleServer, '\0', sizeof(simpleServer));
            i++;
        }while(i<x);
        
        close(simpleChildSocket);
    }

    close(simpleSocket);
    return 0;

}

客户端代码是:

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

int main(int argc, char *argv[]) {

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus = 0;
    char buffer[256] = "";
    struct sockaddr_in simpleServer;

    if (argc != 3) {

        fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]);
        exit(1);

    }

    /* create a streaming socket      */
    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
        fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for connecting */
    simplePort = atoi(argv[2]);

    /* setup the address structure */
    /* use the IP address sent as an argument for the server address  */
    //bzero(&simpleServer, sizeof(simpleServer)); 
    memset(&simpleServer, '\0', sizeof(simpleServer));
    simpleServer.sin_family = AF_INET;
    //inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
    simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
    simpleServer.sin_port = htons(simplePort);

    /*  connect to the address and port with our socket  */
    returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

    if (returnStatus == 0) {
        fprintf(stderr, "Connect successful!\n");
    }
    else {
        fprintf(stderr, "Could not connect to address!\n");
    close(simpleSocket);
    exit(1);
    }

    /*create the message*/
    char buff[100];
    int i=0, x;
    //int n;
    //while((buff[n++] = getchar()) != '\n');
    printf("How many messages do you want to send?\n");
    scanf("%d", &x);
    write(simpleSocket, x, sizeof(x));

    printf("Insert the message:\n");

    do{
        fgets(buff, 100, stdin);
        write(simpleSocket, buff, sizeof(buff));
        i++;
    }while(i<=x);

    /* get the message from the server   */
    returnStatus = read(simpleSocket, buffer, sizeof(buffer));

    if ( returnStatus > 0 ) {
        printf("%d: %s", returnStatus, buffer);
    } else {
        fprintf(stderr, "Return Status = %d \n", returnStatus);
    }

    close(simpleSocket);
    return 0;
}

【问题讨论】:

  • 您的标题需要更改。

标签: c server client


【解决方案1】:

您的阅读有问题。 这应该是:

read(simpleChildSocket, &amp;x, sizeof(x));

Read 需要一个指针。

【讨论】:

  • 你可以看看你的编译器的the warning
  • 更改读取并给出指针不起作用。
猜你喜欢
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
相关资源
最近更新 更多