| 12 | |
| 13 | protected: |
| 14 | void* run(void) |
| 15 | { |
| 16 | acl::socket_stream conn; |
| 17 | if (!conn.open(addr_, 10, 10)) { |
| 18 | printf("%ld: connect %s error %s\r\n", |
| 19 | acl::thread::self(), addr_.c_str(), |
| 20 | acl::last_serror()); |
| 21 | return NULL; |
| 22 | } |
| 23 | |
| 24 | (void) conn.set_tcp_solinger(true, 0); |
| 25 | |
| 26 | printf("connect %s ok, my addr=%s\r\n", |
| 27 | addr_.c_str(), conn.get_local(true)); |
| 28 | for (int i = 0; i < n_; i++) { |
| 29 | if (conn.format("thread-%ld: hello world\r\n", |
| 30 | acl::thread::self()) == -1) { |
| 31 | |
| 32 | printf("write to %s error %s\r\n", |
| 33 | addr_.c_str(), acl::last_serror()); |
| 34 | break; |
| 35 | } |
| 36 | acl::string buf; |
| 37 | if (!conn.gets(buf)) { |
| 38 | printf("gets from %s error %s\r\n", |
| 39 | addr_.c_str(), acl::last_serror()); |
| 40 | break; |
| 41 | } else { |
| 42 | printf("%ld: %s\r\n", acl::thread::self(), |
| 43 | buf.c_str()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | printf("sleep 2 seconds\r\n"); |
| 48 | sleep(2); |
| 49 | printf("disconnect from %s now\r\n", addr_.c_str()); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | acl::string addr_; |