开发者

Run command when user connects?

开发者 https://www.devze.com 2023-04-11 21:20 出处:网络
Is it possible to listen on a port and run a command when a user attempts to connect to that port? Ideal application is for a server that should only be run when someone is actually using it. Windows

Is it possible to listen on a port and run a command when a user attempts to connect to that port? Ideal application is for a server that should only be run when someone is actually using it. Windows or Linux solutions work开发者_如何学C.


linux/unix:

man nc

NAME
       nc - TCP/IP swiss army knife

some options that you may be interested

 -l           listen mode, for inbound connects
-p port      local port number (port numbers can be individual or ranges: lo-hi [inclusive])
 -e prog      specify program to exec after connect (use with caution)

i think nc is also available under windows platform.


One solution could be via inetd or xinetd, specify the port number and a program to run, for you probably a shell script.


I am note sure what is the exact scope of a question but if bound to the programming-level, you could write your server in a way that when nobody is using it, no resources apart from the listening part are allocated. I would call it lazy initialization. When someone connects simply initialize the whole logic of your program. When all connections are gone, deinitialize everything.

0

精彩评论

暂无评论...
验证码 换一张
取 消