Trying to create a stream in C ++ in linux via pthread . Wrote class Threads for creating threads. Wrote a simple server class. Both classes work separately. Then I do this:
#ifndef SERVER_H #define SERVER_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include "Threads.h" class Server { public: Server(int portnum); virtual ~Server(); void* ThreadFunc(void* arg); private: void InitVars(); int hSocket, newsockfd, iPort; socklen_t clilen; char buffer[255]; struct sockaddr_in serv_addr, cli_addr; int n; bool ServerRunning; Threads* thread; }; #endif /* SERVER_H */ #include "Server.h" Server::Server(int portnum) { InitVars(); // Create socket hSocket = socket(AF_INET, SOCK_STREAM, 0); if(hSocket < 0) throw "ERROR opening socket"; iPort = portnum; // Clear and setup sockaddr_in serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(iPort); if(bind(hSocket, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) throw "ERROR binding"; listen(hSocket, 10); int i = 1; thread = new Threads(&Server::ThreadFunc, &i); // clilen = sizeof(cli_addr); // newsockfd = accept(hSocket, (struct sockaddr*) &cli_addr, &clilen); // if(newsockfd < 0) // fprintf(stderr,"ERROR on accept"); // // n = read(newsockfd, buffer, 255); // if(n < 0) // throw "ERROR reading from socket"; // // printf("%s\n", buffer); // // n = write(newsockfd, "I got your message", 18); // if(n < 0) // throw "ERROR writing to socket"; } Server::~Server() { if(hSocket > 0) close(hSocket); if(newsockfd > 0) close(newsockfd); } void Server::InitVars() { ServerRunning = true; memset((char*) &serv_addr, 0x00, sizeof(serv_addr)); memset((char*) &cli_addr, 0x00, sizeof(cli_addr)); memset(buffer, 0x00, 255); } void* Server::ThreadFunc(void* arg) { clilen = sizeof(cli_addr); newsockfd = accept(hSocket, (struct sockaddr*) &cli_addr, &clilen); if(newsockfd < 0) fprintf(stderr,"ERROR on accept"); n = read(newsockfd, buffer, 255); if(n < 0) throw "ERROR reading from socket"; printf("%s\n", buffer); n = write(newsockfd, "I got your message", 18); if(n < 0) throw "ERROR writing to socket"; } I get the error:
Server.cpp:29:49: ошибка: нет подходящей функции для вызова «Threads::Threads(void* (Server::*)(void*), int*)» так как конструктор описан как Threads(void*(*thread_func)(void *), void *__restrict arg);
How to fix?
Here is the code for my threads. I'm just learning how to work with threads so do not swear =).
#ifndef THREADS_H #define THREADS_H #include <pthread.h> class Threads { public: Threads(void*(*thread_func)(void *), void *__restrict arg); virtual ~Threads(); private: pthread_t _thread; }; #endif /* THREADS_H */ #include "Threads.h" Threads::Threads(void*(*thread_func)(void *), void *__restrict arg) { pthread_create(&_thread, NULL, thread_func, arg); } Threads::~Threads() { }