Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while reading data through socket communication

Tags:

java

sockets

Following scenario that explains my problem.
I've a PLC that acts as a server socket program. I've written a Client Java program to communicate through socket communication with the PLC.

Steps that take place in this process are:
1) For each second my Client program happen to communicate with the PLC, read the data in stream, store the data temporarily in a ByteArrayOutputStream and closing both input stream and socket. Following snippet gives the idea

    try {
        socket = new Socket(host, port);
        is = socket.getInputStream();
        outputBuffer = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int read;
        if((read = is.read(buffer)) != -1) {
            outputBuffer.write(buffer, 0, read);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Before closing the socket");
            try {
                is.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("After closing the socket");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2) Processing stored data according to my requirement is what I'm trying to do. So for every 1 second, client program connects to Server, read the data(if data is present), store the data, close socket and process it. And it has to happen for a very long run, probably till the Server program is on. And that may happen till for every few weeks.

3) Problem what I'm facing is, I'm able to run the above show for 1-2 hours, but from then, Client Program unable to fetch the data from the Server Program(PLC in this case), though both are connected through socket. I.e 128 bytes of data present, but Client program isn't able to read that data. And this started happening after program run successfully for almost 2hours

4) Please find the brief code which may help for you to look into.

public class LoggingApplication {
    public static void main(String[] args) throws NumberFormatException {
        if (args.length > 0 && args.length == 2) {
            String ipAddress = mappingService.getIpAddress();
            int portNo = (int) mappingService.getPortNo();
            ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
            execService.schedule(new MyTask(execService, ipAddress, portNo, mappingService), 1000, TimeUnit.MILLISECONDS);
        } else {
            throw new IllegalArgumentException("Please pass IPAddress and port no as arguments");
        }
    }
}

Runnable Code:

public class MyTask implements Runnable {

    public ScheduledExecutorService execService;
    private String ipAddress;
    private int portNo;
    private ConfigurationMappingService mappingService;
    private MySocketSocketUtil mySocketSocketUtil;

    public MyTask(ScheduledExecutorService execService, String ipAddress, int portNo, ConfigurationMappingService mappingService) {
        this.execService = execService;
        this.ipAddress = ipAddress;
        this.portNo = portNo;
        this.mappingService = mappingService;
    }

    public void run() {
        MySocketSocketUtil mySocketSocketUtil = new MySocketSocketUtil(ipAddress, portNo);
        execService.schedule(new MyTask(execService, ipAddress, portNo, mappingService), 1000, TimeUnit.MILLISECONDS);

        mySocketSocketUtil.getData();  //It's able to fetch the data for almost 2 hours but from then, it's just getting empty data and it's keep on giving empty data from then. and so on.


        /*
         *
         *Some code
         */
    }
}

Here's where, I'm having the problem
mySocketSocketUtil.getData(); is able to fetch the data for almost 2 hours but from then, it's just getting empty data and it's keep on giving empty data from then. and so on. It's a big question I know, And I want to understand what might have gone wrong.

Edit: I'm ignoring the condition to check end of the stream and closing a socket based on it is because, I knew I'm going to read first 1024 bytes of data only always. And So, I'm closing the socket in finally block

like image 664
Kishore Kumar Korada Avatar asked Oct 16 '22 20:10

Kishore Kumar Korada


1 Answers

socket = new Socket(host, port);
if(socket != null && socket.isConnected())

It is impossible for socket to be null or socket.isConnected() to be false at this point. Don't write pointless code.

        if((read = is.read(buffer)) != -1) {
            outputBuffer.write(buffer, 0, read);
        };

Here you are ignoring a possible end of stream. If read() returns -1 you must close the socket. It will never not return -1 again. This completely explains your 'empty data':

from then, it's just getting empty data and it's keep on giving empty data from then, and so on

And you should not create a new Socket unless you have received -1 or an exception on the previous socket.

    } else {
        System.err.println("Socket couldn't be connected");
    }

Unreachable: see above. Don't write pointless code.

like image 60
user207421 Avatar answered Oct 20 '22 04:10

user207421