Ben Cantrick (mackys) wrote,
Ben Cantrick

  • Mood:
  • Music:

Help me I am in try / catch hell.

Oh sure, I thought it would be a good idea to use Java to do some port forwarding code we need to extract a video stream off a webserver that's behind a NAT. "It'll be much faster than doing it in C" I said to myself, "and porting should be a snap!" I should have known better.

Let me put it this way: I think I've pretty much had to wrap every single @#$% call to read(), write() and Socket(...) with a try/catch block - some of them with multiple catches. Even socket.setSoTimeout() requires a try/catch block, for fuck's sake! I never had to check the return value from ioctl() in C.

Another thing that annoys me is that byte[]s are the best data structure for low level socket work, but there doesn't seem to be a way to "reset" one to zero length. I don't want to be new'ing 1k arrays constantly, but I have yet to find a good way to express "this array's contents have been transmistted, so feel free to over-write it." I ended up having to declare and maintain a separate int just to hold the number of bytes in the byte[]. Cuz, you know, letting me do byte[].length = 0 or byte[].empty() would have just been way too easy. Rargh!! Stuff like this makes MFC's CString class look well-designed in comparison! And don't get me started on the atrocious travesties that every Java sockets tutorial I've come across has been. I don't think the people who wrote most of those even tried to compile their own code, much less test what happens when a connection actually hangs up...

Well, jigenm4c just gave me some good tips, so maybe select() and will save my bacon. All suggestions are appreciated at this point...
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.