example_tc_socket.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_socket.h"
  17. #include "util/tc_clientsocket.h"
  18. #include "util/tc_http.h"
  19. #include "util/tc_epoller.h"
  20. #include "util/tc_common.h"
  21. #include <iostream>
  22. #include <arpa/inet.h>
  23. #include <fcntl.h>
  24. #include <netdb.h>
  25. using namespace tars;
  26. string now2str(const string &sFormat = "%Y%m%d%H%M%S")
  27. {
  28. time_t t = time(NULL);
  29. struct tm *pTm = localtime(&t);
  30. if(pTm == NULL)
  31. {
  32. return "";
  33. }
  34. char sTimeString[255] = "\0";
  35. strftime(sTimeString, sizeof(sTimeString), sFormat.c_str(), pTm);
  36. return string(sTimeString);
  37. }
  38. void testTC_Socket()
  39. {
  40. TC_Socket tcSocket;
  41. tcSocket.createSocket();
  42. tcSocket.bind("192.168.128.66", 8765);
  43. tcSocket.listen(5);
  44. }
  45. void testTC_TCPClient()
  46. {
  47. TC_TCPClient tc;
  48. tc.init("172.16.28.79", 8382, 3);
  49. cout << now2str() << endl;
  50. int i = 10000;
  51. while(i>0)
  52. {
  53. string s = "test";
  54. char c[1024] = "\0";
  55. size_t length = 4;
  56. int iRet = tc.sendRecv(s.c_str(), s.length(), c, length);
  57. if(iRet < 0)
  58. {
  59. cout << "send recv error:" << iRet << ":" << c << endl;
  60. }
  61. i--;
  62. // cout << c << endl;
  63. assert(c == s);
  64. }
  65. cout << now2str() << endl;
  66. }
  67. void testShortSock()
  68. {
  69. int i = 1000;
  70. while(i>0)
  71. {
  72. TC_TCPClient tc;
  73. tc.init("127.0.0.1", 8382, 10);
  74. string s = "test";
  75. char c[1024] = "\0";
  76. size_t length = 4;
  77. int iRet = tc.sendRecv(s.c_str(), s.length(), c, length);
  78. if(iRet < 0)
  79. {
  80. cout << "send recv error" << endl;
  81. }
  82. if(i % 1000 == 0)
  83. {
  84. cout << i << endl;
  85. }
  86. usleep(10);
  87. i--;
  88. assert(c == s);
  89. }
  90. }
  91. void testUdp()
  92. {
  93. fork();fork();fork();fork();fork();
  94. int i = 1000;
  95. string s;
  96. for(int j = 0; j < 7192; j++)
  97. {
  98. s += "0";
  99. }
  100. s += "\n";
  101. while(i>0)
  102. {
  103. TC_UDPClient tc;
  104. tc.init("127.0.0.1", 8082, 3000);
  105. char c[10240] = "\0";
  106. size_t length = sizeof(c);
  107. int iRet = tc.sendRecv(s.c_str(), s.length(), c, length);
  108. if(iRet < 0)
  109. {
  110. cout << "send recv error:" << iRet << endl;
  111. }
  112. if(i % 1000 == 0)
  113. {
  114. cout << i << endl;
  115. }
  116. i--;
  117. if(c != s)
  118. {
  119. cout << c << endl;
  120. // break;
  121. }
  122. }
  123. }
  124. void testTimeoutSock()
  125. {
  126. int i = 10;
  127. while(i>0)
  128. {
  129. TC_TCPClient tc;
  130. tc.init("127.0.0.1", 8382, 3);
  131. string s = "test";
  132. char c[1024] = "\0";
  133. size_t length = 4;
  134. int iRet = tc.sendRecv(s.c_str(), s.length(), c, length);
  135. if(iRet < 0)
  136. {
  137. cout << "send recv error" << endl;
  138. }
  139. if(i % 1000 == 0)
  140. {
  141. cout << i << endl;
  142. }
  143. i--;
  144. sleep(3);
  145. assert(c == s);
  146. }
  147. }
  148. void testLocalHost()
  149. {
  150. vector<string> v = TC_Socket::getLocalHosts();
  151. cout << TC_Common::tostr(v.begin(), v.end()) << endl;
  152. }
  153. int main(int argc, char *argv[])
  154. {
  155. try
  156. {
  157. testUdp();
  158. return 0;
  159. TC_Socket t;
  160. t.createSocket();
  161. t.bind("127.0.0.1", 0);
  162. string d;
  163. uint16_t p;
  164. t.getSockName(d, p);
  165. t.close();
  166. cout << d << ":" << p << endl;
  167. return 0;
  168. testLocalHost();
  169. string st;
  170. TC_Socket s;
  171. s.createSocket(SOCK_STREAM, AF_LOCAL);
  172. if(argc > 1)
  173. {
  174. s.bind("/tmp/tmp.udp.sock");
  175. s.listen(5);
  176. s.getSockName(st);
  177. cout << st << endl;
  178. struct sockaddr_un stSockAddr;
  179. socklen_t iSockAddrSize = sizeof(sockaddr_un);
  180. TC_Socket c;
  181. s.accept(c, (struct sockaddr *) &stSockAddr, iSockAddrSize);
  182. }
  183. else
  184. {
  185. s.connect("/tmp/tmp.udp.sock");
  186. s.getPeerName(st);
  187. cout << st << endl;
  188. }
  189. }
  190. catch(exception &ex)
  191. {
  192. cout << ex.what() << endl;
  193. }
  194. return 0;
  195. }