socket function returning = 10038
Code:
bool CSocketComm::ConnectTo(LPCTSTR strDestination, LPCTSTR strServiceName, int nFamily, int nType){
// Socket is already opened
if ( IsOpen() ){
return false;
}
// Create a Socket that is bound to a specific service provide
// nFamily: (AF_INET)
// nType: (SOCK_STREAM, SOCK_DGRAM)
WSASetLastError(0);
SOCKET sock = socket(nFamily, nType, 0);
if (INVALID_SOCKET != sock){
// Associate a local address with the socket
SockAddrIn sockAddr;
if (false == sockAddr.CreateFrom(NULL, TEXT("0"), nFamily)){
closesocket( sock );
return false;
}
if ( SOCKET_ERROR == bind(sock, sockAddr, sockAddr.Size() )){
closesocket( sock );
return false;
}
// Now get destination address & port
sockAddr.CreateFrom( strDestination, strServiceName );
// try to connect - if fail, server not ready
if (SOCKET_ERROR == connect( sock, sockAddr, sockAddr.Size())){
closesocket( sock );
return false;
}
// Success, now we may save this socket
m_hComm = (HANDLE) sock;
Inactive=false;
//Allocacion para el IncBuffer
IncBuffer.reserve(1024*512); //512 KB
//Reseting
Porcentaje=0;
Total=0;
Progreso=0;
PckType=0;
}else{
Fncs.Loguear("socket returned Invalid Handle",WSAGetLastError());
}
//Fncs.Loguear("WSAErr",WSAGetLastError());
return (INVALID_SOCKET != sock);
}
why would the socket Function return INVALID_SOCKET and in the WSAGetLastError return 10038
Wich is:
WSAENOTSOCK
10038
Socket operation on nonsocket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.
when on the socket function documentation 10038 is not a valid error code for its call
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
may be regarded to the buffer i dont know what to think this is being cracking me.
Thx In Advance!!
Re: socket function returning = 10038
Instead of just posting a class member function, how do we know what happened before you called this function? Maybe Winsock was not initialized successfully.
This is where you need to go through the entire program, and making sure that all calls are successful, including the startup of Winsock.
Regards,
Paul McKenzie
Re: socket function returning = 10038
WSAStartup called (as suggested by Paul McKenzie)???
Incidentally, calling bind() on a client socket will probably not do what you think it does, and almost always is wrong or at least ineffectual.
Mike
Re: socket function returning = 10038
Quote:
Originally Posted by
MikeAThon
WSAStartup called (as suggested by Paul McKenzie)???
Incidentally, calling bind() on a client socket will probably not do what you think it does, and almost always is wrong or at least ineffectual.
Mike
Hello thx for ur time
yes WSAStartup was called, the program works fine but gives this trouble i am mentioning when i close socket+thread and then more conections arrive one of the clients may drop that error i mentioned.
each connection is a separated thread: they start like this:
Code:
bool CSocketComm::WatchComm(){
if (!IsStart())
{
if (IsOpen())
{
HANDLE hThread;
UINT uiThreadId = 0;
hThread = (HANDLE)_beginthreadex(NULL, // Security attributes
0, // stack
SocketThreadProc, // Thread proc
this, // Thread param
CREATE_SUSPENDED, // creation mode
&uiThreadId); // Thread ID
if ( NULL != hThread){
//SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
ResumeThread( hThread );
m_hThread = hThread;
return true;
}
}
}
return false;
}
this is how they are stoped
Code:
void CSocketComm::StopComm(){
// Close Socket
if (IsOpen()){
CloseComm();
}
// Kill Thread
if (IsStart()){
SleepEx(DEFAULT_TIMEOUT, TRUE);
if (WaitForSingleObject(m_hThread, 10000L) == WAIT_TIMEOUT)
Fncs.Loguear("Thread Brutalmente Detenida");
TerminateThread(m_hThread, 1L);
CloseHandle(m_hThread);
m_hThread = NULL;
}
// Clear Address list
if (!m_AddrList.empty())
{
m_AddrList.clear();
}
/* Release system object when all finished -- usually at the end of the cleanup code */
//DeleteCriticalSection(&CrtS1);
// Destroy Synchronization objects
if (NULL != m_hMutex){
CloseHandle( m_hMutex );
m_hMutex = NULL;
}
//Delocating IncBuffer
//Reseting
Halt=false;
Inactive=true;
Porcentaje=0;
Total=0;
Progreso=0;
PckType=0;
}
CloseComm calls this... it shutdowns and then calls close socket.
Code:
bool CSocketComm::ShutdownConnection(SOCKET sock){
shutdown(sock, SD_BOTH);
return ( 0 == closesocket( sock ));
}
and this is what the thread proc runs.
Code:
void CSocketComm::Run(){
stMessageProxy stMsgProxy;
DWORD dwBytes = 0L;
DWORD dwTimeout = INFINITE; //ERA INFINITE ORIGINALMENTE
LPBYTE lpData = (LPBYTE)&stMsgProxy;
DWORD dwSize = sizeof(stMsgProxy);
bool bSmartAddressing = IsSmartAddressing();
if ( !bSmartAddressing )
{
lpData = stMsgProxy.byData;
dwSize = sizeof(stMsgProxy.byData);
}
// Should we run as server mode
if (IsServer() && !bSmartAddressing)
{
if (!IsBroadcast())
{
SOCKET sock = (SOCKET) m_hComm;
sock = WaitForConnection( sock );
// Get new connection socket
if (sock != INVALID_SOCKET)
{
ShutdownConnection( (SOCKET) m_hComm);
m_hComm = (HANDLE) sock;
OnEvent( EVT_CONSUCCESS, NULL ); // connect
}
else
{
// Do not send event if we are closing
if (IsOpen())
OnEvent( EVT_CONFAILURE, NULL ); // wait fail
return;
}
}
}
else
{
GetPeerName( stMsgProxy.address );
}
while( IsOpen()){
// Blocking mode: Wait for event
dwBytes = ReadComm(lpData, dwSize, dwTimeout);
// Error? - need to signal error
if (dwBytes == (DWORD)-1L)
{
// Do not send event if we are closing
if (IsOpen())
{
if ( bSmartAddressing )
{
RemoveFromList( stMsgProxy.address );
}
OnEvent( EVT_CONDROP, &stMsgProxy.address ); // lost connection
}
// special case for UDP, alert about the event but do not stop
if ( bSmartAddressing )
continue;
else
break;
}
// Chars received?
if ( bSmartAddressing && dwBytes == sizeof(SOCKADDR_IN))
{
OnEvent( EVT_ZEROLENGTH, NULL );
}
else if (dwBytes > 0L)
{
OnDataReceived( lpData, dwBytes);
}
//Sleep(0);
}
}
i cant explicitly call stopcomm from the connection drop it will cause a dead lock because the thread trying to exit itself.
so on the connection drop event i set a private internal bool Inactive to true.
and then on another thred i call manteniance it calls stop comms on all the sockets that had been marked as inactive.
it works fine creating new connection the troubles comes when i call stopcomm it messes all up.
i am trying to debug but why would socket return a Error code that doesnt correspond to that function according to msdn isnot one of its valid errors.
Re: socket function returning = 10038
Quote:
Originally Posted by
Alphadan
yes WSAStartup was called, the program works fine but gives this trouble i am mentioning when i close socket+thread and then more conections arrive one of the clients may drop that error i mentioned.
In your original post, you said that you received 10038 when calling the socket() function. In your words: "why would the socket Function return INVALID_SOCKET and in the WSAGetLastError return 10038 "
Now you are telling us that 10038 error is received "when I close socket+thread".
But later on in the same post, you tell us this: "i am trying to debug but why would socket return a Error code that doesnt correspond to that function according to msdn isnot one of its valid errors." So, it seems that we are back to the socket() function causing 10038
At this point, no one here knows exactly where and when you receive the 10038 error.
Please explain more clearly. Identify in the code exactly where you are receiving the 10038 error. Highlight the line of code.
Incidentally, the code you show us does not apparently follow good multi-threading practices. There are many clues, and you can tell right away, since (for example): 1. you are using the SleepEx function and the WFSO function solely for purposes of a time-out delay; 2. you are using the TerminateThread function; and 3. you are re-using the m_hThread variable for multiple different threads/clients without reserving a separate varaiable for each such thread/client. There are many other clues that good multi-threading practices are not being followed. You might also be touching the UI from a thread (another 'no-no') but we have not seen all of your code (what does OnEvent do, for instance). It might not be possible to resolve your difficulties without a thorough re-write of the code. Sorry.
Re: socket function returning = 10038
Quote:
Originally Posted by
MikeAThon
In your original post, you said that you received 10038 when calling the socket() function. In your words: "why would the socket Function return INVALID_SOCKET and in the WSAGetLastError return 10038 "
Now you are telling us that 10038 error is received "when I close socket+thread".
this line causes the 10038 error on WSAGetLastError(); "But only when the class instance is being reused" thats why i mean when I close socket+thread.
Code:
SOCKET sock = socket(nFamily, nType, 0);
Quote:
Originally Posted by
MikeAThon
2. you are using the TerminateThread function;
The Terminate Thread is Used as last resort if the thread doesnt exit on the time specified in the WFSO timeout.
i even add a error to log if the thread ends this way.
Quote:
Originally Posted by
MikeAThon
3. you are re-using the m_hThread variable for multiple different threads/clients without reserving a separate varaiable for each such thread/client.
notice the m_ its a member variable of the instance each thread works on a separated instance.
so no variable is being reused on a different thread.
Quote:
Originally Posted by
MikeAThon
but we have not seen all of your code (what does OnEvent do
OnEvent is just a function called by the thread proc depending if there is data for read on the socket, or if the socket loses the connection, when the connection is succed etc etc...
btw u said the stopcomm follows bad practices would u give any advice how can i improve it to exit the thread and close the socket on that instance?... btw i dont use terminate thread "only as last resort if the thread is locked and doesnt exits in time.
Re: socket function returning = 10038
Quote:
Originally Posted by
Alphadan
this is the line that causes the 10038 error on WSAGetLastError();
Code:
SOCKET sock = socket(nFamily, nType, 0);
Thank you for clarifying.
You might try to position the call to WSAGetLastError() directly underneath the call to socket():
Code:
SOCKET sock = socket(nFamily, nType, 0);
int iErrTemp = WSAGetLastError();
// ... more code
// ... and then, eventually:
Fncs.Loguear("WSAErr",iErrTemp);
WSAGetLastError() is thread-specific, so other threads should not be over-writing the result. But perhaps intervening function calls within the current thread are affecting the result of WSAGetLastError(). By positioning the call directly underneath the call to socket(), you might get a more accurate result.
Mike