I need a nonblocking socket so in an older version of the program i made a new thread to do this. and now that ive migrated into visual c++ it crashes with no explination... yea im really aggravated... can anyone tell me why this crashes?

#include <iostream>
#include <winsock.h>
#include <string.h>
#include <sstream>
#include <signal.h>
#include <fstream>
#include <conio.h>
#include <sstream>

char *goodMsg=new char[2048];
HANDLE threadH;
DWORD threadD;
bool threadV=false;
int curport;
bool ping();
bool threadPing(int port, int chTm){
	curport=port;
       threadV=false;
	   try{

        threadH=  CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ping,NULL,0,&threadD);

	   } catch(System::Exception ^er){// doesnt catch anything... just crashes
		int i=GetLastError();
	   }
		Sleep(400);
        WaitForSingleObject(threadH,chTm);
        return (GetLastError()==WAIT_TIMEOUT||threadV==false);
               
}
 bool ping(){         
    WORD sockVer;
    WSADATA wsaData;
    sockVer = MAKEWORD(1, 1);
    WSAStartup(sockVer, &wsaData);
    int dataBuffer;
    char *buffer="\\status\\";
    SOCKET mysock;
    mysock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (mysock == INVALID_SOCKET) {
       dataBuffer = WSAGetLastError();
       WSACleanup();
       return false;
    }
    SOCKADDR_IN srvrData;
    srvrData.sin_family = AF_INET;
    srvrData.sin_addr.s_addr = inet_addr("127.0.0.1");
    srvrData.sin_port = htons(curport);
    dataBuffer = connect(mysock,(LPSOCKADDR)&srvrData,sizeof(struct sockaddr));
    if (dataBuffer == SOCKET_ERROR) {
       return false;
    }
    if( send(mysock,buffer,strlen(buffer), 0)!=strlen(buffer)){
    WSACleanup();
           return false;                                      
     }
   if(recv(mysock, goodMsg, 2048, 0)<0){
    WSACleanup();
    return false;
    }
    WSACleanup();
    closesocket(mysock);
    threadV=true;
    return true;

}

Replace..

catch(System::Exception ^er)

with

catch(...) //etc..

I fixed it by using _beginthread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.