Socket error ESOCKTNOSUPPORT problem

Im getting ESOCKNOSUPPORT error when reading from a socket.
But im using SOCK_STREAM type and I dont see any problem why this socket type wont be supported.

Would this happen if I have a invalid socket? The client was able to connect to the “connectme” socket and was communicating with it for a certain time,and suddently this error occurs .

Anyone come across such problem?
Any help would be greatly appreiciated.

Kishore,

Please provide us with a relevant code snippet from your application.

Cameron

Here is my code:


The thread is used to start server and communicate with client and im running it at higher priority than the other threads

void RunServer(unsigned long thread_input)
{
struct sockaddr_in fsin;
int sock,ClientSock;
int not_used;

       /* Internet endpoint address	*/
int                 result;
int                 buffsize = RCV_BUFFER_SIZE;

int timeout = 0;	/* timeout for send and recv calls -nearly 2 minutes*/

unsigned char fResult = 1;
/* Create the socket for TCP communication */
if((sock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 
{
	printf("Socket Creation Error...

");
return;
}
else
{
printf("Socket Creation Successfull…
");
}
/* set socket to use Blocking IO */
result = setsockopt(sock, SOL_SOCKET, SO_BIO, (char *)&not_used, sizeof(not_used));
if (result < 0)
{
printf("UDPecho: setsockopt SO_BIO failure: errno: %d
", getErrno());
socketclose(sock);
return;
}

/* reset structure contents to zero */
memset((char*)&amp;fsin, '0', sizeof(fsin));

/* fill socket structure to the MODBUS port and the local machine IP */
fsin.sin_family = AF_INET;

fsin.sin_port = htons(8085);

fsin.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sock, &amp;fsin, sizeof(fsin)) &lt; 0)
{
    
    printf(" bind failure: errno: %d

", getErrno());
socketclose(sock);
return;
}

printf(" Server Ready on port %d.

", 8085);

if( listen( sock, 1) == SOCKET_ERROR )
{
printf("bind failure: errno: %d

", getErrno());
return ;
}
else
{
/* for ever */
while(1)
{
printf("Slave Active…
Waiting for New connection …
");
if( (ClientSock = accept( sock, NULL, NULL )) == INVALID_SOCKET )
{
printf(“Error Accepting New Connection…”);
return ;
}
printf("New Connection Request Accepted…

");

while(1)
{
 do
 {

//receive is handled here
fResult = HandleRequest((unsigned long)ClientSock);
}
while(fResult);

if( Slave_LastMbErr == ECOMMPATHERROR || Slave_LastMbErr == ETIMEOUT)
{
break;
}
}
		/* HandleRequest() indicated a communication errorSo reset communication by closing the socket and reopening the same  	 */
closesocket(ClientSock);
printf("Closing Socket...

");
ClientSock = 0;
}
}

}


Read function called in HandleRequest() looks so:

int ReadSerialPort(xdata DWORD CommnPathNo, xdata int nBytes, xdata unsigned char * xdata Buffer)
{
struct timeval wait;
int iPortReady;
struct fd_set fd_rcv;
wait.tv_sec=20;
wait.tv_usec=20*1000; //20ms
int errcode;

FD_ZERO(&amp;fd_rcv);
#ifndef NETOS_GNU_TOOLS
FD_SET((SOCKET)CommnPathNo,&amp;fd_rcv);
#else
FD_SET((int)CommnPathNo,&amp;fd_rcv);
#endif

iPortReady=select(FD_SETSIZE,&amp;fd_rcv,NULL,NULL,&amp;wait);
if(iPortReady == 0)
{
	printf("Rcv Socket timedout

");
nBytes = -1;
return nBytes;
}

//recvfrom(sock, rcvData, RCV_BUFFER_SIZE, 0, &amp;fsin, &amp;szfsin);
if(FD_ISSET(CommnPathNo,&amp;fd_rcv)) {
	if( (nBytes = recv((int)CommnPathNo, Buffer, nBytes, 0 )) &lt;= 0 )
	{
		if(nBytes&lt;0)
		{
			printf("Error reading bytes from socket, errorcode = %d

",nBytes);
errcode=getErrno();
printf("Error no -%d
",errcode);
if(errcode == ENOTCONN){
nBytes = - 1;
printf("Socket Rcv Error -ENOTCONN
");

			}
			else if(errcode == ENOTSOCK){
				nBytes = - 1;
				printf("Socket Rcv Error -ENOTSOCK

");
}
else if(errcode == EWOULDBLOCK){
nBytes = - 1;
printf("Socket Rcv Error -EWOULDBLOCK
");
}
else
{


//ESOCKNOTSUPPORT or errorcode =124 occurs offen here


	 	nBytes=1; //Set this to 1 ,that the connection dont close
			 	printf("Read error ,but continue to listen

");
}
}
else { printf("Socket connection closed by peer
");nBytes= -1;}

	}
} else {nBytes = -1;	}
return nBytes;

}

Hope this helps,
Kishore

Sorry,I receive ECONNRESET (104) and not ESOCKTNOSUPPORT(124).

So ECONNRESET mean “connection reset by peer” ,right?
Since my client is contiually sending request every 5 sec.I guess this might happen when the client could nt communicate with the connectme server for certain amount of time ,then it timesout with ECONNRESET.

But why couldnt it communicate? I guess this could happend when server doesnt get its timeslice to act on recieved bytes, while some other thread is blocking it for long time then desired.

If this is the case ,should I try using a higher priority for socket thread? Would this help?

Regards,
Kishore

A note:

  1. SIO_BIO? Last I saw NetOS still used SO_NONBLOCK.
  2. while(1) in a network connection? shudder The way this is written it will do everything once. If the connection is lost you are out of luck. Maybe a more dynamic implimentation would be safer.
  3. If recv() gets a -1, you need to reopen the socket. A zero is “reset by peer”, a -1 is general error and needs a socket reset.
  4. Try a tx_thread_sleep() for a few milliseconds to allow NetOS some gasping room (select doesn’t do that very well). NetOS is unpredictable unless you give it some free time in other threads.
  5. Take a look back a few threads for the multithreaded version of select(), you will get an occasional error using it directly like this.
  6. You are returning -1, 0, and 1 to fResult. -1 or 1 will keep it in the loop. You also need to exit if you get a -1. Look for “while(fResult>0)” to make it work.

-Erik

Hi Erik,
Thanks for the response.
Are u sure the Netos dont support blocking sockets? where did u see in NetOs about not supporting the SO_BIO?

If im going to use SO_NONBLOCK,i dont need select() at all.But with my current implementation, your suggestion with sleep() makes sense.

Kishore

I don’t know about SIO_BIO, if the define is there I would assume it works. I just know that it doesn’t exist on my 6.0 version. Maybe in 6.1 it is there.

Actually, if you are turning off blocking, that is when you DO need select(). How else are you going to be able to tell when the socket closes? With blocking off, a return of “0” from recv() is common.

Also, take a look at my #6. That is a common bug. Remember that a -1 is really a 65535 which is not zero.

-Erik

Hi,
Im having no problem in using select with SO_BIO,as u can see in my receive thread.I make use of FD_SET and wait option for this.

Thanks for ur other suggestion to not use 1 for condition while loop.Now im using while(fresult>0) instead.

Thanks and Regards,
Kishore

At this point, what is the problem?

-Erik

The problem is solved. The client socket always disconnected,and i was wondering why when im having a higher priority for my receive thread. But in the mean time i found there was another thread which blocks for long time,not allowing the receive thread the time to run ,which results in client disconnection.

Since I changed my priorities,things are working as expected.

Thanks,
Kishore