Solved

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

Posted on 2012-04-05
8
287 Views
Last Modified: 2012-04-09
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);
out.println(SendMessage);
out.close();
echoSocket.close();
0
Comment
Question by:cschene
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 37814489
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'
0
 

Author Comment

by:cschene
ID: 37814512
"Can you please post the full stack trace?"

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


catch(Exception e) {
                  x1.stopTimer();
                  x1.setPortData(e.getMessage() + " on port " + x1.getPortNumber());
                  System.err.println(e.getMessage());
                  }
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 37814519
Change

System.err.println(e.getMessage());

Open in new window


to

e.printStackTrace();

Open in new window


and post the output
0
 

Author Comment

by:cschene
ID: 37815758
I figured out what my problem was...it 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 localhost...so 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.
CmServerPortTester.java
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 37817249
but the server side gets hung up after that.

Where? At what line?
0
 

Author Comment

by:cschene
ID: 37817733
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(CmServerPortTester.java:265)
        at CmServerPortTester.main(CmServerPortTester.java:66)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37817822
It is throwing when it hits this line

Be careful - 'throwing' != 'hung'

 at CmServerPortTester.processServerPortTest(CmServerPortTester.java:265)

Open in new window


What line does that correspond to in the file you attached? (It's not the same)
0
 

Author Comment

by:cschene
ID: 37825830
Sorry.....it was my mistake...pure cockpit error.

Thanks for the help...you have the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
Java Jpanels and Jframe 8 28
stackato and cloud 4 64
servlet example issue 6 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now