How do I know when data had been read from a port and it can be closed

I am using this snippet of Java code to send data to a port. If I execute the two close commands the receiver reading from the port throws a bind error: "Unrecognized Windows Sockets error: 0: JVM_Bind"

If I never execute the two close commands, the reader gets the data just fine.

I assume I must close the PrintWriter and echoSocket eventually...but how do I know when the receiver has read all the data so I can close?

Java code:

Socket echoSocket = new Socket(servername, Tx.getPortNumber());
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
Christopher ScheneSystem Engineer/Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the receiver reading from the port throws a bind error: "Unrecognized Windows Sockets error: 0: JVM_Bind"

Can you please post the full stack trace?

Generally speaking, when you have this kind of thing happening (although the error message seems slightly odd in this case), the fault is in the protocol. It sounds like you have not arranged the protocol in an orderly way,such that the receiver is 'surprised' that the sender has closed the socket. If you can't rearrange the protocol, you will be forced to handle the exception at the receiver as 'unexceptional'
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
"Can you please post the full stack trace?"

How do I do that? I am processing the exception as follows:

catch(Exception e) {
                  x1.setPortData(e.getMessage() + " on port " + x1.getPortNumber());


Open in new window



Open in new window

and post the output

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I figured out what my problem was the parsing of the int...which I believe you answered as well.

What I am trying to do here is test a group of ports by sending a message to a port on a server (2112) and having the server send that message back to the client on the port that is specified in the message and repeat this until the client processes all ports and either verifies them as working or not working. We need this test because we have to keep certain ports open for our software to function.

The approach I am taking is (1) server waits on the Rx port (2112) for a message, (2) Server reads the message  and sends that exact message back to the client and then (4) the client processes the next port and so on until done.

This is experimental code:  I am just trying to figure out the proper approach and then I will throw away and re-write once I understand the design required.

if I run with the following commandline, the code will do a loopback test where it sends and receives on the same port. This works.

java CmServerPortTester -loopbacktest  -txport 2112 -rxport 2112

if I want to run a test of ports 2112, 500, 1024 I run two copies of the software. Note, server is defaulting to I am looping on the same PC for testing.

server: java CmServerPortTester -server  -txport 2112 -rxport 2112
client: java CmServerPortTester -client  -txport 2112 -rxport 5000 -rxport 1024

So, it exchanges the first set of messages and verified one port, but the server side gets hung up after that.
but the server side gets hung up after that.

Where? At what line?
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
It is throwing when it hits this line

      Socket echoSocket = new Socket(servername, i);

In this Method: static void processServerPortTest(testPort x) {

I am not getting much detail on the exception. All I see is this: (how do I get more detail?)

Exception in thread "main" java.lang.NullPointerException
        at CmServerPortTester.processServerPortTest(
        at CmServerPortTester.main(
It is throwing when it hits this line

Be careful - 'throwing' != 'hung'

 at CmServerPortTester.processServerPortTest(

Open in new window

What line does that correspond to in the file you attached? (It's not the same)
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented: was my mistake...pure cockpit error.

Thanks for the have the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.