Results 1 to 6 of 6

Thread: Socket Disconnection, SIGPIPE and ThreadKill

  1. #1
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45

    Socket Disconnection, SIGPIPE and ThreadKill

    As I recently started writing a new server program from scratch with the Sockets unit, I have joined the barren realm of network programming. Seriously, there are only 66 threads in this forum vs hundreds elsewhere.

    My problem is as follows:
    I have a socket open between many clients and the server, exchanging data very happily. However, if I close just one of the clients, I get a runtime error #141 - I have try,except block in where the data is transmitted and that calls no error, and this behaviour occurs even when the server is waiting to start the next cycle in a delay() loop. Occasionally I get error 111 on the client side if the server is closed.

    I have tried to use the SocketError() procedure, and have it running in a parallel thread from the program's startup checking for error every 2ms, however it picks nothing up when the runtime error occurs.

    After googling I found it has something to do with the TCP pipe being broken and SIGPIPE killing my program as it thinks that due tot he broken pipe, it cannot continue.

    And no, I don't understand any of that last bit...

    Any help would be greatly appreciated.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,246
    Blog Entries
    2
    Hi code_glitch,

    I can't offer any advice about using the sockets library as the only server I've ever written was produced using Kylix 3 and Indy 9. Indy 10 does I believe support Freepascal without lots of baggage. The server I produced ended up being a Linux deamon, could handle 800 requests/second and never died when the client disconnected.

    I guess what I'm trying to say is that if you can use a slightly higher level library like Indy, alot of this excess baggage would be taken care of for you.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    a Linux deamon, could handle 800 requests/second
    Thats some performance indeed... Though the reason I chose Sockets was not primarily because it fits in virtually zero resources but because it comes as part of the default pascal units, does not require any 3rd party code and was quite flexible to write everything from the ground up.

    However, it does amaze me that there is such little knowledge on SIGPIPE - I mean, its the core of linux and modern computing... Now that everything works so well and its just this problem causing hitches and snags - I'd hate to drop all that code because of some SIGPIPE error if you know what I'm getting at. So far, everything fits in a 600kb executeable and 1,003kb of ram needing a whopping 1.6mhz of cpu power to handle 25 simultaneous clients of I/O. So far, port redirection is working, as is a basic TX & RX protocol - authentication is on the way and the bones of a permission system is already present. Not that I need it to use so little resources, its just a feature - one that I can hopefully migrate into future projects: I'm looking into those tiny embedded boards that pack enough punch for windows xp (just) but are a few mm tall, and a few inches square with <1w power consumption. Fits in a small box, and when booted with linux can run a very bare bones version of X and OpenGLES with a custom UI could be so sweet

    The only thing I could dig up was: http://web.archiveorange.com/archive...4e7vvncSxNbiG1 And I can't seem to make any sense of it
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,246
    Blog Entries
    2
    Ok, you got me thinking... take a look at this page from the very first article I wrote for PGD. It talks about handling some signals within Linux. These are part of my server daemon.

    You may be able to add a handler for SISPIPE and prevent it from killing the application.

    Just a thought.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    That does look like it could make some headway actually, I'd never thought of looking there to be honest. The niceness of pascal: if you hadn't told me it was written in kylix I'd never have known

    I'll have a quick read tonight but I doubt I'll get any of it written into the server without me falling asleep so that'll have to wait until tomorrow i guess.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  6. #6
    Quote Originally Posted by code_glitch View Post
    That does look like it could make some headway actually, I'd never thought of looking there to be honest. The niceness of pascal: if you hadn't told me it was written in kylix I'd never have known
    Well, the very first line uses unit "libc", which is a dead giveaway for Kylix. Unit libc is declared legacy, and will never be fully supported by FPC (libc unit deprecation )

    I'll have a quick read tonight but I doubt I'll get any of it written into the server without me falling asleep so that'll have to wait until tomorrow i guess.
    Don't do it manually. Try to use the FPC libraries for this (fcl-extra, daemonapp ) as possible. This delivers more chance that it will work between OSes and architectures (unit libc is not available for Mac, FreeBSD and Linux other than i386)

    As far as the original problem goes, try to determine with strace what call exactly goes wrong. Are you reading from a socket in a different thread while closing it in another? Do you fpshutdown sockets properly?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •