0

Say I have a Node.js tcp server:

const map = new Map();

const s = net.createServer(function(socket){

     socket.pipe(jsonParser()).on('data', function(v){
        v && v.pid && map.set(v.pid, socket);
     }):
});

I connect to it using Bash like so:

exec 3<>/dev/tcp/localhost/9091  # persistent file descriptor

exit_code=$?

if [[ ${exit_code} -ne 0 ]]; then
   echo "could not connect to daemon";
   exit;
fi

echo "{\"pid\":${BASH_PID},\"args\":[${ARGS}],\"cwd\":\"$(pwd)\"}"  >&3

my question is - do I have to wait for the first message from the socket to know more information about the socket? Or is there some way to get the information upon first connection, without having to listen to data being written to/from the socket?

4
  • "information about the socket"? What is that? What information are you trying to get? Commented Apr 20, 2018 at 19:05
  • the information that's in the bash echo statement, the pid that it's connecting from, pwd, etc. Commented Apr 20, 2018 at 19:05
  • @jfriend00 the nature of the original question is as follows: is the fastest way to register information with the server, is for the client to do: 1. try to make connection. 2. receive a message from the server that you are connected. 3. Then send the server a message with some data (such as username/password). Commented May 9, 2018 at 21:11
  • My question is simply if there is a faster way (single request) to "register/authenticate" with the server, something faster than steps 1,2,3 above. Commented May 9, 2018 at 21:12

1 Answer 1

1

It appears your question is this: "What is the fastest way to register/authenticate with the TCP server if you're making up your own protocol"?

  1. Establish TCP connection
  2. Send authentication credentials
  3. Receive response that indicates success or failure to authenticate

You do not have to wait for the server to send you something back after establishing the TCP connection (that's already been done internally at the TCP level - you don't have to have an application-level message after connection).

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I guess you can't include the creds with the connection request? (The first request).
@AlexanderMills - With TCP you have to establish the transport before you can send application level data.
@AlexanderMills - You could use UDP instead of TCP for your client and server and basically skip step 1. Of course, you accept some other compromises with UDP (such as delivery reliability, packet ordering, etc...).
thanks since you know about this, do you have thoughts on this related question? stackoverflow.com/questions/50262882/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.