R
R
Ripper_acskt2022-04-13 15:33:41
C++ / C#
Ripper_acskt, 2022-04-13 15:33:41

Error connecting to self-written ftp server, what's the problem?

When trying to connect to the ftp server, a welcome message is displayed, after which the connection is terminated
server.h

#ifndef SERVER_H
#define SERVER_H

#include <sys/types.h>
#include "user.h""
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <pthread.h>
#include <QDebug>

#define SERVER_PORT 21
#define QUEUE_SIZE 10
#define BUFFER_SIZE 4096

class Server
{
public:
    Server();

private:
//    const int buffer_size = 4096;
    int *s, b, l, on = 1, num_of_users = 0;
    int **sa = (int**)malloc(QUEUE_SIZE * sizeof(int*));
    char *buffer;
    pthread_mutex_t mutex;
    struct sockaddr_in channel;
    struct hostent *h;
    pthread_t tid;
    User *user;

    static void* start_server(void *s);
    static void* new_connection(void *s);
    int menu();
};

#endif // SERVER_H

server.cpp
#include "server.h"

Server::Server()
{
    pthread_mutex_init(&mutex, NULL);
    user = new User();
    user->set_current_dir("/Users/ripper/Desktop/bsuir/spovm/ForthSemCourseWork/files");

    s = new int;

    memset(&channel, 0, sizeof(channel));

//    h = gethostbyname("127.0.0.1");
//    if(!h) qDebug() << "Ошибка выполнения gethostbyname";

    channel.sin_family = AF_INET;
//    memcpy(&channel.sin_addr.s_addr, h->h_addr, h->h_length);
    channel.sin_addr.s_addr = htonl(INADDR_ANY);
    channel.sin_port = htons(SERVER_PORT);


    if((*s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) qDebug() << "ошибка сокета";
    if(setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) qDebug() << "ошибка setsockopt REUSEADDR";
    if(setsockopt(*s, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) qDebug() << "ошибка setsockopt REUSEPORT";
    if((bind(*s, (struct sockaddr *) &channel, sizeof(channel))) < 0) qDebug() << "ошибка связывания";

    if(listen(*s, QUEUE_SIZE) < 0) qDebug() << "ошибка ожидания";

    if(pthread_create(&tid, NULL, start_server, (void *)this)) qDebug() << "ошибка создания потока ожидания";
//    start_server((void *)s);

//    if(pthread_join(tid, NULL)) qDebug() << "ошибка ожидания потока";
}

void* Server::start_server(void *serv){
    pthread_t tid;
    Server *server = (Server *)serv;
    while(1){
        server->sa[server->num_of_users] = (int*)malloc(sizeof(int));
        *(server->sa[server->num_of_users]) = accept(*((Server*)serv)->s, 0, 0);
        if(*(server->sa[server->num_of_users]) < 0) qDebug() << "ошибка доступа";
        write(*(server->sa[server->num_of_users]), "Hello in my server\n", 19);
//        if(pthread_create(&tid, NULL, new_connection, serv)) qDebug() << "ошибка создания потока подключения";
        server->num_of_users++;
        new_connection(serv);
//        pthread_mutex_lock(&server->mutex);

//        pthread_mutex_unlock(&server->mutex);
    }
    exit(0);
}

void* Server::new_connection(void *serv){

    int fd, bytes;

    Server *server = (Server *)serv;
    char *buff = server->buffer;
    int descriptor;
    while(1){
//        pthread_mutex_lock(&server->mutex);

        server->buffer = (char*)malloc(BUFFER_SIZE * sizeof(char));
        descriptor = *(server->sa[server->num_of_users-1]);

//        pthread_mutex_unlock(&server->mutex);

        while((bytes = read(descriptor, server->buffer, BUFFER_SIZE)) <= 0) continue;

//        pthread_mutex_lock(&server->mutex);
//        strcpy(server->buffer, buff);
//        pthread_mutex_unlock(&server->mutex);

        if(!server->menu()) break;
//        close(sa);
    }

    return(0);
}

int Server::menu(){
    QStringList list;
//    pthread_mutex_lock(&mutex);
    int len, descriptor = *(sa[num_of_users-1]);
//    pthread_mutex_unlock(&mutex);
    if(strstr(buffer, "close\r\n") || strstr(buffer, "quit\r\n") || strstr(buffer, "disconnect\r\n") || strstr(buffer, "exit\r\n")){
//        pthread_mutex_lock(&mutex);
        close(*sa[num_of_users-1]);
//        pthread_mutex_unlock(&mutex);
        return 0;
    }
    if(strstr(buffer, "ls\r\n")){
//        pthread_mutex_lock(&mutex);

        user->get_all_files()->current_dir(user->get_current_dir().toUtf8().data());

        for(int i = 0; i < user->get_all_files()->get_num(); i++){
            buffer = (char*)malloc(BUFFER_SIZE * sizeof(char));
            list = user->get_all_files()->get_all_files()[i].name.split("/");
            buffer = (list[list.length()-1] + "\n").toUtf8().data();
            len = (list[list.length()-1] + "\n").length();
            write(descriptor, buffer, len);
        }
        user->get_all_files()->set_empty();

//        pthread_mutex_unlock(&mutex);
        return 1;
    }
    return -1;
}


FileZila output
6256c30820047587065498.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2022-04-13
@Tomaxauk

what is the problem?

You have a race between these:
if(pthread_create(&tid, NULL, new_connection, serv)) qDebug() << "ошибка создания потока подключения";
        server->num_of_users++;

and this:
descriptor = *(server->sa[server->num_of_users-1]);

From the point of view of working with threads, the code is written in no way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question