What i need to do is with the help of Sockets in java,:
If a client requests for desktop view of the Server
the Server code should then take screenshots one after the other and send it to the client and the client will display one as soon as the image screenshot is reached to him.
i have written a code to send the first screenshot from the Server to the client,IT WORKS FINE FOR A SINGLE TIME.

here is the code

SwingWorker dvworker=new SwingWorker<String,Void>(){
	 public String doInBackground() throws
     AWTException, IOException 
			 JOptionPane.showMessageDialog(null,"waiting for the server to respond");
             Socket dvcomm_C=new Socket(s_ip,3003);
			 Socket dvclient=new Socket(s_ip,3002);
			 BufferedReader dvcomm_C_br=new BufferedReader(new InputStreamReader(dvcomm_C.getInputStream()));
             BufferedReader br=new BufferedReader(new InputStreamReader(dvclient.getInputStream()));
             String ans=br.readLine();
            	 try {
            	 BufferedReader br2=new BufferedReader(new InputStreamReader(dvclient.getInputStream()));
					 String filename=br2.readLine();
					System.out.println("name sent by Server:"+filename);
						JOptionPane.showMessageDialog(null,"Desktop view cancelled by the remote machine"+dvclient.getInetAddress());				
			    		DataOutputStream output;
			          DataInputStream input;
			          BufferedInputStream bis;
			          //BufferedOutputStream bos;
			           byte[] receivedData;
			           int in;
			            receivedData = new byte[9999];
			           /* String wd1 = System.getProperty("user1.dir");
			//---------------------------------------JFileChooser will be opened here..-------------------------------------------

			     JFileChooser fc1 = new JFileChooser(wd1);
			     int rc1 = fc1.showDialog(null, "Select a Folder a save the File");
			     if (rc1 == JFileChooser.APPROVE_OPTION)
			      File file1 = fc1.getSelectedFile();
			      filename1 = file1.getAbsolutePath();
			      // call your function here
			      } */   
			            bis = new BufferedInputStream(dvclient.getInputStream());
			          // bos = new BufferedOutputStream(new FileOutputStream("C:\\sss.txt"));
			             FileOutputStream bos=new FileOutputStream("D:\\ScreenSharing_files\\"+filename);//received.txt");
			            while ((in = bis.read(receivedData))!= -1)
			             //JOptionPane.showMessageDialog(null,"File from "+dvclient.getInetAddress()+ "Successfully received");
			            // else
			            	// JOptionPane.showMessageDialog(null,"Desktop view  from "+dvclient.getInetAddress()+ "stopped by the remote machine");
			             jif.setTitle("Desktop view "+dvclient.getInetAddress());
			             ImageIcon icon=new ImageIcon("D:\\ScreenSharing_files\\"+filename);
			             JLabel imagelabelnew=new JLabel(icon);
			             jif.setSize(new Dimension(800,600));
			           //printWriter to send the commands to the server
			             PrintWriter pw_C=new PrintWriter(dvcomm_C.getOutputStream());
			    	}catch(Exception e){System.err.println(e);}
             //for ends here
                JOptionPane.showMessageDialog(null,"server rejected the Desktop view");
	 }catch(Exception e){System.err.println(e);}
	 return new String();
 public void done()
 SwingWorker DesktopviewServer=new SwingWorker<Void, Void>() {

		public Void doInBackground() {
			Socket dvconnection;
			Socket dvcommconnection;
			String filename="";
			System.out.println("Desktop View communication Server Listening on port 3003");
			System.out.println("Desktop View Server Listening on port 3002");
					ServerSocket dvcomm_S = new ServerSocket(3003);
				       System.out.println("Connection Established on port 3003");
			        ServerSocket dvserver = new ServerSocket(3002);
			        System.out.println("Connection Established on port 3002");
			        //buffered reader to read the commands from the client

			    	more=JOptionPane.showConfirmDialog(jf, "Do u want to share your screen ?", "yes or no",
			    	System.out.println("user clicked yes");
			    	BufferedReader console=new BufferedReader(new StringReader("yes"));
					PrintWriter pw=new PrintWriter(dvconnection.getOutputStream());
					String send;
					//done handshaking,now take the screen capture and send the files
					filename ="";
	            	 File file=new File("new");
	            	 PrintWriter pw2=new PrintWriter(new OutputStreamWriter(dvconnection.getOutputStream()));
	            	 BufferedImage screencapture = new Robot().createScreenCapture(
	            	           new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );

	            	    file = new File("screencapture.jpg");
	            	     ImageIO.write(screencapture, "jpg", file);
	            	     //file is now saved on the hard disk in the current folder
	                 filename = file.getName();
	                   /*BufferedReader br2=new BufferedReader(new InputStreamReader(dvconnection.getInputStream()));
	                   String start_=br2.readLine();
	             DataInputStream input;
	             DataOutputStream output;
	             BufferedInputStream bis;
	             BufferedOutputStream bos;
	             int in;
	             byte[] byteArray;
	          try {


	                output=new DataOutputStream(dvconnection.getOutputStream() );
	                 bis = new BufferedInputStream(new FileInputStream(filename));
	                 input =new DataInputStream(bis);       
	                   bos = new BufferedOutputStream(dvconnection.getOutputStream());
	                 byteArray = new byte[9999];
	                 while ((in = input.read(byteArray))!=-1)
	               //printWriter to send the commands to the client
	                 //JOptionPane.showMessageDialog(null,"File Successfully Transfered to"+s_ip);
	          }catch ( Exception e ) 
			    	}catch(Exception e){}}
			            JOptionPane.showMessageDialog(null,"Desktop view stopped by server");
					    	System.out.println("user clicked no");
					    	BufferedReader console=new BufferedReader(new StringReader("no"));
							PrintWriter pw=new PrintWriter(dvconnection.getOutputStream());
							String send;
					    	}catch(Exception e){System.err.println(e);}
			      // return new String();
		}catch(Exception ee){}
		public void done()
			System.out.println(" i am dvserverworkers done");

what this code does for me is :
it shows me a screen shot jpeg image of the server machine,taken immediately.

What i need to do is:
i need a mechanism by which i can loop both the server for sending files until the client says for eg."Exit ".or server says for eg."no more Desktop viewing".
and the client should update the images in a frame as soon as they are received from the server;

What i have tried is :
I made another socket connection(on port 3003,name of the socket is dvcomm_S) when the first socket connection is established.that means two sockets connection instead of one now.
After the handshaking is done between the client and the server,
that means when the server says "yes " for the request for Desktop view from the client,
a new BufferedReader on the server for the second socket wil be used to check the "exit" condition.i.e client wil send the exit command via PrintWriter
and vice versa.
and i then put the code in a while loop(condition) on both the sides (server and client)

What happens after these changes:
only one of the threads is looping,i.e Server keeps taking screenshots but doesnt sends .
dont knw what happens next!!!

Please pls help me with this code
and tell me what i can do with existing code .

Re: Trouble in Desktop sharing code 80 80

Make a loop in the main class that is like

-Accept incoming connection & spawn a new thread for it

There may be a better way but when I made a program to accept 4-5 connections at once, this worked well. I'm not sure if continuously sitting in a while loop would be a good idea for a real-world application. (I'm pretty sure it wouldn't be). But that is the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.