recv始终等于-1

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

循环功能(action == 1表示主持游戏,action == 2表示加入游戏):

int run(game_t **game, btimer_t mTimer, SDL_Renderer *renderer, \
client_t *client)
{
    int action = 1;
    static int check = 0;
    int i = 0;

    SDL_RenderSetScale(renderer, 2, 2);
    for (; 42 ; i++) {
        game[0] = retrieveInput(game[0]);
        SDL_PollEvent(&game[0]->event);
        timerUpdate(&mTimer);
        if (game[0]->event.type == SDL_QUIT || game[0]->quit == ESCAPE)
            return (-1);
        if (i == 0 || action != 0)
            SDL_RenderClear(renderer);
        action = menu(renderer, game[0]);
        if (action == 1) {
            create_serv_and_init_client(client);
            game_loop(game, &mTimer, renderer);
        }
        if (action == 2) {
            client[1] = (check == 0) ? create_client(1) : client[1];
            send_msg(client[1]);
            check++;
            game[1]->connect = 0;
            game_loop(game, &mTimer, renderer);
        }
        SDL_RenderPresent(renderer);
    }
}

这是我的server.c:

serv_t create_serv_socket()
{
    serv_t serv;
    int opt = 1;

    serv.sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(serv.sockfd < 0){
        printf("[-]Error in connection.\n");
        exit(1);
    }
    if (setsockopt(serv.sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
        perror("setsockopt");
        exit(1);
    }
    memset(&serv.serverAddr, '\0', sizeof(serv.serverAddr));
    serv.serverAddr.sin_family = AF_INET;
    serv.serverAddr.sin_port = htons(PORT);
    serv.serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv.ret = bind(serv.sockfd, (struct sockaddr*)&serv.serverAddr, sizeof(serv.serverAddr));
    if (serv.ret < 0) {
        printf("[-]Error in binding.\n");
        exit(1);
    }
    listen(serv.sockfd, 4);
    return (serv);
}

void *waiting_connection(void *server)
{
    serv_t *serv = (serv_t *)server;

    serv->newSocket = accept(serv->sockfd, (struct sockaddr*)&serv->newAddr, \
    &serv->addr_size);
    if (serv->newSocket < 0)
        exit(1);
    if ((serv->childpid = fork()) == 0) {
        close(serv->sockfd);
        while (recv(serv->newSocket, serv->buffer, 1024, 0) != 0) {
            printf("Client: %s\n", serv->buffer);
            send(serv->newSocket, serv->buffer, strlen(serv->buffer), 0);
            bzero(serv->buffer, sizeof(serv->buffer));
        }
    }
    return NULL;
}

void create_serv_and_init_client(client_t *cl)
{
    static int i = 0;
    pthread_t thread_serv;
    serv_t serv;

    if (i == 0) {
        cl[0].content = retrieve_info();
        cl[0].connected = 1;
        PORT = cl[0].content.port;
        serv = create_serv_socket();
        if (pthread_create(&thread_serv, NULL, waiting_connection, \
        (void *)&serv) < 0) {
            perror("could not create thread");
            exit(1);
        }
        cl[0] = create_client(0);
        printf("OK\n");
        i++;
    }
}

和我的client.c:

content_t retrieve_info(int check)
{
    content_t info;

    printf("Choissisez un pseudo : ");
    scanf("%s", info.username);
    printf("Port : ");
    scanf("%d", &info.port);
    if (check == 1) {
        printf("Adresse IP : ");
        scanf("%s", info.ip);
    }
    return (info);
}

client_t create_client(int check)
{
    static int id = 0;
    client_t cl;
    char ip[100] = "127.0.0.1";

    if (check == 1) {
        cl.content = retrieve_info(1);
        strcpy(ip, cl.content.ip);
        PORT = cl.content.port;
    }
    cl.clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(cl.clientSocket < 0){
        printf("[-]Error in connection.\n");
        exit(1);
    }
    printf("[+]Client Socket is created.\n");

    memset(&cl.serverAddr, '\0', sizeof(cl.serverAddr));
    cl.serverAddr.sin_family = AF_INET;
    cl.serverAddr.sin_port = htons(PORT);
    cl.serverAddr.sin_addr.s_addr = inet_addr(ip);
    cl.id = id;
    cl.ret = connect(cl.clientSocket, (struct sockaddr*)&cl.serverAddr, sizeof(cl.serverAddr));
    if(cl.ret < 0){
        printf("[-]Error in connection.\n");
        exit(1);
    }
    if (check == 1)
        send(cl.clientSocket, ip, sizeof(ip), 0);
    printf("[+]Connected to Server.\n");
    cl.connected = 1;
    id++;
    return (cl);
}

my.h:

typedef struct {
    int input;
    char username[100];
    char ip[100];
    int port;
} content_t;

typedef struct {
    int sockfd;
    int ret;
    struct sockaddr_in serverAddr;
    int newSocket;
    struct sockaddr_in newAddr;
    socklen_t addr_size;
    pid_t childpid;
    char buffer[1024];
    content_t content[4];
}serv_t;

typedef struct {
    int clientSocket;
    int connected;
    int ret;
    int id;
    struct sockaddr_in serverAddr;
    content_t content;
}client_t;

问题是我的客户端可以连接到服务器,但是无法发送数据,客户端无法提供服务,而客户端无法提供服务。

send_msg函数仅在此处进行测试,她向服务器发送了一条消息,但服务器从未向我们显示该消息。

c sockets tcp sdl sdl-2
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.