Skip to content

Commit 87f73c2

Browse files
committed
first commit
0 parents  commit 87f73c2

23 files changed

+1226
-0
lines changed

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 2.8.9)
2+
3+
set(CMAKE_CXX_STANDARD 17)
4+
5+
#Set the executable name
6+
set(EXE_SERVER server)
7+
8+
#Set the executable name
9+
set(EXE_CLIENT client)
10+
11+
project (socketTime)
12+
13+
#Bring the custom libraries
14+
link_directories( lib )
15+
16+
17+
#Bring the headers
18+
include_directories(include)
19+
20+
#Bring the sources
21+
file(GLOB SOURCES_SERVER "src/server/*.cpp")
22+
file(GLOB SOURCES_CLIENT "src/client/*.cpp")
23+
24+
#Add opencv cmake .config path
25+
#set (OpenCV_DIR opt/opencv/release)
26+
27+
SET(Boost_USE_STATIC_LIBS ON)
28+
set(CMAKE_USE_PTHREADS_INIT true)
29+
set(CMAKE_USE_PTHREADS_INIT ON)
30+
31+
find_package(Threads REQUIRED )
32+
find_package(Boost COMPONENTS program_options log log_setup REQUIRED)
33+
find_package(Threads)
34+
35+
36+
#Build the executable
37+
add_executable(${EXE_SERVER} ${SOURCES_SERVER})
38+
add_executable(${EXE_CLIENT} ${SOURCES_CLIENT})
39+
40+
#Link the libraries
41+
target_link_libraries(${EXE_SERVER} Threads::Threads Boost::program_options Boost::log Boost::log_setup)
42+
target_link_libraries(${EXE_CLIENT} Threads::Threads Boost::program_options Boost::log Boost::log_setup)
43+
44+

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
programa con arquitectura cliente servidor en c++ con minimo un servidor y dos clientes.
2+
3+
# MultipleThreadMultipleClientSingleServerSocketCPP

build/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

compile.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
g++ Server.cpp main_server.cpp MyThread.cpp CustomerSupport.cpp -o server -lpthread
2+
g++ Client.cpp main_client.cpp Clock.cpp -o client -lpthread
3+

include/Clock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef Clock_class
2+
#define Clock_class
3+
#include <iostream>
4+
using namespace std;
5+
class Clock{
6+
public:
7+
string currentDateTime();
8+
virtual ~Clock();
9+
};
10+
#endif
11+

include/GetLocalAddress.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <iostream> ///< cout
3+
#include <cstring> ///< memset
4+
#include <errno.h> ///< errno
5+
#include <sys/socket.h> ///< socket
6+
#include <netinet/in.h> ///< sockaddr_in
7+
#include <arpa/inet.h> ///< getsockname
8+
#include <unistd.h> ///< close
9+
#include<string.h>
10+
#include<netinet/in.h> //#define INET_ADDRSTRLEN 16 #define INET6_ADDRSTRLEN 46
11+
#include <cstring> ///< memset
12+
13+
int getLocalAddress(std::string& myIP);

include/GetLocalPortNumber.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include <iostream> ///< cout
3+
#include <cstring> ///< memset
4+
#include <errno.h> ///< errno
5+
#include <sys/socket.h> ///< socket
6+
#include <netinet/in.h> ///< sockaddr_in
7+
#include <arpa/inet.h> ///< getsockname
8+
#include <unistd.h> ///< close
9+
10+
int getLocalPortNumber(char* myIP);
11+

include/Manager.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef MY_MANAGER_H
2+
#define MY_MANAGER_H
3+
4+
5+
#include <boost/log/attributes.hpp>
6+
#include <boost/log/common.hpp>
7+
#include <boost/log/core.hpp>
8+
#include <boost/log/expressions.hpp>
9+
#include <boost/log/sinks.hpp>
10+
#include <boost/log/sinks/sync_frontend.hpp>
11+
#include <boost/log/sinks/text_ostream_backend.hpp>
12+
#include <boost/log/sources/logger.hpp>
13+
#include <boost/log/sources/record_ostream.hpp>
14+
#include <boost/log/sources/severity_feature.hpp>
15+
#include <boost/log/sources/severity_feature.hpp>
16+
#include <boost/log/sources/severity_logger.hpp>
17+
#include <boost/log/sources/severity_logger.hpp>
18+
#include <boost/log/support/date_time.hpp>
19+
#include <boost/log/utility/setup/common_attributes.hpp>
20+
#include <boost/log/utility/setup/common_attributes.hpp>
21+
#include <boost/log/utility/setup/formatter_parser.hpp>
22+
#include <boost/make_shared.hpp>
23+
#include <boost/shared_ptr.hpp>
24+
#include <boost/thread/thread.hpp>
25+
#include <fstream>
26+
#include <boost/core/null_deleter.hpp>
27+
#include <boost/log/trivial.hpp>
28+
namespace logging = boost::log;
29+
namespace attrs = boost::log::attributes;
30+
namespace src = boost::log::sources;
31+
namespace sinks = boost::log::sinks;
32+
namespace expr = boost::log::expressions;
33+
namespace keywords = boost::log::keywords;
34+
35+
enum sign_severity_level
36+
{
37+
trace,
38+
debug,
39+
info,
40+
warning,
41+
error,
42+
fatal,
43+
report
44+
};
45+
46+
47+
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
48+
my_logger, src::severity_logger_mt<sign_severity_level>
49+
);
50+
51+
52+
class Manager
53+
{
54+
public:
55+
using Backend = sinks::text_ostream_backend;
56+
using TextSink = sinks::synchronous_sink<Backend>;
57+
58+
Manager()
59+
{
60+
backend_ = boost::make_shared<Backend>();
61+
backend_->auto_flush(true);
62+
63+
boost::shared_ptr<TextSink> sink(new TextSink(backend_));
64+
sink->set_formatter(
65+
expr::format("[%1%]<%2%>: %3%") %
66+
//expr::format("[%1%]<%2%>(%3%): %4%") %
67+
expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") %
68+
//logging::trivial::severity %
69+
expr::attr < sign_severity_level > ("Severity") %
70+
//expr::attr < attrs::current_thread_id::value_type > ("ThreadID") %
71+
expr::smessage
72+
);
73+
sink->set_filter(expr::attr<sign_severity_level>("Severity") >= warning);
74+
logging::core::get()->add_sink(sink);
75+
76+
logging::add_common_attributes();
77+
logging::core::get()->add_global_attribute(
78+
"ThreadID", attrs::current_thread_id()
79+
);
80+
}
81+
82+
void set_log_file(const char* filename)
83+
{
84+
backend_->remove_stream(current_stream_);
85+
current_stream_.reset(new std::ofstream(filename));
86+
backend_->add_stream(current_stream_);
87+
//stdout
88+
backend_->add_stream(
89+
boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
90+
}
91+
92+
private:
93+
boost::shared_ptr<Backend> backend_;
94+
boost::shared_ptr<std::ostream> current_stream_;
95+
};
96+
97+
98+
#endif

include/MyClient.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <string.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <netdb.h>
9+
#include <iostream>
10+
#include "Clock.h"
11+
#include <Manager.h>
12+
13+
class MyClient
14+
{
15+
private:
16+
int socketConnectToClient_;
17+
unsigned short serverPortNumber_;
18+
int numberOfByteOfBuffer_;
19+
20+
Clock clock;
21+
22+
struct sockaddr_in serverAddress_;
23+
struct hostent *serverIP_;
24+
Manager logManager;
25+
my_logger::logger_type log = my_logger::get();
26+
char buffer[1024];
27+
public:
28+
MyClient(char *_serverIP_, unsigned short _serverPort_);
29+
int createClientForConnexion();
30+
int connectClientToServer();
31+
int sendRequestToServer();
32+
int receiveResponseFromServer();
33+
int closeConnexion();
34+
int setLog(std::string& _logName_);
35+
int setLog(std::string&& _logName_);
36+
// Setter and Getter socketConnectToClient
37+
38+
auto socketConnectToClient() const& -> const int& { return socketConnectToClient_; }
39+
auto socketConnectToClient() & -> int& { return socketConnectToClient_; }
40+
auto socketConnectToClient() && -> int&& { return std::move(socketConnectToClient_); }
41+
// Setter and Getter serverPortNumber
42+
43+
auto serverPortNumber() const& -> const unsigned short& { return serverPortNumber_; }
44+
auto serverPortNumber() & -> unsigned short& { return serverPortNumber_; }
45+
auto serverPortNumber() && -> unsigned short&& { return std::move(serverPortNumber_); }
46+
47+
// Setter and Getter numberOfByteOfBuffer
48+
49+
auto numberOfByteOfBuffer() const& -> const int& { return numberOfByteOfBuffer_; }
50+
auto numberOfByteOfBuffer() & -> int& { return numberOfByteOfBuffer_; }
51+
auto numberOfByteOfBuffer() && -> int&& { return std::move(numberOfByteOfBuffer_); }
52+
53+
// Setter and Getter socketConnectToClient
54+
55+
auto serverAddress() const& -> const sockaddr_in& { return serverAddress_; }
56+
auto serverAddress() & -> sockaddr_in& { return serverAddress_; }
57+
auto serverAddress() && -> sockaddr_in&& { return std::move(serverAddress_); }
58+
// Setter and Getter socketConnectToClient
59+
};

include/MyProcessor.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef MY_PROCESSOR_H
2+
#define MY_PROCESSOR_H
3+
#include "MyServer.h"
4+
5+
/**
6+
* Implementation of a Processor to deal with a client request
7+
*
8+
* MyProcessor is an abstract class, due to the
9+
* the existence of the pure virtual member function process (= 0)
10+
* process needs to be overriden by derived class otherwise compile error
11+
* no objects of class MyProcessor can be directly created
12+
* virtual function avoid function overloading
13+
* At runtime the os uses dynamic binding to determine the type of the object
14+
* Function that are not virtual uses static binding,
15+
* the type of the object is defined at compile time
16+
* For a base class with a derived make the base class desctucort virtual
17+
* Then for every derived class define its destructor appropriately
18+
* Don't let the base class destructor clean the object when it's out of the current scope
19+
*
20+
*/
21+
class MyProcessor
22+
{
23+
public:
24+
25+
virtual void process(MyServer serverProcessingAcceptedConnexion) = 0;
26+
27+
};
28+
29+
#endif /*MY_PROCESSOR_H */

0 commit comments

Comments
 (0)