0

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 
	 {
		 try{
			 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();
            
             
             
             while(ans.equals(null))
             {
             
             }
             
             if(ans.equals("yes"))
             {
            	 try {
            		 
            	 BufferedReader br2=new BufferedReader(new InputStreamReader(dvclient.getInputStream()));
					 String filename=br2.readLine();
					System.out.println("name sent by Server:"+filename);
					if(filename.equals("cancelled"))
						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");
			              //System.out.print(bis);
			            while ((in = bis.read(receivedData))!= -1)
			              {
			                bos.write(receivedData,0,in);
			                System.out.println(receivedData);
			              }
			             bos.flush();
			             //if(!(filename.equals("null")))
			             //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);
			             jifpanel.add(imagelabelnew);
			             jif.add(jifpanel);
			             jif.setSize(new Dimension(800,600));
			             jif.setVisible(true);
			             jifpanel.updateUI();
			           //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
            	 
					dvclient.close();
			    
          
					
             }
            	 else{
                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");
			System.out.println("");
			
			while(true)
			{
				try{
					ServerSocket dvcomm_S = new ServerSocket(3003);
				       dvcommconnection=dvcomm_S.accept();
				       System.out.println("Connection Established on port 3003");
			        ServerSocket dvserver = new ServerSocket(3002);
			       dvconnection=dvserver.accept();
			        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",
			                                          JOptionPane.YES_NO_OPTION);
			    	
			    	if(more==0)
			    	{
			    		
			    	try{
			    	System.out.println("user clicked yes");
			    	BufferedReader console=new BufferedReader(new StringReader("yes"));
					PrintWriter pw=new PrintWriter(dvconnection.getOutputStream());
					String send;
					send=console.readLine();		
					pw.println(send);
					pw.flush();
					//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();
	                 
	                  
	                  
	                		  
	                   System.out.println(filename);
	                   pw2.println(filename);
	                   
	                   pw2.flush();
	                   
	                   /*BufferedReader br2=new BufferedReader(new InputStreamReader(dvconnection.getInputStream()));
	                   String start_=br2.readLine();
	                   while(start_.equals(null)){}*/
	                  
	                  
	                  
	                 
	                 
	                  
	                  
	           
	             DataInputStream input;
	             DataOutputStream output;
	             BufferedInputStream bis;
	             BufferedOutputStream bos;
	             int in;
	               
	             byte[] byteArray;
	          try {

	                

	                
	                output=new DataOutputStream(dvconnection.getOutputStream() );
	                
	               
	                
	                filename=file.getAbsolutePath();
	                 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)
	                {
	                  bos.write(byteArray,0,in);
	                   //System.out.println(byteArray);
	                }
	                 
	                 bos.flush();
	               //printWriter to send the commands to the client
				       
	                 //JOptionPane.showMessageDialog(null,"File Successfully Transfered to"+s_ip);
	          }catch ( Exception e ) 
	          {
	              System.err.println(e);
	            }
           		 
	          dvconnection.close(); 
					
			    	
			    	
			    	}catch(Exception e){}}
			    	else
			          {
			            JOptionPane.showMessageDialog(null,"Desktop view stopped by server");
			            try{
					    	
					    	System.out.println("user clicked no");
					    	BufferedReader console=new BufferedReader(new StringReader("no"));
							PrintWriter pw=new PrintWriter(dvconnection.getOutputStream());
							String send;
							send=console.readLine();		
							pw.println(send);
							pw.flush();
					    	}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 .

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by BestJewSinceJC
0

Make a loop in the main class that is like

while(true)
{
-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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.