LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2012, 11:50 AM   #1
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Rep: Reputation: Disabled
sendto returns ENOTBLK error


Hi all,

I have a socket for each client to receive UDP packets from the clients and then send it to a SIP server.
I receive the packets from the clients correctly without any errors.

I then use the same socket for sending USP packets back to the clients but when I try to send a packet through the socket, a call to sendto returns an error with code 15 which corresponds to ENOTBLK.

I cannot relate this error code to any problem regarding sockets.

Does anyone have a hint about what may be the problem?

Thanks in advance,

Antonio
 
Old 08-07-2012, 11:54 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Errors are generally easier to diagnose if one has the benefit of seeing the code that is causing the error. Therefore, could you please post the code where you are performing the sendto(), including the declarations and setup of variables that hold the data that is being sent?

P.S. Perhaps you are using the incorrect descriptor for the socket?
 
Old 08-07-2012, 12:05 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
You mean 'sendto' returned 15?
 
Old 08-07-2012, 12:27 PM   #4
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi, yes sendto return 15.
 
Old 08-07-2012, 01:45 PM   #5
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi all, an small correction to my previous post:

sendto returns -1, and consulting errno gives me the error code 15: ENOTBLK

I have not provided the code because it does not have anything particular. The main issue is that sendto should not return this error!!!

Regarding source code:
This is how the socket is opened:

handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if(handle == -1)
return(ERROR);

x=fcntl(handle,F_GETFL,0); // Get socket flags
fcntl(handle,F_SETFL,x | O_NONBLOCK); // Add non-blocking flag

ac5xServiceAddr.sin_family = AF_INET;
ac5xServiceAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ac5xServiceAddr.sin_port = htons(SourcePort);

ReturnedStatus = bind(handle, (struct sockaddr *)&(ac5xServiceAddr), sizeof(struct sockaddr_in));


and this is how data is sent:

BytesSent = sendto(handle, pPacket, Size, 0, (struct sockaddr*)ac5xServiceAddr, sizeof(struct sockaddr_in));

if (BytesSent==-1)
{
errorNum = errno;
printf("error=0x%08X", errorNum);
}

and this is how data is received:

struct sockaddr_in SocketAddr;
int SocketAddrSize;

pPacketSize = recvfrom(handle, pPacketBuffer, MAX_SIZE, 0, (struct sockaddr*)&SocketAddr, (socklen_t *)&SocketAddrSize);


I have a thread that is continuosly (with some usleep) checking data received and another thread that send data onto the same socket.

I can receive packets but I cannot send packets through this socket.

Regards
 
Old 08-07-2012, 02:00 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Try this to switch to non-blocking mode:

Code:
    int optval= 1;
    ioctl (handle, FIONBIO, &option);
 
Old 08-07-2012, 02:50 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by silva_antonio View Post
I can receive packets but I cannot send packets through this socket.
And where is the packet sent to? Is it to the same client that issued a packet to your Receiver thread?
 
Old 08-07-2012, 05:12 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Quote:
Originally Posted by silva_antonio View Post
and this is how data is sent:

BytesSent = sendto(handle, pPacket, Size, 0, (struct sockaddr*)ac5xServiceAddr, sizeof(struct sockaddr_in));
It should be
Code:
BytesSent = sendto(handle, pPacket, Size, 0, (struct sockaddr*)&SocketAddr, sizeof(struct sockaddr_in));

Last edited by NevemTeve; 08-08-2012 at 02:40 AM. Reason: code tag added
 
Old 08-08-2012, 04:40 AM   #9
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi, the client is the same that sent the packet received by the server.
I have tried to change the address to a different client (ADDR1) in order to route the packet to another client but the result is the same.
I have created a mirror socket to send the same packet to another address(ADDR1), and in this case I can send the packet without any problem.

I believe it is something related of how the socket is created.

Regards

PS: I made a mistake, in fact in my code I have:
BytesSent = sendto(handle, pPacket, Size, 0, (struct sockaddr*)&SocketAddr, sizeof(struct sockaddr_in));
 
Old 08-08-2012, 04:45 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Okay, now leave out everything from your code that is not essential in reproducing the problem, and past in the rest. But do use [code] and [/code] tags.
 
Old 08-08-2012, 04:49 AM   #11
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Okay, now leave out everything from your code that is not essential in reproducing the problem, and past in the rest. But do use
Code:
 and
tags.
Hi, the code provided is the essential to reproduce the problem.
Regards
 
Old 08-08-2012, 05:13 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Which part of my suggestion is problematic? Okay, I'll rephrase it:

Okay, now leave out everything from your code that is not essential in reproducing the problem, until you get a complete and working minimal-program that shows the problem. Then paste it in, but do use [code] and [/code] tags.
 
Old 08-10-2012, 08:44 AM   #13
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Which part of my suggestion is problematic? Okay, I'll rephrase it:

Okay, now leave out everything from your code that is not essential in reproducing the problem, until you get a complete and working minimal-program that shows the problem. Then paste it in, but do use
Code:
 and
tags.
Hi there, sorry for the delay but see bellow the source code.

I've been finding some particularities:
1. I have besides this socket, an management socket, created in this manner but it is only opened once and sends and receives packets without any problem.
2. This socket that experiences this problem is opened and closed several times (for reconfiguration purposes).
3. I have created a mirror socket which sends every data the server sends to the clients to my PC. This socket works great but it is opened like a client (OpenRecordingSocket).

Which condition(s) may originate the error I am getting, ENOTBLK?


Open Socket
Code:
int OpenServerSocket(int SourcePort, U8 BroadCast)
{
    int				ReturnedStatus;	
    int			        handle = -1;
    int 			BufferSize;
    int 			VarLength = sizeof(BufferSize);
    int                         x;

	handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	if(handle == -1)
		return -1;
    	
        x=fcntl(handle,F_GETFL,0);              // Get socket flags
        fcntl(handle,F_SETFL,x | O_NONBLOCK);   // Add non-blocking flag		

	memset(&ac5xServiceAddr, 0, sizeof(ac5xServiceAddr));

	ac5xServiceAddr.sin_family = AF_INET;
	ac5xServiceAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	ac5xServiceAddr.sin_port = htons(SourcePort);

	ReturnedStatus = bind(handle, (struct sockaddr *)&(ac5xServiceAddr), sizeof(struct sockaddr_in));

	if (ReturnedStatus == -1)
	{
		int yes = 1;
		setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
		getsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, (socklen_t *)&VarLength);		
		ReturnedStatus = bind(handle, (struct sockaddr *)&(ac5xServiceAddr), sizeof(struct sockaddr_in));
	}

	if(ReturnedStatus == -1)
	{
        ReturnedStatus = close(handle);	
        return -1;
	} 

	int on = (int)BroadCast;
	setsockopt(handle, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on));
	getsockopt(handle, SOL_SOCKET, SO_BROADCAST, (char*)&on, (socklen_t *)&VarLength);

	return handle;	
}
Receive Packet
Code:
int ReceiveMiiPacket(int socketHandle, char* pPacketBuffer, int* pPacketSize)
{
struct sockaddr_in		SocketAddr;
int				SocketAddrSize;

SocketAddrSize = sizeof(struct sockaddr_in);
*pPacketSize = recvfrom(socketHandle, (char*)pPacketBuffer, 1500, 0, (struct sockaddr*)&SocketAddr, (socklen_t *)&SocketAddrSize);
	
return *pPacketSize;
}

Send Packet
Code:
int SendMiiPacket(int socketHandle, char* pPacket, int Size)
{
	int			BytesSent, errorNum; 
	struct sockaddr_in*	pSockAddr; 

	pSockAddr = GetSocketAddress(socketHandle);

	BytesSent = sendto(socketHandle, pPacket, Size, 0, (struct sockaddr*)pSockAddr, sizeof(struct sockaddr_in));

	if (BytesSent==-1)
		{
                errorNum = errno;
                printf("SendMiiPacket failed with error=0x%08X \n", errorNum);
		}

	return BytesSent;
}

Code:
int OpenRecordingSocket(void)
{
    int			handle = -1;
    int 		BufferSize;
    int 		VarLength = sizeof(BufferSize);
    int                 x;
    int                 SourcePort = 50505;
    int                 BroadCast = FALSE;
    
	handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	if(handle == -1){
		return -1;
        }

        x=fcntl(handle,F_GETFL,0);              // Get socket flags
        fcntl(handle,F_SETFL,x | O_NONBLOCK);   // Add non-blocking flag

	memset(&TargetAddress, 0, sizeof(TargetAddress));

	TargetAddress.sin_family = AF_INET;
	TargetAddress.sin_addr.s_addr = inet_addr(LAPTOP_IPADDR);
	TargetAddress.sin_port = htons(SourcePort);

	int on = (int)BroadCast;
	setsockopt(handle, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on));
	getsockopt(handle, SOL_SOCKET, SO_BROADCAST, (char*)&on, (socklen_t *)&VarLength);

	return handle;	
}
 
Old 08-10-2012, 09:21 AM   #14
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Try this to switch to non-blocking mode:

Code:
    int optval= 1;
    ioctl (handle, FIONBIO, &option);
Hi I have also use this way for configuring the non-blocking mode but the results were the same.
Regards
 
Old 08-10-2012, 09:27 AM   #15
silva_antonio
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Which part of my suggestion is problematic? Okay, I'll rephrase it:

Okay, now leave out everything from your code that is not essential in reproducing the problem, until you get a complete and working minimal-program that shows the problem. Then paste it in, but do use
Code:
 and
tags.
My project is too complex to simulate in an small program the problems that I am getting (there's a complex process of client configurations prior of normal operation).
I will try to get this done but the core functions used are the ones posted.
Regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Php returns this error adrianc.grigoras Linux - Software 1 02-22-2009 09:53 PM
apache 1.3.27, https url returns error -12263 in Firefox, fatal error (40) in Opera leo22838 Linux - Server 0 04-04-2008 01:51 PM
SWIG wrap.c file returns error: struct_vector_wrap.c:2413: error: dereferencing point varun_shrivastava Programming 0 05-25-2007 04:47 AM
sendto error (socket) payal_shah Linux - Networking 0 02-23-2005 12:18 PM
nmap raw packet sendto error in Mandrake 9.2 conn-fused Linux - Networking 0 06-30-2004 02:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration