00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
#ifndef IPopen_INC
00040
#define IPopen_INC
00041
00042
#include <fstream>
00043
#include <string>
00044
#include <vector>
00045
#include <unistd.h>
00046
#include <plearn/base/PP.h>
00047
#include <plearn/base/general.h>
00048
00049
#ifndef _MINGW_
00050
#include <unistd.h>
00051
#include <netinet/in.h>
00052
#include <netinet/tcp.h>
00053
#include <arpa/inet.h>
00054
#include <sys/socket.h>
00055
#include <netdb.h>
00056
#endif
00057
00058
namespace PLearn {
00059
using namespace std;
00060
00061
#ifndef _MINGW_
00062 class IPServer {
00063
public:
00064 IPServer(
int port_no,
int max_connections) {
00065
DelayedConstructor(port_no, max_connections);
00066 };
00067 IPServer(
int max_connections_) {
00068
DelayedConstructor(
ip_port, max_connections_);
00069 };
00070 IPServer() {
00071
DelayedConstructor(
ip_port,
max_connections);
00072 };
00073 ~IPServer() {
00074 shutdown(
socket_fd, 2);
00075 close(
socket_fd);
00076 };
00077
00078 int get_socket_fd() {
return socket_fd; };
00079 struct sockaddr_in *
get_address() {
return &
address; };
00080 string machine_name() {
return hostname(); };
00081 int port_no() {
return port; };
00082
00083
protected:
00084 int socket_fd;
00085 int port;
00086 struct sockaddr_in address;
00087
private:
00088 void DelayedConstructor(
int port_no,
int max_connections_) {
00089
port = port_no;
00090
00091
int addr_len =
sizeof(
struct sockaddr_in);
00092
00093
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
00094
if (socket_fd <= 0)
00095
PLERROR(
"Cannot create socket");
00096 address.sin_family = AF_INET;
00097 address.sin_addr.s_addr = INADDR_ANY;
00098 address.sin_port = htons(port_no);
00099
00100
if (bind(socket_fd, (
struct sockaddr *)&address, addr_len))
00101
PLERROR(
"Cannot bind socket");
00102 listen(socket_fd,
max_connections);
00103
int nodelay = 1;
00104 setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (
char *)nodelay,
sizeof(
int));
00105 }
00106
public:
00107
static int ip_port;
00108
static int max_connections;
00109 static void set_ip_port(
int port_no,
int max_connections_ = 100) { IPServer::ip_port = port_no; IPServer::max_connections = max_connections_; };
00110 };
00111
00112
00113 class IPopen:
public PPointable {
00114
public:
00115
00116 PStream pipe;
00117
00118
00119 IPopen(
IPServer &server)
00120 {
establish_communication(server); };
00121 IPopen(
IPServer &server,
const string &command,
bool the_verbose =
false)
00122 {
verbose = the_verbose;
launch(server, command); }
00123
~IPopen();
00124
00125 int get_socket_fd()
const {
return socket_fd; };
00126
00127
protected:
00130
void launch(
IPServer &server,
const string &command);
00131
void establish_communication(
IPServer &server);
00132
00133 bool verbose;
00134 int socket_fd;
00135 };
00136
00137
00138
00139
00140
00141
00142
int establish_connection(
const int n_hosts,
const char *hostnames[],
int port_no);
00143 inline int establish_connection(
const char *hostname,
int port_no)
00144 {
return establish_connection(1, &
hostname, port_no); }
00145 inline int establish_connection(
const int argc,
const char *argv[])
00146 {
00147
if (argc >= 3)
return establish_connection(1, &argv[1], atoi(argv[2]));
00148
PLERROR(
"Wrong number of arguments");
00149
return -1;
00150 }
00151
00152
#endif // ~_MINGW_
00153
00154 }
00155
00156
#endif