Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *MultiThread(void *threadid)
- {
- long tid;
- tid = (long)threadid;
- cout << "Thread with id : " << tid << endl;
- SOCKET vecsocket = INVALID_SOCKET;
- char *sendbuf = "this is a test";
- int iResult;
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- //iResult = getaddrinfo("localhost", "8000", &hints, &result);
- iResult = getaddrinfo("116.62.52.134", DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- cout << "Connect To Server Failed" << endl;
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- }
- // Attempt to connect to an address until one succeeds
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- cout << "Have PTR" << endl;
- vecsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
- if (vecsocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- }
- // Connect to server.
- iResult = connect(vecsocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(vecsocket);
- vecsocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- iResult = send(vecsocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR) {
- printf("send failed with error: %d\n", WSAGetLastError());
- closesocket(vecsocket);
- WSACleanup();
- }
- printf("Bytes Sent: %ld\n", iResult);
- pthread_exit(NULL);
- ///pthread_exit(NULL);
- //closesocket(vecsocket);
- return 0;
- }
- int main() {
- WSADATA wsaData;
- int iResult;
- vector<thread> dddd;
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed with error: %d\n", iResult);
- return 1;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- pthread_t threads[NUM_THREADS];
- int rc;
- int i;
- for (i = 0; i < NUM_THREADS; i++){
- //cout << "main() : creating thread, " << i << endl;
- rc = pthread_create(&threads[i], NULL, MultiThread, (void *)i);
- if (rc){
- cout << "Error:unable to create thread," << rc << endl;
- exit(-1);
- }
- }
- pthread_exit(NULL);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement