Operating System - HP-UX
1834130 Members
2934 Online
110064 Solutions
New Discussion

Bind one port by 2 programs ( Error : Port Already In Use )

 
SOLVED
Go to solution
Rahul_19
Advisor

Bind one port by 2 programs ( Error : Port Already In Use )

F1... F1... F1...
Hi All ,

I want to bind the port which is already in use by another process.( without making the changes in the first processs source code )

Rahul.


Be as radical as reality.
17 REPLIES 17
Fred Ruffet
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

You can't.

Only one server process can listen on a port at the same time.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Sanjay Kumar Suri
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

One port can be assigned to one service only. Look at /etc/services file.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Mark Grant
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Fred just stole my "You can't" post :)

The usual way of dealing with the problem is for an application to get a connection from the client, negotiate a new port to communicate over and fork.
Never preceed any demonstration with anything more predictive than "watch this"
KapilRaj
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

hv'nt u already raised a thread for the same ?

Kaps
Nothing is impossible
Rahul_19
Advisor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Ok Fred / Sanjay do you have any Alternative on this , because I what to monitor Port to check whether there is any data has arrived or not , and I donâ t have the source code for The first process .

Regards,
Rah
Be as radical as reality.
KapilRaj
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Use lsof to monitor that port ..

use lsof to find out the process which is using that port , I do not think port numbers will be stored in a source-code , it should be in a config file ..

Kaps
Nothing is impossible
Fred Ruffet
Honored Contributor
Solution

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Mark,

It was real fun :)
Server/Agent process does not free the port, in general case, cause when Server and Client negociate the port, then Server forks to an agent, and come back listening on this port... So it is not free.

Rahul,

What you need is a snifer program. The easiests (and also free) will be tcpdump and ethereal. I use them on Linux. I don't know for ethereal, but I've seen on this forum that tcpdump can be compiled on HP. Nevertheless, you can use another machine on the network to snif with this programs... They can show you traffic for specific port on specific IP, and a lot much.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Rahul_19
Advisor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

AlrFred,

Alright Fred , Guys
Since I can not bind the port which is used by the another process then I came up with another solution check the following script , but it gives me the Interface error ( we can put this in the demon (Fork ) ) and accomplish the task please give your suggestion ASAP

#!/bin/csh

set PORT = $1
if ($PORT == "") then echo -n "port> " set PORT = $< endif

set IP=`/usr/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | \ awk '{print $2}'`

foreach STATE ( ESTABLISHED LAST_ACK CLOSING TIME_WAIT CLOSE_WAIT LISTEN IDLE ) set CNT=`netstat -an | grep $IP.$PORT | grep $STATE | wc -l` echo $STATE ":" $CNT end


Regards
Rahul


Be as radical as reality.
Fred Ruffet
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Rahul,

I would like to clarify a point :
Do your want to know if there is traffic on a port, or do you want to know if someone is listening on a port ?

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Rahul_19
Advisor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Fred,
Hear is the Situation

What I Have:

One program using the port say 20000
(For Which I donâ t have source code but I know the port number it is listening to )


What I want:

I want to send the signal to another process to do some other job as soon as some data is arrived on the port no 20000.

Now how do we do this????

Regards,
Rahul
Be as radical as reality.
Fred Ruffet
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

The easiest way would be to trace log for application, but I imagine you don't have one...

Now what you can do is to install lsof or tcpdump, and make them monitor that port. As soon as they tell you so, you can launch your work.

You should have a look at both lsof and tcpdump, as long as they do not act the same (tcpdump monitors network traffic, lsof monitors files, but as long as sockets are files...)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
rick jones
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

you seem to have posted the same question twice...

see the other thread about LD_PRELOAD

as for tcpdump, if you are scripting, you would use tcpdump. ifyou are programming, you would use libpcap (which is used by tcpdump). keep in mind that the overhead of promiscuous mode is non-trivial so using tcpdump will consume a fair amount of CPU time - particularly when the network traffic rates are high. if the rates are rather low (rates into an interface, not just the one application/port you wish to monitor) then it may not matter.

an indirect way to go would be to parse the output of ndd /dev/tcp tcp_status and notice when the receive sequence numbers change. that means that data has arrived at TCP for the given connection, but does not necessarily mean it has been read by the application.
there is no rest for the wicked yet the virtuous have no pillows
Bharat Katkar
Honored Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Hi rahul,
You check whether that port is listening or requesting clinets request by command:

# netstat -a
Use "cut" to capture the appropriate field.
see man netstat,cut

Don't know how much this will help.
Regards,
You need to know a lot to actually know how little you know
Rahul_19
Advisor

Re: Bind one port by 2 programs ( Error : Port Already In Use )


Fred Ruffet :
Rick jones :
Kapil :
Bharat Katkar :

Ok..........

Please Check the following site http://www.tcptrace.org/
Please find and download the file tcptrace-6.6.1.tar on net it has most of the source code for
Tcpdump and tcptrace , I am trying to dig out most of it. It might be useful for all the UNIX guys. Or You may download the Tcptrace from the same site : http://www.tcptrace.org/

GOOD LUCK......... Guys

Regards
Rahul





Be as radical as reality.
James Lynch
Valued Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

Rahul,

If you have access to the client process, and the client can be configured to connect to a different port, say 20001 instead of 20000, then you could write a server side proxy program. The proxy program would listen for any connections on port 20001 coming from the client. The proxy would would also open a connection via the loopback interface to the "real" server program on port 20000. Whenever the proxy receives data from the client it would pass it directly to the "real" server and send a signal to your other process. The proxy would also have to pass data from the "real" server back to the client.

Using this method you don't have to parse any logfiles, you don't have to worry about the overhead of promiscusous mode, and you should never miss a single byte of data that gets passed to the server.

-JL
Wild turkey surprise? I love wild turkey surprise!
Todd Whitcher
Esteemed Contributor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

From the sound of it you may want to do a network trace as well as a system call trace of the application.

Just and FYI you can get a "swinstallable" version of ethereal and tcpdump off of HP's Internet Express Media:

Its available for 11.11 11.23

http://www.software.hp.com/portal/swdepot/displayProductInfo.do?productNumber=HPUXIEXP1111

You could use tusc also to see what the source code is doing by setting it up to follow the listener PID. Use the -f option to follow forks.

Something like this:

tusc -aeflpT "" -o tusc.out PID_OF_APP


http://gatekeep.cs.utah.edu/hppd/hpux/Sysadmin/tusc-7.5/

Hope this helps,

Todd
Rahul_19
Advisor

Re: Bind one port by 2 programs ( Error : Port Already In Use )

James Lynch / Todd / ALL

Thanks guys, I am trying to do all the things possible to accomplish the job
All guys have added the new different avenues to the exercise to find the
solutions for the above said problem.

Thanks and Regards

Rahul

Be as radical as reality.