How can I keep netcat connection open?

I have two files, client.sh and server.sh. All the necessary data is on the server, which is sent to the client using netcat. The client just get these data and display it to the end user. The problem is, when I try to show the dialog loading screen from the server to the client:

server.sh

# CLIENT PORT: 8765
# SERVER PORT: 5678

while :
do
    touch registered_users data

    nc -vv -l -p 5678 > data

    case `cat data` in
        "SPLASH_SCREEN")
            for ((i=0;i<100;i++))
            do
                echo $i
            done | dialog --title 'Loading...' --gauge 'Welcome!' 8 40 0 > /dev/tcp/127.0.0.1/8765
        ;;
    esac
done

client.sh

# CLIENT PORT: 8765
# SERVER PORT: 5678

echo "SPLASH_SCREEN" > /dev/tcp/127.0.0.1/5678

while :
do
    nc -l -p 8765 > server_response
    cat server_response
done

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Solved it! just had to use the -k option

 -k    Forces nc to stay listening for another connection after its current
       connection is completed.  It is an error to use this option without the
       -l option.

EDIT: This answer assumes you’re using openbsd-netcat, some versions like gnu-netcat have a reduced set of features, hence some flags like -k might not be present, package names might vary according to your distro


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x