Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3695100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:34:36+00:00 2026-05-19T04:34:36+00:00

I am trying to write a simple bash script that is listening on a

  • 0

I am trying to write a simple bash script that is listening on a port and responding with a trivial HTTP response. My specific issue is that I am not sure if the port is available and in case of bind failure I fall back to next port until bind succeeds.

So far to me the easiest way to achieve this was something like:

for (( i=$PORT_BASE; i < $(($PORT_BASE+$PORT_RANGE)); i++ ))
do
  if [ $DEBUG -eq 1 ] ; then
    echo trying to bind on $i
  fi
  /usr/bin/faucet $i --out --daemon echo test 2>/dev/null
  if [ $? -eq 0 ] ; then                        #success?
    port=$i
    if [ $DEBUG -eq 1 ] ; then
      echo "bound on port $port"
    fi
    break
  fi
done

Here I am using faucet from netpipes Ubuntu package.

The problem with this is that if I simply print “test” to the output, curl complains about non-standard HTTP response (error code 18). That’s fair enough as I don’t print HTTP-compatible response.

If I replace echo test with echo -ne "HTTP/1.0 200 OK\r\n\r\ntest", curl still complains:

user@server:$ faucet 10020 --out --daemon echo -ne "HTTP/1.0 200 OK\r\n\r\ntest"
...
user@client:$ curl ip.of.the.server:10020
curl: (56) Failure when receiving data from the peer

I think the problem lies in how faucet is printing the response and handling the connection. For example if I do the server side in netcat, curl works fine:

user@server:$ echo -ne "HTTP/1.0 200 OK\r\n\r\ntest\r\n" | nc -l 10020
...
user@client:$ curl ip.of.the.server:10020
test
user@client:$

I would be more than happy to replace faucet with netcat in my main script, but the problem is that I want to spawn independent server process to be able to run client from the same base shell. faucet has a very handy --daemon parameter as it forks to background and I can use $? (exit status code) to check if bind succeeded. If I was to use netcat for a similar purpose, I would have to fork it using & and $? would not work.

Does anybody know why faucet isn’t responding correctly in this particular case and/or can suggest a solution to this problem. I am not married neither to faucet nor netcat but would like the solution to be implemented using bash or it’s utilities (as opposed to write something in yet another scripting language, such as Perl or Python).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:34:37+00:00Added an answer on May 19, 2026 at 4:34 am
    faucet 10020 --out --daemon \
        echo -ne "HTTP/1.0 200 OK\r\nContent-Length: 4\r\n\r\ntest"
    

    works fine. The issue seems to be that echo doesn’t know how to properly shutdown a socket, using just close instead, and curl is unhappy about getting a -1 (disorderly shutdown) rather than a 0 (orderly shutdown) from recvfrom.

    Try socat which sticks around to clean up after the child is done.

    socat tcp-l:10020,fork,reuseaddr \
        exec:'echo -ne "HTTP/1.0 200 OK\r\n\r\ntest"'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple bash script that will copy the entire
I am trying to write a simple bash script that will search for files
I'm trying to write a simple bash script that accepts all arguments and interprets
I'm trying to write (what I thought would be) a simple bash script that
I'm trying to write a Bash script that will SSH into a machine and
I'm trying to write a simple script that will list the contents found in
I'm trying to write a simple bash script but something seems wrong, I'm testing
I'm trying to write a simple WPF app that has two ellipses, joined by
I am trying to write a really simple Outlook VSTO add in that checks
I'm trying to think of clever, clear, and simple ways to write code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.