Writing to a socket and handling broken pipes

I have some code in PHP that connects to a socket. I've been getting a broken pipe intermittently while writing to it. The problems seems to go away if write to the pipe again. I'm wondering what's required (the safest way) to recover from it. I'm also wondering whether socket_write may return without writing the full string that was passed into it. Here's what I have currently.

function getSocket() { $socket = socket_create( AF_UNIX, SOCK_STREAM, 0 ); if ( $socket === FALSE ) { throw new Exception( "socket_create failed: reason: " . socket_strerror( socket_last_error() )); } } $result = socket_connect($socket, $address); if ($result === false) { throw new Exception("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket))); } return $socket; } function writeSocket($stmt) { $tries = 0; $socket = getSocket(); do { // Is is possible that socket_write may not write the full $stmt? // Do I need to keep rewriting until it's finished? $writeResult = socket_write( $socket, $stmt, strlen( $stmt ) ); if ($writeResult === FALSE) { // Got a broken pipe, What's the best way to re-establish and // try to write again, do I need to call socket_shutdown? socket_close($socket); $socket = getSocket(); } $tries++; } while ( $tries < MAX_SOCKET_TRIES && $writeResult === FALSE); }

-------------Problems Reply------------

Q1. I'm wondering what's required (the safest way) to recover from it.

A: That depends on the application. The socket listener is closing the connection, or you've set socket options that you're not showing us. How you deal with either of these things depends on the semantics of the application.

Q2. I'm also wondering whether socket_write may return without writing the full string that was passed into it.

A: Yes. socket_write() can write no bytes, some of the bytes, or all of the bytes before returning. If it returns a value greater than zero but less than the length of the input, you should adjust offsets (perhaps using substr()). If it returns zero or less, inspect socket_last_error() for clues as to whether it is retry-able. This contortion is covered in the manual.

Have you tried setting SO_KEEPALIVE or SO_SNDTIMEO? You might also test in your loop for the buffer length to see if the whole thing has been sent.

Good luck and HTH, -- Joe

Use socket_set_nonblock() and fix your while statement:

$writeResult === FALSE should be $writeResult !== FALSE instead.

Category:php Views:6 Time:2011-08-26

Related post

  • How can I reconnect a socket after a broken pipe? 2011-09-20

    The program connects to a server, and when the connection is closed by the server, if I try to reconnect it says: socket.error: [Errno 9] Bad file descriptor If I close the socket in the client and then i try to reconnect, it says: socket.error: [Err

  • How to handle a broken pipe (SIGPIPE) in python? 2008-10-07

    I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when t

  • Python: unix socket -> broken pipe 2010-04-26

    I'm trying to get Python socket working as an alternative to calling the command line socat. This socat command works fine: echo 'cmd' | sudo socat stdio <path-to-socket> but when I run this python code, I get an error: >>> import sock

  • Broken Pipe exception when tried to stream mp3 stream from local server to android mediaplayer 2010-10-27

    I am trying to stream mp3 stream from my local http server indeed hosted on my phone to android media player.When local server gets the new socket , it starts writing some http headers followed by mp3 stream. but mediaplayer socket is throwing "Broke

  • Python socket.send() can only send once, then socket.error: [Errno 32] Broken pipe occurred 2011-06-04

    I'm a newbie in network programming, so please forgive me if this is a dumb question :) I created 1 client and 1 SocketServer.ThreadingMixIn server on Ubuntu 10.04.2 using Python2.7, but it seems like I can only call sock.send() once in client, then

  • Java, Getting a weird Socket Exception: "Broken pipe" and "Operation timed out" 2011-11-11

    I have a client server application that has 16 threads (8 client, 8 server) and each has a TCP connection in a 1-1 matching (so 8 TCP streams). I have the server sending X amount of random bytes then close. Meanwhile the client just reads X amount of

  • What is the reason for Broken Pipe on Unix Domain Sockets? 2012-04-15

    I have a server application which received requests and forwards them on a Unix Domain Socket. This works perfectly under reasonable usage but when I am doing some load tests with a few thousand requests I am getting a Broken Pipe error. I am using J

  • Broken Pipe Java program writing in an external C programs stdin and reading from stdout 2011-05-31

    I am using the follwing java code to write in the stdin of a C program which basically reads from stdin adds A_A_A to the end of the line and writes it back in the stdout which the java program reaads from and throws the stdout BufferedWriter b_stdou

  • Socket Send error as Broken Pipe 2009-12-10

    From A TCP client when i call the send to server. It is giving Broken Pipe error. --------------Solutions------------- A broken pipe occurs whenever you try to write data to a pipe but all of the readable ends of the pipe have closed. In your case, i

  • Send behavior on Broken pipe 2010-07-05

    While writing a simple server-client application, this question came in my mind. When someone tries to write to a broken pipe, a SIGPIPE would be generated. Let's say I handle the signal in my code. Now what error does the write call returns - EPIPE

  • Python BaseHTTPServer, how do I catch/trap "broken pipe" errors? 2011-05-19

    I build a short url translator engine in Python, and I'm seeing a TON of "broken pipe" errors, and I'm curious how to trap it best when using the BaseHTTPServer classes. This isn't the entire code, but gives you an idea of what I'm doing so far: from

  • SIGPIPE, Broken pipe 2011-07-26

    I am working on a networking program using epoll on linux machine and I got the error message from gdb. Program received signal SIGPIPE, Broken pipe. [Switching to Thread 0x7ffff609a700 (LWP 19788)] 0x00007ffff7bcdb2d in write () from /lib/libpthread

  • Is there a cross platform way of handling named pipes in Java or should I write my own? 2009-11-03

    I'm writing a bit of JNI code where a DLL running in the process space of various processes on the system needs to talk back to a java process. I've decided to use named pipes to do this (for various reasons) after weighing up shared mem/sockets/rpc

  • What does the exception "javax.servlet.jsp.JspException: Broken pipe" signify? 2010-05-05

    I'm getting the following error: javax.servlet.jsp.JspException: Broken pipe Now I have seen questions/answers with respects to the socket exception, but this error is coming from a different package. Any help is greatly appreciated. BTW, I am seeing

  • Django + WebKit = Broken pipe 2010-05-19

    I'm running the Django 1.2 development server and I get these Broken Pipe error messages whenever I load a page from it with Chrome or Safari. My co-worker is getting the error as well when he loads a page from his dev server. We don't have these err

  • PHP's fwrite stops writing to a socket file pointer 2010-07-22

    I have the following PHP code for writing to a filepointer $fp that was opened using fsockopen: syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:"); $bytes = 0; while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $b

  • Broken Pipe when Using Python Multiprocessing Managers (BaseManager/SyncManager) to Share Queue with Remote Machines 2010-09-06

    In the last month, we've had a persistent problem with the Python 2.6.x multiprocessing package when we've tried to use it to share a queue among several different (linux) computers. I've posed this question directly to Jesse Noller as well since we

  • What is the difference between java.net.SocketException: Connection reset and java.net.SocketException: Broken Pipe? 2011-01-10

    What is the difference between java.net.SocketException: Connection reset and java.net.SocketException: Broken Pipe? I am trying to figure what are the reasons for these two exceptions. We are getting following error on our server, which is basically

  • Google App Engine & jQuery Ajax causes a Broken Pipe error 2011-02-02

    I have a pretty standard case, where I try to submit some JSON data via jQuery's Ajax. My Java Script code looks like this: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.139 (s). 11 q(s)