Java Connections netstat -ano - java

I am new to java and I have been testing my application all day long.
I just did
netstat -ano
and it gave me a huge listing of active connections (listening, established) does this mean when i close my appliation these connections are not being shutdown (close())?
here is a screenshot:
alt text http://img340.imageshack.us/img340/9950/netor.jpg
any advise on how to go about closing the connection when im done with it? i am trying to close the connection best to my knowledge but it appears im not doing enough.
thanks for your time.
EDIT: tcpview is great. yes those connections were earlier on the day when the code was not complete. now when i run it, it gets registered in tcpview and vanishes when i close the connection.

netstat is only of partial use for this sort of thing. Your output doesn't show which connections and ports are used by which process.
Download the tcpview tool from microsoft, that'll show you clearly which process is using which ports, and which is maintaining open connections.

Related

How to reconnect jconsole on connection lost

I am doing load test for my project, when i tried to use jconsole to monitor when the server is restarted connection is getting lost is there any solution for this?
Thanks
jconsole connects to a process, when the server is restarted it gets a new process id, and jconsole did not know of the new process that is created. There is no other but you have to connect it yourself.
Jconsole is connected to a process (jvm). When your server is stop, the process doesn't exist anymore and so the jconsole connection is lost. And so you have to connect jconsole to the new process created when you server is starting.
is there any solution for this?
One way to ease the pain is to use a JMX URL instead of a process ID. The JMX URL never changes on restart so while you have to reconnect anyway, at least the process is less painful.
URLs are of the format service:jmx:rmi:///jndi/rmi://hostName:portNum/jmxrmi. Not sure what your server is, but here's how to enable it on tomcat.

How can I determine if another local machine is alive?

Is it possible to make my local computer function as a gateway in Java? I need the other local machines to connect directly to my computer to see if they are alive or not.
You could run a Java server program on your desired PC and let it listen on a port. Then you could use other programs (browser, other Java programs etc.) to connect to this port, and send commands to be executed by the Java server program.
If you just want to see if the PC is turned on or not, I'd just use the ping command though. Or see this answer: How to do a true Java ping from Windows?
Surely it's the other way round? Surely you want to connect to the other machines to see if they're alive? In which case see InetAddress.isReachable().
Try this.
Create a Java Server Socket, which keeps listening to the client at some port.
Write a client in Java which connects to the Server, wrap the connection logic in try-catch block....
If your host is alive the try code is executed which contains the code to connect to the
Server, if this connection process fails you will get UnknownHostException, here you can instead type a message that the connection failed.
You could more easily manage and control this by polling for other devices from a central server. If possible, avoid unnecessary client/agent apps that might tax your development and support resources as well as taking up RAM on the client workstations.
There are many monitoring tools that already do what you want. I'd have a look at Nagios, for example.
If you want to develop your own app, do your own quick troubleshooting, or just get a feel for network discovery tools, then take a look at NMAP. You could, for example, search a subnet for anything that responds to TCP:445 and see what Windows machines are alive.
If you do go the Nmap route, please have a look at Nmap4j on Sourceforge. It's a Java wrapper API that simplifies the work needed to integrate Java and Nmap.
Cheers!

how to kill a hang connection in jvm when you are outside of a jvm (ubuntu box)?

I have an application runs in jboss 4.2.2 server with jdk 1.6. The program has bug in it that it doesn't set http connection timeout when it opens its the connection. So when the third party side has issues the connection is hanged forever which makes the thread hangs as well. And soon we are running out of thread. However, due to the release cycle we can't put a fix in immediately. I was wondering there is a way to terminate a network connection from outside the jvm? So the thread can be release back to the thread pool? I potentially has a lot of connection open to the same third party site so it is nice to figure out the problem connection and just kill that one.
Thanks,
While searching for a question of my own, I came across what seems to be a great tutorial on how to externally kill a thread.
http://www.rhcedan.com/2010/06/22/killing-a-java-thread/
You can grep the output of netstat and kill the connection using tcpkill, and run this using cron.
However this cannot be more than a very temporary solution.
This ServerFault Q & A may be relevant. It explains that tcpkill will only work if there is active traffic on the connection.
(This is because ... apparently ... tcpkill works by sending a TCP RESET packet. In order for this to work it needs to know the correct sequence number, and it can only figure this out by examining other packets for the session.)

HTTP socket closed on Linux, then unable to open it again

We have an issue on one of customers servers, where something seems to close the java application HTTP socket, and not let it open afterwards for some time.
Meaning it goes like this:
1) Application works fine, then something causing the socket to close.
2) Any subsequent attempts to open it, including application restart will produce the "java.net.BindException: Address already in use" for some time.
3) Then it would finally let open the socket via another application restart.
It's the first time we see such issue happening, and quite stumbled by it.
Does it rings a bell for anyone?
OS: 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64, CentOS release 5.5
Java version: 1.6.0_20
Thanks!
Seems like you should tell your Linux to create socket with immediate rebinding allowed, see SO_REUSEADDR in man 7 socket.
Sounds like you should be investigating the 'something causing the socket to close' part.
As for what's closing the socket, you'll have to investigate your code. It's not something external that closes the socket, it can only be your code.
The behaviour you see when you attempt to bind to that socket again is normal and expected, and there's some explanations as for why here.
You can set the SO_REUSEADDR socket option to tell the system to go ahead and allow a program to bind to that port anyway. For java, see here
You can't do an instant rebind as sockets linger until all queued messages for the socket have been successfully sent or the linger timeout has been reached. You can change this policy with SO_LINGER.
More info can be found here (manpage) and here (javadoc)
As for the closing problem, this seems like a bug in your code.

How do i close an opened port in Windows 7?

when i try to start my java based server there is a message that says the port is already in use...
And all my java web servers are stopped... So if anyone can help me i will appreciate it...
If there is really still a process that has the port open, you can easily check for that (and close the process) via TCPView. It might be that the port is just lingering (for example due to not being shutdown properly), in which case you'd have to wait for the socket to close and check that the code is clear with regard to that.
I was trying to establish socket communication between PC(JAVA Server)<-->Android(Client)
on Debugging, popped the problem : Address already in use
instant/feasible resolution i found out was to:
run cmd as adminstrator
netstat -abn
find the process listening to the port of intrest
terminate the process from task manager
PS : Unlike from an external application mentioned in previous answers, steps in this answer allows you to find out the used ports with the resources already present.
A few options... us TCPView to find the program that has the port open and kill it... reboot... just reset the network connection. Try those.

Categories