Results 1 to 5 of 5

Thread: code gets hang

  1. #1
    rungtashubham is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default code gets hang

    hey m developing a program which is based on swing. i want to open a new form when i select a radio button. i have done coding for this. but when new form opens my code gets hang. i don't know what to do for this...
    plz help me as soon as possible.
    i need to submit it at my college..

    m posting u my code here.
    problem is with File Transfer Radio button...

    Java Code:
    package FileTransferServer;
    
    import java.net.*;
    import java.awt.Desktop;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
     
    public class FTPServer {
     
         public FTPServer() {
    
     		  DataInputStream din;
    		  DataOutputStream dout;
    		  BufferedReader br;
              int port;
              ServerSocket server_socket;
     		  String fileName;
             
              port = 6666;
     
              try {
     
                   server_socket = new ServerSocket(port);
                   FileInformationForm.txtInforamtion.append("\nServer waiting for client on port " + server_socket.getLocalPort());
     
                   // server infinite loop
                   while(true) {
                        Socket socket = server_socket.accept();
                        FileInformationForm.txtInforamtion.append("\nNew connection accepted "+socket.getInetAddress()+":"+socket.getPort());
     					din=new DataInputStream(socket.getInputStream());
    					dout=new DataOutputStream(socket.getOutputStream());
    					br=new BufferedReader(new InputStreamReader(System.in));
                        try { 
                             byte[] b = new byte[1024];
                             int len=0;
                             int bytcount=1024;
                             String FileName="ftptest.pdf"; 
                            
                             InputStream is = socket.getInputStream();
                             BufferedInputStream in2 = new BufferedInputStream(is,1024);
                             fileName=din.readUTF();
                             FileInformationForm.txtInforamtion.append("\nFile Name : "+fileName);
                             FileInformationForm.txtInforamtion.append("\nComputer Name  : "+din.readUTF());
                             FileInformationForm.txtInforamtion.append("\nTime  : "+din.readUTF());
                             FileOutputStream inFile ;
                             File file = new File(fileName);
                             int option = JOptionPane.showConfirmDialog(FTPServerMain.desk,"File Received Save Disk?");
                             if(option == JOptionPane.YES_OPTION){
                             	JFileChooser chooser = new JFileChooser();
    							chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    							chooser.showOpenDialog(FTPServerMain.desk);
                             	inFile = new FileOutputStream(chooser.getSelectedFile()+"\\"+fileName);
                             	while( (len = in2.read(b,0,1024)) != -1 ) {
                            		 bytcount=bytcount+1024;
                            	 	inFile.write(b,0,1024);
                             	}
                             	in2.close();
                             	inFile.close(); 
                             	FileInformationForm.txtInforamtion.append("\nBytes Writen : "+bytcount);
                             	FileInformationForm.txtInforamtion.append("\nFile Transferred Successfully");
                             }
                             else {
                             	inFile = new FileOutputStream("tmp"+"\\"+fileName);
                             	while( (len = in2.read(b,0,1024)) != -1 ) {
                            		 bytcount=bytcount+1024;
                            	 	inFile.write(b,0,1024);
                             	}
                             	in2.close();
                             	inFile.close(); 
                             /*	FileInformationForm.txtInforamtion.append("\nBytes Writen : "+bytcount);
                             	File fileInput = new File(fileName);
                             	Desktop dsk = Desktop.getDesktop();
                            	dsk.open(fileInput);*/
                             }
                        }
    				    catch(IOException e) {
                        	FileInformationForm.txtInforamtion.append("\nUnable to open file" + e);
                             return;
                        }
                        try {
                            File fileInput = new File(fileName);
                            if(fileInput.exists()) {
                            
                            }
                        }
                        catch (Exception e) {
                        	FileInformationForm.txtInforamtion.append("EXception "+e);
                        }
     
                   }
     
              }
     
              catch (IOException e) {
            	  FileInformationForm.txtInforamtion.append("EXception "+e);
              }
         }
    }
    Java Code:
    package FileTransferServer;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import serverside.*;
    
    
    public class FTPServerMain extends JFrame implements ActionListener{
    	static final long serialVersionUID = 101;
    	 static JDesktopPane desk;
    
    	public FTPServerMain() {
    	 desk = new JDesktopPane();
    		setJMenuBar(setMenuBar());
    		setContentPane(desk);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(1020,730);
    		setVisible(true);
    		desk.add(new FileInformationForm());
                    //ServerSideApp.getApplication().hide(null);
                    FTPServer fTPServer = new FTPServer();
    	
    	}
    	
    	public static void main(String[] arc) {
            FTPServerMain fTPServerMain = new FTPServerMain();
    	}
    	
    	public void actionPerformed(ActionEvent ae) {
    		if(ae.getActionCommand().equals("Exit")){
    			System.exit(0);
    		}
    	}
    	private JMenuBar setMenuBar() {
    		JMenuBar mbr = new JMenuBar();
    		JMenu mnuFile;
    		JMenuItem mnuExit;
    		
    		mnuFile = new JMenu("File");
    		mnuExit = new JMenuItem("Exit",'x');
    		mnuFile.add(mnuExit);
    		mnuFile.setMnemonic('f');
    		
    		mbr.add(mnuFile);
    		
    		
    		mnuExit.addActionListener(this);
    		
    		return mbr;
    	}
    }
    Java Code:
    package FileTransferServer;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    
    import javax.swing.JInternalFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    
    public class FileInformationForm extends JInternalFrame{
    	static final long serialVersionUID = 101;
    	static JTextArea txtInforamtion;
    	JScrollPane pane;
    	public FileInformationForm() {
    		
    		toInitialize();
    		
    		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    		setBounds((dim.width-600)/2,(dim.height-510)/2,600,510);
    		setTitle("File Information");
    		setVisible(true);
    	}
    
    	private void toInitialize() {
    		txtInforamtion = new JTextArea();
    		pane = new JScrollPane(txtInforamtion);
    		
    		getContentPane().setLayout(null);
    		getContentPane().add(pane);
    		
    		txtInforamtion.setBackground(Color.black);
    		txtInforamtion.setForeground(Color.green);
    		txtInforamtion.setEditable(false);
    		
    		pane.setBorder(new CompoundBorder(new TitledBorder("FileInformation"),new EtchedBorder()));
    		pane.setBounds(50,100, 500,300);
    	}
    }
    this is my code which gets hang when i call it via another form.
    plz tell me what wrong wid this or what should i do?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: code gets hang

    Read up on threading and Swing because it looks like you're trying to call long-running tasks on the Swing event thread or EDT which will freeze this thread and cause your application to become completely unresponsive. You can find out more in this tutorial: Concurrency in Swing

  3. #3
    rungtashubham is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: code gets hang

    thanx but i have followed all the steps mention in this link.. still getting same problem... :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: code gets hang

    Quote Originally Posted by rungtashubham View Post
    thanx but i have followed all the steps mention in this link.. still getting same problem... :(
    Then show us your most recent SSCCE, and let's see if we can help you out further.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: code gets hang

    Does the FTPServer constructor return to where is is called in the FTPServerMain constructor?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need Help Starting GUI for Hang Man (JFrames)
    By napoleonrokz in forum New To Java
    Replies: 3
    Last Post: 05-27-2011, 01:19 AM
  2. What type of hang? and how to fix?
    By joshbrigade in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 11:40 PM
  3. Read XML from Internet Hang!
    By yupingliew in forum Networking
    Replies: 0
    Last Post: 10-22-2009, 06:56 AM
  4. Applet hang
    By Worlock in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 07:43 AM
  5. Web application hang without comm.jar
    By chankokchern in forum Java Applets
    Replies: 0
    Last Post: 12-16-2008, 01:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •