LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sendto returns ENOTBLK error (https://www.linuxquestions.org/questions/programming-9/sendto-returns-enotblk-error-4175420853/)

silva_antonio 08-07-2012 11:50 AM

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

dwhitney67 08-07-2012 11:54 AM

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?

NevemTeve 08-07-2012 12:05 PM

You mean 'sendto' returned 15?

silva_antonio 08-07-2012 12:27 PM

Hi, yes sendto return 15.

silva_antonio 08-07-2012 01:45 PM

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

NevemTeve 08-07-2012 02:00 PM

Try this to switch to non-blocking mode:

Code:

    int optval= 1;
    ioctl (handle, FIONBIO, &option);


dwhitney67 08-07-2012 02:50 PM

Quote:

Originally Posted by silva_antonio (Post 4748314)
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?

NevemTeve 08-07-2012 05:12 PM

Quote:

Originally Posted by silva_antonio (Post 4748314)
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));

silva_antonio 08-08-2012 04:40 AM

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));

NevemTeve 08-08-2012 04:45 AM

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.

silva_antonio 08-08-2012 04:49 AM

Quote:

Originally Posted by NevemTeve (Post 4748798)
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

NevemTeve 08-08-2012 05:13 AM

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.

silva_antonio 08-10-2012 08:44 AM

Quote:

Originally Posted by NevemTeve (Post 4748811)
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;       
}


silva_antonio 08-10-2012 09:21 AM

Quote:

Originally Posted by NevemTeve (Post 4748321)
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

silva_antonio 08-10-2012 09:27 AM

Quote:

Originally Posted by NevemTeve (Post 4748811)
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


All times are GMT -5. The time now is 06:44 AM.