8 static const int MAXSIZE = 4096;
9 auto port = stoi(sport);
11 int sockfd, n,rec_len;
12 char recvline[MAXSIZE], sendline[MAXSIZE];
14 struct sockaddr_in servaddr;
16 if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
18 oss <<
"create socket error(" << errno <<
"): " << strerror(errno);
19 throw Error(oss.str());
22 memset(&servaddr, 0,
sizeof(servaddr));
23 servaddr.sin_family = AF_INET;
24 servaddr.sin_port = htons(port);
26 if ( (hext = gethostbyname(sip.c_str())) == NULL ) {
28 oss <<
"gethostbyname error for " << sip << endl;
29 throw Error(oss.str());
31 memcpy(&servaddr.sin_addr, hext->h_addr_list[0], hext->h_length);
33 if( connect(sockfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr)) < 0) {
35 oss <<
"connect error(" << errno <<
"): " << strerror(errno) << endl;
36 throw Error(oss.str());
39 if((rec_len = recv(sockfd, buf, MAXSIZE,0)) == -1) {
41 oss <<
"recv error(" << errno <<
"): " << strerror(errno) << endl;
42 throw Error(oss.str());
58 struct sockaddr_in servaddr;
60 if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {
61 throw Error(
string(
"Server: ") + strerror(errno));
64 memset(&servaddr, 0,
sizeof(servaddr));
65 servaddr.sin_family = AF_INET;
66 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
67 servaddr.sin_port = (port>0 ? htons(port) : 0);
69 if( bind(socket_fd, (
struct sockaddr*)&servaddr,
sizeof(servaddr)) == -1) {
70 throw Error(
string(
"Server: ") + strerror(errno));
73 struct sockaddr_in connAddr;
74 socklen_t len =
sizeof(connAddr);
75 if(getsockname(socket_fd, (sockaddr*)&connAddr, &len) !=0)
throw Error(
"Server: retrived Port failed!");
76 Port = port = ntohs(connAddr.sin_port);
79 if( listen(socket_fd, 10) == -1)
throw Error(
string(
"Server: ") + strerror(errno));
82 gethostname(hostname, 1024);
88 if(
Verbose>0) cout << endl <<
"Started @ " <<
now() << endl;
89 for(
int r=0; r<round; r++) {
90 for(
int c=0; c<total; c++) {
94 cout <<
" Server[" << port <<
"]: " << current <<
" / " << (total-1) <<
" @ " <<
now(
false) << flush;
102 if( (connect_fd = accept(socket_fd, (
struct sockaddr*)NULL, NULL)) == -1) {
103 cout <<
"Server: " << strerror(errno) << endl;
106 struct linger so_linger;
107 so_linger.l_onoff = 1;
108 so_linger.l_linger = 0;
109 setsockopt(connect_fd, SOL_SOCKET, SO_LINGER, &so_linger,
sizeof so_linger);
111 string data = to_string(current);
112 if(send(connect_fd, data.c_str(), data.length(),0) == -1) {
113 if(
Verbose>1) cout <<
"Server: " << strerror(errno) << endl;
119 if(
Verbose>0) cout <<
"Finished @ " <<
now() << endl << endl;
class used to wrap error message
static string Next(string sip, string sport)
class extended to GiNaC symbol class, represent a positive symbol
bool file_exists(string fn)
string now(bool use_date)
date/time string
void string_replace_all(string &str, const string &from, const string &to)
void garWrite(const string &garfn, const map< string, ex > &resMap)
garWrite to write the string-key map to the archive