Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. void *MultiThread(void *threadid)
  2. {
  3.  
  4. long tid;
  5. tid = (long)threadid;
  6. cout << "Thread with id : " << tid << endl;
  7.  
  8.  
  9. SOCKET vecsocket = INVALID_SOCKET;
  10.  
  11. char *sendbuf = "this is a test";
  12. int iResult;
  13. struct addrinfo *result = NULL,
  14. *ptr = NULL,
  15. hints;
  16.  
  17. ZeroMemory(&hints, sizeof(hints));
  18. hints.ai_family = AF_UNSPEC;
  19. hints.ai_socktype = SOCK_STREAM;
  20. hints.ai_protocol = IPPROTO_TCP;
  21. //iResult = getaddrinfo("localhost", "8000", &hints, &result);
  22. iResult = getaddrinfo("116.62.52.134", DEFAULT_PORT, &hints, &result);
  23. if (iResult != 0) {
  24. cout << "Connect To Server Failed" << endl;
  25. printf("getaddrinfo failed with error: %d\n", iResult);
  26. WSACleanup();
  27.  
  28. }
  29. // Attempt to connect to an address until one succeeds
  30. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  31.  
  32. cout << "Have PTR" << endl;
  33. vecsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
  34. if (vecsocket == INVALID_SOCKET) {
  35. printf("socket failed with error: %ld\n", WSAGetLastError());
  36. WSACleanup();
  37. }
  38.  
  39.  
  40. // Connect to server.
  41. iResult = connect(vecsocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  42.  
  43. if (iResult == SOCKET_ERROR) {
  44. closesocket(vecsocket);
  45. vecsocket = INVALID_SOCKET;
  46. continue;
  47. }
  48. break;
  49. }
  50.  
  51.  
  52. iResult = send(vecsocket, sendbuf, (int)strlen(sendbuf), 0);
  53. if (iResult == SOCKET_ERROR) {
  54. printf("send failed with error: %d\n", WSAGetLastError());
  55. closesocket(vecsocket);
  56. WSACleanup();
  57. }
  58.  
  59. printf("Bytes Sent: %ld\n", iResult);
  60. pthread_exit(NULL);
  61.  
  62. ///pthread_exit(NULL);
  63. //closesocket(vecsocket);
  64. return 0;
  65.  
  66. }
  67.  
  68.  
  69. int main() {
  70.  
  71. WSADATA wsaData;
  72. int iResult;
  73. vector<thread> dddd;
  74.  
  75. struct addrinfo *result = NULL,
  76. *ptr = NULL,
  77. hints;
  78.  
  79. // Initialize Winsock
  80. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  81. if (iResult != 0) {
  82. printf("WSAStartup failed with error: %d\n", iResult);
  83. return 1;
  84. }
  85.  
  86. ZeroMemory(&hints, sizeof(hints));
  87. hints.ai_family = AF_UNSPEC;
  88. hints.ai_socktype = SOCK_STREAM;
  89. hints.ai_protocol = IPPROTO_TCP;
  90.  
  91.  
  92. pthread_t threads[NUM_THREADS];
  93. int rc;
  94. int i;
  95.  
  96. for (i = 0; i < NUM_THREADS; i++){
  97. //cout << "main() : creating thread, " << i << endl;
  98. rc = pthread_create(&threads[i], NULL, MultiThread, (void *)i);
  99.  
  100. if (rc){
  101. cout << "Error:unable to create thread," << rc << endl;
  102. exit(-1);
  103. }
  104. }
  105.  
  106. pthread_exit(NULL);
  107. WSACleanup();
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement