Resource temporarily unavailable linux socket

UNIX Socket FAQ

Now I am programming to communicate with some network printer through TCP Socket program.By sending command «\033E 1\r» to printer,causes, check the port for error normally.

In my case i used following code

bytesSent = send( sockfd, "\033E 1\r",sizeof("\033E 1\r"), 0); bytesRecv = recv( sockfd,recvbuf, 1024, 0 );

Here command goes to printer proper manner.But when i try to receive the recv gets blocking can not able to read anything its hanging there.

Then i used Non Blocking Socket with select() method in this case i am getting «Resource temporarily unavailable».

Please help me to figure out this error

Thanks in Advance
Regards
Siva

#2 2010-03-02 01:21 PM

Re: Resource temporarily unavailable Error In Socket

When you do a read on a socket and there’s nothing to read then this happens:

1) If you are using blocking sockets, the read call will block until data arrives.

2) If using non-blocking sockets, the read won’t block, but return with an error
EAGAIN/EWOULDBLOCK, which means there’s nothing to read.

So your real problem is the printer not sending you any data at all. The «error»
is not a real error, but a way to tell you what you already knew: That there is
(yet) nothing to read.

If I had to guess the printer either expects you to send something else or more,
or it behaves as printers do and just stopped working for no apparent reason.

#3 2010-03-02 01:36 PM

Re: Resource temporarily unavailable Error In Socket

But in printer port have some error.I comes to know through the LCD display of Printer.Even same thing i applied in USB Printer it gives the error report.In network printer i cant able to read even port has error.

#4 2010-03-02 02:59 PM

Re: Resource temporarily unavailable Error In Socket

Are you trying to print out a «1»? If not, try removing the space.

I’d make sure the command you’re sending is correct, and to reset the
printer before sending it new stuff. Also, what reply do you expect?
And I’d try to adding a \n after the \r, or instead of it.

#5 2010-03-03 04:45 AM

Re: Resource temporarily unavailable Error In Socket

The Command «\033E 1\r» is the printer specific command.When i send this one it will check the printer port for error in case error is there it will return the error number else it will return the Printer make and model string.

Читайте также:  Linux permission denied on folder

As of now even in blocking and non blocking i cant able to receive anything from printer i want to resolve the problem please help me.

#6 2010-03-03 10:55 AM

Re: Resource temporarily unavailable Error In Socket

Are you really sure it’s exactly ‘\033E 1\r’? Not \n, or withut space or binary 1
instead of ASCII ‘1’?

#7 2010-03-03 01:57 PM

Re: Resource temporarily unavailable Error In Socket

I Know very well the command is «\033E 1\r».We only developed the firmware to the printer.

#8 2010-03-03 02:43 PM

Re: Resource temporarily unavailable Error In Socket

You could try to look at what’s being send with tcpdump or Wireshark,
maybe that sheds some light on the matter. I think it’s a miscommunication
between the printer and this software, or there’s a bug in the printer
firmware, or the network part stopped working after the error.

If you trust the printer to be okay then you’d need to post more of your code
if you want us to have any chance to find the bug. Your original question is
answered though: You get that behaviour because the printer doesn’t send
anything. It’s possible, but highly unlikely, it’s because you didn’t send the full
command: Check bytesSent.

What happens when you send that command just after the printer started up?

#9 2010-03-03 04:47 PM

Re: Resource temporarily unavailable Error In Socket

bytesSent = send( sockfd, "\033E 1\r",sizeof("\033E 1\r"), 0);

Do you really want to use sizeof() there? That will result in you sending the null
character, as well. Is that maybe freaking the printer out, because it doesn’t expect
to see embedded nulls in its data stream? What happens if you use strlen() instead?

#10 2010-03-04 05:19 AM

Re: Resource temporarily unavailable Error In Socket

Here i used strlen() even though i am getting same error.In My case error less condition if i am sending «\033E 1\r» it returns the Device Model and make.But if again i am sending the printer shows command error.

In this case i cant able to receive the error code to that error from printer.

#11 2010-03-04 02:11 PM

Re: Resource temporarily unavailable Error In Socket

*shrug* Sounds to me like a problem with the printer, not your code, then. But,
we can’t really be sure without seeing all the real code in actual use. (Though, even
then, we might not be able to say much about it without access to the same type of
printer you’re talking to, either. Unless there’s some obvious and simple bug,
anyway. )

Источник

will tcp socket recv return «resource temporarily unavailable» when recv buf already have data

I implement TCP socket communication using epoll to monitor all the client events, only one thread process all the client in a for loop. every socket is non-blocking. now I just suffered a problem, when client send data more than MTU, means several fragment packet, server always can’t read all the data completely. like below, I read the head first, get the pdu len from the head, then read the pdu part. the problem is though I read the head successfully, closely following with the pdu recv(), but it always return EAGAIN several times. so my retry will break. Because server needs to process thousands of client event, so I think it is a great performance consumptionn to let the retry always continue which is not tolerable. I use tcpdump capturing the packets from client, every packet is fragment to max 1448 bytes data, but head is only 5 bytes, why I can read the head successfully, but the following data recv() operation will return EAGAIN? is it possible recv return EAGAIN when data already arrive the recv buffer? In my opinion, I can read the first 5 bytes, so must have more data to read in the recv buffer. maybe related with the assemble process in tcp/ip stack. code is like below, every pdu recv, need 10 or more retry, maybe success.

. #define HDR_LEN 5 n = epoll(epfd, events, 1000, -1) for(i =0; i < n; i++) < uint8 pHdr[HDR_LEN] = ; uint16 pdulen = 0, offset =0; infd = events[i].fd; nRead = recv(infd, pHdr, HDR_LEN); // read the data head first pdulen = ntohs(*(uint16 *)(pHdr+2)); // get the pdu len from the head uint8 *pbuf = malloc(pdulen+HDR_LEN); memcpy(pbuf, pHdr, HDR_LEN); // move the head to buf while(offset != pdulen) // then read the pdu data < nRead = recv(infd, pbuf+HDR_LEN+offset, pdulen-offset); if (nRead <=0) < if (nRead == -1 && errno == EAGAIN) // resource temporarily unavailable < if (retry < 5) < usleep(500); retry++; continue; >else break; // already try 5 times, should always continue? > else break; > else < offset += nRead; retry = 0; >> if (offset == pdulen) process(pbuf, pdulen+HDR_LEN); // process the complete data . > . 

Is your socket in non-blocking mode? If so, then EAGAIN is to be expected whenever there is no more data left in the receive buffer to retrieve. In order to handle non-blocking I/O of multiple sockets simultaneously, you’ll need to implement a state machine so that you can go back your normal poll() call after having received a partial response. Then when poll() indicates there is more data to read on your socket, then (and only then) should you go back and recv() the additional data from the socket, appending it to the data you got from that socket previously, until you’ve got the whole PDU.

Читайте также:  Astra linux обновление системы через интернет

‘Will tcp socket recv return “resource temporarily unavailable” when recv buf already have data?’ No.

Источник

What can cause a “Resource temporarily unavailable” on sock send() command

What can cause a Resource temporarily unavailable error on a socket send() command? The socket is setup as AF_UNIX, SOCK_STREAM . It works most of the time, but occasionally gets this error. The receiving end of the socket appears to be working properly. I know this isn’t very detailed, but I’m just looking for general ideas. Thanks!

I don’t think it is related to that post. My sockets are SOCK_STREAM which I believe are blocking which is what I want.

Whether a stream is blocking or non-blocking is independent of whether it’s SOCK_STREAM or SOCK_DGRAM. The answer there is relevant.

4 Answers 4

«Resource temporarily unavailable» is the error message corresponding to EAGAIN , which means that the operation would have blocked but nonblocking operation was requested. For send() , that could be due to any of:

  • explicitly marking the file descriptor as nonblocking with fcntl() ; or
  • passing the MSG_DONTWAIT flag to send() ; or
  • setting a send timeout with the SO_SNDTIMEO socket option.

@caf, In my case, different MTU size configuration in two side was causing sctp association Txqueue to be overflown when high rate of packet exchange was happening. Making the MTU same on both system made the problem go away. But can anyone please explain what is the reason behind the problem?

@GabrielFernandez: Because this is what SO_SNDTIMEO requests: that the send not block for longer than the timeout period. If the send hasn’t completed and the timeout expires, it returns EAGAIN to indicate this condition. How you handle that is up to your application, bearing in mind that the data you tried to send has not been sent.

Читайте также:  Команда создание пользователя на linux

That’s because you’re using a non-blocking socket and the output buffer is full.

 When the message does not fit into the send buffer of the socket, send() normally blocks, unless the socket has been placed in non-block- ing I/O mode. In non-blocking mode it would return EAGAIN in this case. 

EAGAIN is the error code tied to «Resource temporarily unavailable»

Consider using select() to get a better control of this behaviours

  1. client connect to server, and send 1MB data to server every 1 second.
  2. server side accept a connection, and then sleep 20 second, without recv msg from client.So the tcp send buffer in the client side will be full.

Code in client side:

#include #include #include #include #include #include #include #define exit_if(r, . ) \ if (r) < \ printf(__VA_ARGS__); \ printf("%s:%d error no: %d error msg %s\n", __FILE__, __LINE__, errno, strerror(errno)); \ exit(1); \ >void setNonBlock(int fd) < int flags = fcntl(fd, F_GETFL, 0); exit_if(flags < 0, "fcntl failed"); int r = fcntl(fd, F_SETFL, flags | O_NONBLOCK); exit_if(r < 0, "fcntl failed"); >void test_full_sock_buf_1() < short port = 8000; struct sockaddr_in addr; memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; int fd = socket(AF_INET, SOCK_STREAM, 0); exit_if(fd> int main()

Code in server side:

 #include #include #include #include #include #include #include #define exit_if(r, . ) \ if (r) < \ printf(__VA_ARGS__); \ printf("%s:%d error no: %d error msg %s\n", __FILE__, __LINE__, errno, strerror(errno)); \ exit(1); \ >void test_full_sock_buf_1()

Start server side, then start client side.

server side may output:

accept a connection from 127.0.0.1:35764 but now I will sleep 15 second, then exit Process finished with exit code 0 

enter image description here

client side may output:

connect to server successsend: 1024000, erron: 0, Success send: 1024000, erron: 0, Success send: 1024000, erron: 0, Success send: 552190, erron: 0, Success send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 11, Resource temporarily unavailable send: -1, erron: 104, Connection reset by peer send: -1, erron: 32, Broken pipe send: -1, erron: 32, Broken pipe send: -1, erron: 32, Broken pipe send: -1, erron: 32, Broken pipe send: -1, erron: 32, Broken pipe 

enter image description here

You can see, as the server side doesn’t recv the data from client, so when the client side tcp buffer get full, but you still send data, so you may get Resource temporarily unavailable error.

Источник

Оцените статью
Adblock
detector