This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a socket connection that needs to do the following:
1. Send data to the socket and return that info to the caller. 2. Use a thread to listen for data all the time, and send that data to a parser.
I can do one or the other fine, but when trying to do both of these things I run into a problem. For example, I have a method in another class which sends a command to ask for host information, and expects an ArrayList to be returned. The thread picks up the return data and sends it to the parser, but that will not help me because I need that data returned to the command sender. At the same time, there will be incoming data on the socket that was not requested with a command, and that data does need to be sent to the parser.
So my question is, how can I return data on a command to the method that called the command, and also listen for other data at the same time?
Here is the code I have right now, which can send a command, but does no return it to the method that sent the command:
[ March 03, 2008: Message edited by: Brad Walton ]
Hi Nitesh.. thanks for the reply. There is only 1 connection, from this class to a server. I read the data from the server with 'line = in.readLine();' in the code above.
I had an idea to use a command_flag, and a static instance dataArrayList. Something like this in the Listen thread:
And in the command method:
this way is a little sloppy and can cause unwanted data to get into the ArrayList along with the wanted data. I have a feeling there is a much better way to do this, but I haven't figured it out (and that's why I'm here ) [ March 04, 2008: Message edited by: Brad Walton ]
Hi Brad, I think i am not very clear about your question. In the initial post i thought you were asking about how to concurrently listen for data and process the received data. Have you got the answer for that? In the last post i think you are asking about how to parse the data, is that so? What is the wanted and unwanted data you are referring to here?
Joined: Feb 16, 2008
Ok, let me try to explain it again. I need this class to connect to a server (done and working), and then listen on the socket for incoming data (I use a thread to accomplish that). If the �Listen� thread gets some data, send that data to a parser. Most of the time the data needs to go to the parser� but not all the time.
I also need to have the ability to send a command to the server, and retrieve the response from the server (for that specific command), so that the response may be sent back to the method in another class which called it. The class that actually sends the command and retrieves the response will return an ArrayList. I cannot do that with the Listener because it sends that data to a parser (there is no return in the Listener method).
So, I need to find a way to either halt the Listener class when I send a command, so I can listen in the send_command method... Or, in some way get the data I want returned to the class that needs it. What I was trying to find out is if anyone has done this, or something similar, before, and could share the design for how this works (or links to examples of this)? Or if anyone has any ideas as to how this could work?
Does that help clear it up? [ March 04, 2008: Message edited by: Brad Walton ]
There is a single server port(Socket that you are listening/sending request to) that you are concerned with. On the same port you get some messages which:
Are not a reply to one of the messages you have sent before.
Are a reply to one of the messages you have sent before.
Your problem is that you have two listeners that do different work based on the kind of message(as defined above) you have received? Is the above correct?
If yes, then this is a classic messaging scenario where you need to co-relate a request and a response. Also, you want to process different messages differently. How JMS does this is that it has a correlation id with a response message that the server sends. The correlation id is the id of the request message that this is a reply for. (You might have to read that line again ) If the message is not a reply message, then it will not have a correlation id. You can have filters on your listeners that will choose based on the correlation id. So, a listener listening for replies will only pick reply messages (i.e. the ones having a correlation id) and the other listener will just pick messages that does not have a correlation id.
So, in your case also, the server needs to co-operate with you by sending a correlation id with the reply messages.
Joined: Feb 16, 2008
I only have 1 Listener right now, which I hoped to handle both types of server messages.. But yes you've essentially got the concept right. This server will not 'play nice' in terms of a correlation id. It will, however, send a number counter when each response is completed. Something like:
Then the server will return this at the end of the next command reply:
... and so on. If I kept a static counter in the class would that be sufficient? I have managed to get this working using a design I thought up the last day or so, but I doubt it would be considered very elegant. Below is the class (sorry for the messy code):
As you can see it uses the String command to determine if a command has been sent, and if so it does not send it to the parser. The sendReceiveCommand method picks this up and returns it to the caller. Pretty ugly eh? [ March 05, 2008: Message edited by: Brad Walton ]