Reputation: 25
not getting the message "File Recieved Sucessfully" please help me.While transfering the file from client to server even the file is received it doesnot showing the message.
It is the program to transfer the file from client to server using sockets and interface is also created by using java swings so please kindly help me to rectify the error
package Receiver;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Receive extends JFrame {
public Receive() {
super("Packet hiding");
//Svr3.main();
setSize(350, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton readyButton = new JButton("Ready");
JButton next = new JButton("Exit");
readyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try{
ServerSocket server=new ServerSocket(127);
Socket clientS=server.accept();
Scanner read=new Scanner(System.in);
Scanner scan=new Scanner(clientS.getInputStream());
PrintWriter out=new PrintWriter(clientS.getOutputStream(),true);
String idata,odata;
int bytesRead;
int current = 0;
try
{
idata=scan.nextLine();
String inp;
inp = JOptionPane.showInputDialog(null, "Question: "+idata);
odata=inp;
out.println(odata);
InputStream in = clientS.getInputStream();
OutputStream output = new FileOutputStream("C:/Users/KRISHNASAI/Documents/NetBeansProjects/Proj1/src/Receiver/Encrypt.zip");
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
JOptionPane.showMessageDialog(null,"File Recieved Sucessfully");
// Closing the FileOutputStream handle
output.close();
scan.close();
out.close();
clientS.close();
server.close();
}
catch(Exception e)
{
}
System.out.println("conversation ended");
}
catch(Exception e)
{
}
}
});
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try{
Decrypt.main(null);
System.exit(0);
}
catch(Exception e)
{
}
}
});
c.add(readyButton);
c.add(next);
}
public static void main(String args[]) throws Exception{
Receive s = new Receive();
s.setVisible(true);
}
}
Upvotes: 0
Views: 257
Reputation: 15428
OutputStream output = new FileOutputStream("your file");
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
JOptionPane.showMessageDialog(null,"File Recieved Sucessfully");
Swing does event(for example, ActionEvent
) handling task including GUI rendering in Event dispatch thread(EDT). Any kind of task tends to take longer executing inside this thread will block this thread and hence your application will become frozen(Stuck like it is dead).
Remove your file transferring code from readyButton
actionPerformed(ActionEvent)
function.
create a Runnable class class FileTransferHandler extends Runnable
, override it's run()
function with necessary code.
Start a thread with the runnable class on readyButton
click: Inside the actionPerformed
function do: new Thread(new FileTransferHandler()).start()
In addition to these changes,
public static void main(String args[]) throws Exception{
Receive s = new Receive();
s.setVisible(true);
}
As i have said GUI rendering(updating) task should be done inside EDT. Make use of SwingUtilities.invokeLater(Runnable)
function to post the GUI rendering task to EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Receive().setVisible(true);
}
});
Upvotes: 1
Reputation: 94
May be you can set a point by line "idata = scan.nextLine();",step run will help you to find the wrong
Upvotes: 0