How to initiate an update for clients by the server in java

I have a client/server app to manage a line of some sort. all the clients add objects to my line.

I want the server to send a screen capture of the jpanel to the clients every time there is a change in the line, line inserted or removed. I managed to capture the jpanel to a jpeg and even send it. but the flow of my app is stopped, after the first update I get eofexception that terminates my listening server socket.

what is the correct way to update a client ? should I set a serversocket to always listen on the client side too ?

please help, im stuck with this for like 2 weeks.

This is my listening thread (Server):

public class ListeningThread implements Runnable {

static boolean listening = true;    
public BufferedReader in;


public void run() {

    ServerSocket echoServer = null;
    String line;
    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;

   try {
       echoServer = new ServerSocket(RequestReciever._communicationPort);
    }
    catch (IOException e) {
       System.out.println(e);

    }   

// Create a socket object from the ServerSocket to listen and accept 
// connections.
// Open input and output streams

try {


// As long as we receive data, send it to be phrased to a request.
        while (true) {

         clientSocket = echoServer.accept();
         is = new DataInputStream(clientSocket.getInputStream());
         os = new PrintStream(clientSocket.getOutputStream());

 // An option for a stop listening button. currently not available !
         if( listening==true )  {
         line = is.readUTF();
         os.println(line); 
         System.out.println(line);
         RequestReciever.pharseToRequest(line);
//           clientSocket = null;


       }
       else  {
                echoServer.close();
                is.close();
                os.close();
                break;
            }
       }

}   

catch (IOException e) {
       e.printStackTrace();
       System.out.println("Listening Thread Unknown error");
    }
}
}

This is my Pharse Method:

 public static void pharseToRequest(String input) {

    List<String> list = new ArrayList<String>(Arrays.asList(input.split(";;;")));
    if (list.get(0).equalsIgnoreCase("Login") && list.get(1).equalsIgnoreCase ("Login") && list.get(2).equalsIgnoreCase("5"))
    {
        _adminClients.add(list.get(4));
        updateScreenCapture();
        AdminClientUpdate tmp = new AdminClientUpdate(list.get(4));
        Thread aCU = new Thread (tmp);
        aCU.start();
    }
    else
    {
    ServerRequest newReq = new ServerRequest(list.get(0), list.get(1), Integer.parseInt(list.get(2)),list.get(3),list.get(4));
    addRequest(newReq);
    }
}

and This is the AdminClientUpdate Class

public class AdminClientUpdate implements Runnable {

static boolean listening = true;    
public BufferedReader in;
public String _ip;

public AdminClientUpdate(String ip)
{
    _ip = ip;
}

public void run() {

        try {
            Socket socket = new Socket(_ip,   RequestReciever._communicationPort);
            InputStream in = new FileInputStream("Capture/tmp.jpg");
            java.io.OutputStream out = socket.getOutputStream();

            copy(in, out);
            System.out.println("Sent Image !");
            socket.close();
            out.close();
            in.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("Cant find tmp.jpg");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }


    static void copy(InputStream in, java.io.OutputStream out) throws IOException {
            byte[] buf = new byte[8192];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
     }
}

Answers


eliminate this

echoServer.close();

this line closes the socket. due which the connection is aborted.


After a few brain meltdowns , I have decided that putting a server socket on the client side to listen for updates from the server is the best way. I fixed a few things : * The server should start a new thread to handle every accepted connection, instead of processing each one in-line in the accept thread. * I tried to get the first update via the server socket instead of the login initialization. now, after getting the 1st update while logging in, I added a Server Socket on the client side so it will keep listening for further updates from server.


Need Your Help

How to disable a textbox without fading text?

c# asp.net textbox

Is there anyway of getting all of the properties of a !enabled textbox except the faded text?