Results 1 to 5 of 5
  1. #1
    JavaGeek is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Sockets - Listening to a remote port

    I am new to sockets. I have a piece of equipment that sits on our network at a specific address and port. The equipment cannot connect to a remote location. However, it can initiate a conversation on the port. In Java, we are connecting to the equipment using a Socket (client) and constantly checking for messages through a loop. This seems inefficient. Is there a better way to listen to a remote port?

    At times, we can be the one initiating the conversation, but I'm fine on that part.

    Thanks in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Sockets - Listening to a remote port

    I am still not certain what you are trying to do. Here is what I get so far. You have some equipment that is apparently listening on some port at some IP address. You said the equipment cannot make an outgoing connection but can initiate a conversation on the port. This seems contradictory to me. What I would assume is that the equipment can listen for incoming connection requests and can respond to those but cannot initiate one on its own. But you want to check for periodic activity on the equipment.

    What you are essentially describing is polling a piece of equipment. The ideal way would be to cause an "interrupt" and have the equipment contact you when there is activity. But you said you can't do that. So polling is probably the only way. One method would be to have a background thread maintain a connection to the equipment and do the polling and then generate an event to alert some other listener that activity has occurred. Then you have several options. Let the background thread act as an intermediary and relay the information. Or have the receiver of the event initiate a new connection to retrieve the information.

    Edit: There may be another possibility. If the equipment has Simple Network Management Protocol (SNMP) support you might be able to use that to generate activity alerts. You need to check out the equipment specs and if it supports SNMP then read up on that and see if it can be used. And I do not know if the JDK (standard or enterprise) supports SNMP so you may need a third party package. Be warned though, the world of SNMP and MIBs etc is a very large one. You won't learn this stuff overnight.

    Regards,
    Jim
    Last edited by jim829; 11-02-2014 at 05:45 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kakes is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default Re: Sockets - Listening to a remote port

    I couldnt understand your problem exactly.
    But i guess you want to send something to and from a device through a network.
    I recommend you to use simple version of tcp or udp programming. You can ask me if you need further help :)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Sockets - Listening to a remote port

    As described, the simple answer to me is "use blocking IO" so the thread doing the read action will block until there actually is something to read. But that is me making assumptions that the "constantly in a loop" bit means that non-blocking IO is used. It would be nice to seem some code that illustrates how that loop currently works.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JavaGeek is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: Sockets - Listening to a remote port

    Thank you all for your replies. (For some reason, I didn't get an e-mail.)

    I only started looking at this a few days ago, and I may not be explaining it very well. The equipment communicates via COM port. We use a device to connect multiple pieces of equipment to the network. The device acts as a server to pass messages back and forth through the COM port. My application is a web-based user interface that talks to the equipment through the device. Sometimes, the web app will initiate communication to the equipment - no issues with this. Other times, the equipment needs to initiate communication, but the device doesn't forward the messages to the web server. I have to constantly check the socket for messages to read. Here is a very rudimentary example of the code:

    Java Code:
    Socket mySocket = new Socket(deviceIP, equipmentPort);
    
    while(threadIsRunning){
       //Attempt to read
       InputStream myStream = mySocket.getInputStream();
       ... //Read the data, if any.
    }
    The threads for each piece of equipment loop forever (until interrupted) polling for the messages that may be coming from the equipment. This seems very inefficient to me, but maybe that's the only way to do it. I will look into the suggestions you gave. I also found software from the device vendor that forwards the communication to a port on a local computer (my web server). That way, I could open a ServerSocket on that local port to listen for messages. But that software only runs on Windows. I am going to contact the device manufacturer to see if there is a better Java solution.

Similar Threads

  1. Sending serialized objects to a remote server via NIO sockets
    By noodle_variation_187-X in forum Networking
    Replies: 0
    Last Post: 05-14-2013, 01:26 PM
  2. Sockets - 1 sender, but multiple listening
    By Ambergahill in forum New To Java
    Replies: 2
    Last Post: 11-11-2010, 03:27 AM
  3. Multiple Sockets on a single Port
    By Sajith in forum Networking
    Replies: 2
    Last Post: 08-16-2010, 06:31 PM
  4. Replies: 7
    Last Post: 11-25-2009, 11:34 PM
  5. Accessing Remote Serial Port
    By ali_sakar in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •