简体   繁体   中英

Robust Java Socket Programming

My client application needs to be able to recover in several situations. When the power is turned on the client reads the address and port from a file and attempts to connect to the server. If this is the first time this client has been used on this host a default IP address is tried but this may be incorrect. The user then needs to enter the correct IP address. The port is hard coded. The way it works now is a one second swing.Timer attempts to get the status of the server...

statusTimer = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        final byte GET_STATUS = 20;
        SendCommand(GET_STATUS, null);
    }
});
statusTimer.start();

If there is no connection the user is asked to connect...

private void SendCommand(byte command, byte[] parameters) {
    if ((null == socket) || (null == outputStream) || socket.isClosed())
        if (JOptionPane.YES_OPTION == 
            JOptionPane.showConfirmDialog(null, "Connect?")) 
            Connect();
            ...

private void Connect() {
    String s = (String) 
        JOptionPane.showInputDialog(null, "Enter IP Address", ipAddress);
    if (validateIp(s)) {
        ipAddress = s;
        connectTask = new ConnectTask();
        connectTask.start();
        ...

The socket is opened, the io streams are set, and an RxThread is started.

class ConnectTask extends Thread {
    @Override public void run() {
        try {
            if (null == socket) socket = new Socket(ipAddress, TCP_PORT);
            if (null == outputStream) outputStream = socket.getOutputStream();
            if (null == inputStream) inputStream = socket.getInputStream();
            if (null == rxThread) rxThread = new RxThread();
            ...

There are a few bad things that could happen at this point, for example:
1. The server could be off when the client is turned on. What is the best way for the client to automatically open the socket, set the io streams, and continue when the server is powered on?
2. In the same way, the server might see a power cycle during a read, write, or neither. Is there a good way to automatically recover after power is restored?
3. The IP address may need to be corrected as mentioned above.
It would be nice to handle these and other situations without requiring the user to close and open the client application or power cycle the system. What kinds of approaches are needed for a bullet-proof java client socket?
This and this are similar but deal with a server sockets

You need to be able to distinguish between first-time usage (when the server details may be wrong, and the user should be prompted) and later usage (when the server may just be down). You could save a file when the user first enters the IP, for example.

After that, you probably want something like: an inner loop where you attempt to connect to the server and read data, with a timeout. If the socket is closed due to a network error then this can be caught with a try-catch block. Put all this into an outer loop so that you try again when disconnected.

Depending on the details, and assuming the amount of data is small, it may well be easiest for your client to open a connection every time rather than attempting to maintain a connection, then sleep in between connections.

If the server is unresponsive, your current code (which creates a new ConnectTask thread each second using a Timer) may end up creating multiple ConnectTasks, one created per second, each trying to connect in parallel, though it's hard to be certain from the partial code you posted.

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