简体   繁体   中英

Client Socket as the listener?

I need all my clients attached to a server to be socket listeners. The server will attempt to connect to a specific client and instruct it to preform a various task via a socket message. So my question is, can my Java Client be the ServerSocket and listen for Server requests? And is it possible for a webpage loaded on a client machine to know the clients IP address to send the message too. We have an application running on our server that has many clients attached and when a button is pressed on the clients webpage it will attempt to connect with this client and send a message to it. Most of the time servers are listeners, but in our case we need just opposite where the client is listener waiting for server requests. But the server needs to know what the client IP address is to establish the connection since our clients are attached via DHCP.

Btw, All our clients are basically running a webpage from our server and each client has it own PC. The thing I need to do is control a USB device on each client machine. I have control over the website software, so my goal was to use sockets to do this since controlling a clients external hardware is very difficult remotely. So what I was going to do is write a Java app and that runs on the client machine acting as a listener which when someone presses a button on the webpage the server will attempt to connect to the client machine and send the message down to it. I'm just not sure how to handle this from the server side, so it knows what client it's talking too since I don't want to broadcast the message to all clients, but only the one the button was pressed on, keep in mind each client is attached via DHCP addressing, so I can't hardcode the hostID. I can attach java/groovy or ajax scripts to the button event on the server side, but I'm not sure how to setup the socket protocol on the server to make this work.

Here is an example of a socket connection I could use on the client / server side, but I need to know the clients DHCP IP address is to make it work properly.

Server side requesting client task

public void requestSocket() 
{ 
 Socket socket = null;
 private BufferedReader in = null;
 private PrintWriter out = null;

//Create socket connection for server
    String serverAddress = "localhost"; // Needs to be the client DHCP IP address.
    try
    {
        socket = new Socket(serverAddress, 4444);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    catch (UnknownHostException e)
    {
        System.out.println("Unknown host: " + serverAddress);
        System.exit(1);
    }
    catch  (IOException e)
    {
        System.out.println(e);
        System.out.println("No I/O");
        System.exit(1);
    }
}

Client side listener acting as the server

public void listenSocket()
{ 
    int portNumber = 4444;
    ServerSocket server = null;
    Socket client = null;
    BufferedReader in = null;
    PrintWriter out = null;

    try
    {
          client = new ServerSocket(portNumber);
    }
    catch (IOException e)
    {
        System.out.println("Could not listen on port " + portNumber);
        System.exit(-1);
    }

    try
    {
          server = client.accept();
    }
    catch (IOException e)
    {
        System.out.println("Accept failed :" + portNumber);
        System.exit(-1);
    }

    try
    {
        in = new BufferedReader(new InputStreamReader(server.getInputStream()));
        out = new PrintWriter(server.getOutputStream(), true);
    } catch (IOException e) {
        System.out.println("Read failed");
        System.exit(-1);
    }

    while(true)
    {
        try
        {
            line = in.readLine();
            //Send data back to client
            out.println(line);
        }
        catch (IOException e)
        {
            System.out.println("Read failed");
            System.exit(-1);
        }
    }
 }

Can anyone help answer these questions?

First: what are your clients? Java Client or Webpage in browser?

Second: if it is JavaClient it can be only ServerSocket in it to listen what your server will tell and Server must know IP and port of each clients to do that.

Third: for Java Client actually JMS with Topic is there for such tasks. Server publishes message, while clients listen and do with message what is needed.

and Forth: If it is a webpage there is only one way - from JavaScript (or auto-refresh mechanism) browser can ask Server periodically (by timer) for new messages. With HTML there is no way for the server to initiate any actions on client browser. It is always request-response.

Updated: try something like that:

<html>
<head>
    <title>Test Loop Back</title>
</head>
<body>
    <form action="http://localhost:4444" id=in_form>
    <table>
        <tr>
            <td>
                <label>IN_MSG:</label>
            </td>
            <td>
                <input type="text" name="IN_MSG" id="IN_MSG"/>
            </td>
        </tr>
    </table>
    <button type="submit" formmethod="post">Post Something</button>
   </form>  
  </body>
</html>

and see what you get in your background Java listening on port 4444. Mayb it can be enough? you can pass anything you may need as separate input fields and them can be hidden as well as whole form can be invisible...

If you need some data from server prepared for background java think the way on picture to avoid communication from server to background java. 在此处输入图片说明

main point is to avoid direct communication from server to background java. All passes through the browser, so no pain with IP, ports, proxy, security etc. Browser and javascript on webpage take care of all...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM