Unable to implement SwingWorker in FTP - java

I am trying to create FTP using TCP/IP in Java. Everything works fine in IDE, but then I decided to make it an IDE independent application by introducing GUI. In order to update GUI I implemented SwingWorker. Here is the code for my Client.java where the problem arises. Transfer gets successful but Stream is not flushed and the connection is not closed due to which the file gets corrupted. Also, GUI doesn't get updated.
Receive Method
public void receive()throws Exception
{
String ipadd = ipaddress.getText();
String port1 = socketname.getText();
String filepath = directory.getText();
String filepath1 = filepath.replaceAll("\\\\", "/");
String filename1 = name.getText();
String finalpath = filepath1+"/"+filename1;
int port = Integer.parseInt(port1);
Socket s = new Socket(ipadd,port);
byte[] contents = new byte[10000];
File file = new File(finalpath);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataInputStream is = new DataInputStream(s.getInputStream());
long fileLength = is.readLong();
while((bytesRead = is.read(contents))!=1) {
SwingWorker<Void,Integer> worker = new SwingWorker<Void,Integer>() {
#Override
protected Void doInBackground() throws Exception {
totalbytesread+= bytesRead;
bos.write(contents, 0 ,bytesRead);
publish(totalbytesread);
return null;
}
#Override
protected void process(List<Integer> chunks) {
int value = chunks.get(chunks.size() - 1);
System.out.println(value);
progress.setText("Received "+(value/1000000)+" MB out
of "+(fileLength/1000000)+" MB.......("+
(value*100)/fileLength+"%)");
super.process(chunks);
}
#Override
protected void done() {
progress.setText("File Received!!");
super.done();
}
};worker.execute();
}
bos.flush();
s.close();
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "File Successfully Received!!");
}
Please help..

Related

Java socket file transfer - Connection reset

I'm trying to make a program that transfer a file using java sockets. This is what I've written so far:
Sender:
private ServerSocket sendSocket;
public Send(int port) throws IOException
{
sendSocket = new ServerSocket(port);
}
public void run()
{
Socket socket = null;
try
{
Scanner scan = new Scanner(System.in);
InputStream inStream = null;
socket = sendSocket.accept();
inStream = socket.getInputStream();
String filePath = scan.nextLine();
OutputStream thisFile = new FileOutputStream(filePath);
byte[] bytes = new byte[16*1024];
int count;
while ((count = inStream.read(bytes)) > 0)
{
thisFile.write(bytes, 0, count);
}
System.out.println("Done!");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Receiver:
private static OutputStream thatFile;
public Receive(Socket socket) throws IOException
{
thatFile = socket.getOutputStream();
}
public void run()
{
try
{
System.out.println("Where do you want to save the file?");
Scanner scan = new Scanner(System.in);
String filePath = scan.nextLine();
File saveFile = new File(filePath);
byte[] bytes = new byte[16 * 1024];
InputStream inStream = new FileInputStream(saveFile);
int count;
while ((count = inStream.read(bytes)) > 0)
{
thatFile.write(bytes, 0, count);
}
}
catch (Exception e)
{
}
}
}
But whenever I run the program, after the client gives a destination for the download path, connection reset error happens on the sender side. I'm sure the port is open as I've tested on this port before. What's the problem?
This is what happens when I run the program:
Sender side:
Press 1 to send or 2 to receive.
1
What is the file's path?
C:\Users\orie5\Documents\Cmp\a.txt
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
at java.base/java.io.InputStream.read(InputStream.java:218)
at def.Send.run(Send.java:47)
Receiver Side:
Press 1 to send or 2 to receive.
2
Please enter the ip of the peer you want to connect to.
Where do you want to save the file?
C:\Users\orie5\Documents\Cmp\b.txt
Thanks in advance!

errors in getting & sending Data (String ,Int ) in socket java

hi every one i have a project that get someString and intand from client and i want to send them to my server.but i have problem in getting this . i get nothing or wrong things. please help me.
Client:
//build a socket.
public void connectclient() throws IOException
{
socket = new Socket("localhost", 9097);
System.out.println("connect to server on port 9097");
}
public void startstreams() throws IOException , ClassNotFoundException
{
in = socket.getInputStream();
out = socket.getOutputStream();
dos = new DataOutputStream(out);
dis = new DataInputStream(in);
writer = new OutputStreamWriter(out);
reader = new InputStreamReader(in);
breader = new BufferedReader(reader);
bwriter = new BufferedWriter (writer);
w = new PrintWriter(out, true);
}
public void writeSocketMyJson(String n) throws IOException
{
w = new PrintWriter(out, true);
w.println(n);
w.flush();
w.close();
}
massage1 = "username";
//send username that get from login form to server.
public void sendusername() throws IOException
{
writeSocketMyJson(massage1);
}
massage2 ="admin";
//send password that get from login form to server.
public void sendpassword() throws IOException
{
writeSocketMyJson(massage2);
}
//send access level to server.
public void sendaccess(int l) throws IOException
{
dos.writeInt(l);
dos.flush();
}
sendaccess(21);
Server:
//build server.
public void connectserver() throws IOException
{
listener = new ServerSocket(9097);
System.out.println("Server is running on port 9097 ...");
}
//wait for new connection.
public void waitforclient() throws IOException
{
socket = listener.accept();
System.out.println("A new client connected to the server");
}
public void startstreams() throws IOException
{
in = socket.getInputStream();
out = socket.getOutputStream();
dos = new DataOutputStream(out);
dis = new DataInputStream(in);
writer = new OutputStreamWriter(out);
reader = new InputStreamReader(in);
bwriter = new BufferedWriter (writer);
breader = new BufferedReader (reader);
}
public String readSocket() throws IOException
{
breader = new BufferedReader(reader);
while (true)
{
massage = new String();
massage = breader.readLine();
if (massage.equals(null) == false)
{
break;
}
}
return(massage);
}
//get username that client send it.
public String getusername() throws IOException, ClassNotFoundException
{
try
{
username = new String();
username = readSocket();
System.out.println("the username is : " + username);
}
catch(IOException IOE)
{
IOE.printStackTrace();//if there is an error, print it out
}
return(username);
}
//get password that client send it.
public String getpassword() throws IOException, ClassNotFoundException
{
try
{
password = new String();
password = readSocket();
System.out.println("the password is : " + password);
}
catch(IOException IOE)
{
IOE.printStackTrace();//if there is an error, print it out
}
return(password);
}
//get commend from client.
//admin or user send which commend 21-add new information 22-show information
public int getaccess() throws IOException
{
System.out.println("server get access : " + dis.readInt());
return(dis.readInt());
}
but when i call getpassword()i get nothing .
when i callgetaccess()i get nothing.
why? please help me.
i have main class too that control the orders
main:
//build Server & Client
Server server = new Server();
Client client = new Client();
//Start Server & Client
server.connectserver();
client.connectclient();
//Server wait for new connection
server.waitforclient();
//start the Streams
server.startstreams();
client.startstreams();
client.sendusername();
String msg1 =server.readSocket();
client.sendpassword();
String msg2 =server.readSocket();
client.sendaccess();
int n = getaccess();
Your code is full of nonsense. You do things like
breader = new BufferedReader (reader);
public String readSocket() throws IOException
{
breader = new BufferedReader(reader);
and like
w = new PrintWriter(out, true);
public void writeSocketMyJson(String n) throws IOException
{
w = new PrintWriter(out, true);
and like
password = new String();
password = readSocket();
I would suggest that you just throw it all away and begin from scratch, with some sample code for java sockets, of which there exist numerous on the interwebz.
The thing to remember is that you cannot just pretend that a socket is a regular stream, and you cannot just create a BufferedReader on top of it. At any given moment you need to know exactly how many bytes were sent, and therefore exactly how many bytes to expect to receive.
So generally, on the sending side you cannot write a string and expect on the receiving side the end of the string to be detected with an end-of-line. On the sending side you need to first write the length of the string, as a 32-bit int, so on the receiving side you know you are expecting exactly 4 bytes that contain the length of the string that follows. Then, you can read the exact right number of bytes that make up the string. Of course, do not forget that in java, a character is 2 bytes long.

Null pointer exception occurs while trying to read repeatedly from an InputStream

The code works fine when I close the client just after sending one instruction. But when I want a client and server connection to persist, so that the client can send multiple instructions to the server one after another, I get a Null pointer exception at the server and the message java.net.SocketException: Socket is closed at the client. This happens after the client sends a file to the server and the server successfully receives it. Need help. The error occurs at the Connection class code line switch(clientMsg). It seems to me that for some reason the BufferedReader in goes null, but I might be mistaken about that. The code is as follows. Thanks.
Server
public class server {
private static ServerSocket serverSocket;
private static Socket socket = null;
public static void print(Object s) {
System.out.println(s);
}
#SuppressWarnings("resource")
public static void main (String args[]) throws IOException {
System.out.print("Specify listening port: ");
Scanner _a = new Scanner(System.in);
int a = _a.nextInt();
try{
serverSocket = new ServerSocket(a);
}
catch(IOException e) {
System.out.println(e);
}
while (true) {
try {
socket = serverSocket.accept();
print("Connected to " + socket);
Thread client = new Thread(new Connection(socket));
client.start();
}
catch (IOException e) {
print(e);
}
}
}
}
Connection
public class Connection implements Runnable {
public static void print(Object s) {
System.out.println(s);
}
private Socket socket;
private BufferedReader in = null;
public Connection(Socket client) {
this.socket = client;
}
#Override
public void run(){
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String clientMsg;
while (true) {
clientMsg = in.readLine();
switch (clientMsg) {
case "1":
receiveFile(); //method code not included
break;
default:
print("Command not recognized");
break;
}
//in.close();
}
}//try run()
catch (IOException e) {
print(e);
}
}
Client
public class client {
private static Socket connectToServer;
private static String fileName;
private static BufferedReader keybrdIn;
private static PrintStream msgToServer;
public static void println(Object e) {
System.out.println(e);
}
public static void print(Object e) {
System.out.print(e);
}
public static void main(String args[]) throws IOException{
try{
print("Enter IP: ");
String ip = new Scanner(System.in).nextLine();
print("Enter port: ");
int port = new Scanner(System.in).nextInt();
connectToServer = new Socket(ip, port);
keybrdIn = new BufferedReader(new InputStreamReader(System.in));
}catch(IOException e) {
println(e);
}
msgToServer = new PrintStream(connectToServer.getOutputStream());
while (true) {
try {
switch(Integer.parseInt(action())) { //action() method code not included
case 1:
msgToServer.println("1");
sendFile();
break;
default:
println("Invalid input");
break;
}
}catch (IOException e) {
println(e);
}
}
}
sendFile()
public static void sendFile() throws IOException {
print("Enter file name: ");
fileName = keybrdIn.readLine();
File file = new File(fileName);
byte[] bytearray = new byte[8192];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
OutputStream os = connectToServer.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
int count;
while ((count = dis.read(bytearray)) > 0){
dos.write(bytearray, 0, count);
}
dis.close();
dos.flush();
dos.close();
}
receiveFile()
public void receiveFile() {
try {
int count;
DataInputStream clientFileStream = new DataInputStream(socket.getInputStream());
String fileName = clientFileStream.readUTF();
OutputStream fileOutput = new FileOutputStream("_" + fileName);
byte[] mybytearray = new byte[8192];
BufferedOutputStream bos = new BufferedOutputStream(fileOutput);
System.out.println("Downloading " + fileName + " ...");
//outToClient().writeBytes("Uploading. Please wait...\n");
while ((count = clientFileStream.read(mybytearray)) > 0){
bos.write(mybytearray, 0, count);
}
fileOutput.close();
bos.close();
clientFileStream.close();
}
catch (IOException e) {
print(e);
}
}
In sendFile(), you close the data output stream which closes your underlying connection's output stream.
According to the documentation of Socket.getOutputStream():
"Closing the returned OutputStream will close the associated socket".
Since you already closed stream, it will also close socket as well as Eyal mentioned. However, at the moment you close the stream, server side will aware of that and return -1 for read() results.
So, even if you didn't specify file length at beginning, this will generally works well.
However, since you already closed stream, you can't reuse it no matter what. To fix this issue, probably you need to change your Client class so that Client should create socket connection, send files, close socket. That's one lifecycle of opened client socket.
Or maybe in while loop of Client class, 1) take ip, port, and filename to send 2) Create new Thread and provide those information so let thread open connection, send file, close connection 3) and in the meantime, client while() can keep take next ip, port, and filename to send from the user. By doing this, you don't need to make client program wait until file transfer to be completed.
Regarding the NPE in the server, readLine() returns null at end of stream. You are ignoring it. You should be testing for it immediately after the call, and if null close the socket and exit the read loop.

Java - cannot write to a second file

I've been having some trouble trying to send a file from a server to a client. I can't seem to send the same file from the server to the client into two SEPERATE files. Instead, it just appends to the end of the first file! Any help would be appreciated.
EDIT : I've modified the code. I've modularized the file sending and recieving tasks into 2 functions 'sendfile' and 'recievefile'. The error I'm getting now is that the socket is closed at the sendfile and recievefile functions after the second call. But all I'm closing is the file and the output and input streams! Perhaps closing those streams will close the socket too...? Anyways, I've tried NOT closing the input and output streams and what happens is - 1)nothing gets transferred to the destination file. So I just get a blankly created file at the server end. 2)The second file doesn't even get created. Great.
Any help would, as usual, be appreciated.
Server:
package com.http.server;
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])throws Exception
{
System.out.println("Server running...");
/* Listen on port 5555 */
ServerSocket server = new ServerSocket(5555);
/* Accept the sk */
Socket sk = server.accept();
System.out.println("Server accepted client");
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
/* Read the filename */
String serverlocation = "C:/Users/Arjun/Desktop/CNW model/HTTP Website/";
String filename = serverlocation + inReader.readLine();
if ( !filename.equals("") ){
/* Reply back to client with READY status */
outReader.write("READY\n");
outReader.flush();
}
sendfile(sk, filename);
sendfile(sk, filename);
outReader.close();
inReader.close();
sk.close();
server.close();
}
public static void sendfile(Socket sk, String filename)
{
try{
OutputStream output = sk.getOutputStream();
FileInputStream file = new FileInputStream(filename);
byte[] buffer = new byte[sk.getSendBufferSize()];
int bytesRead = 0;
while((bytesRead = file.read(buffer))>0)
{
output.write(buffer,0,bytesRead);
}
file.close();
output.close();
}
catch (Exception ex){
System.out.println(ex);
}
}
}
Client:
package com.http.client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField txtFile;
public static void main(String args[]){
/* Create and display the client form */
Client clientForm = new Client();
clientForm.Display();
}
public void Display(){
JFrame frame = new JFrame();
frame.setTitle("Client");
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
JLabel lblFile = new JLabel("URL:");
txtFile = new JTextField();
txtFile.setPreferredSize(new Dimension(150,30));
JButton btnTransfer = new JButton("Get");
btnTransfer.addActionListener(this);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
mainPanel.add(lblFile);
mainPanel.add(txtFile);
mainPanel.add(btnTransfer);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/* File Open Dialog box allows the user to select a file */
String filename=txtFile.getText();
try{
/* Try to connect to the server on localhost, port 5555 */
Socket sk = new Socket("localhost", 5555);
/* Send filename to server */
OutputStreamWriter outreader = new OutputStreamWriter(sk.getOutputStream());
outreader.write(filename + "\n");
outreader.flush();
/* Get response from server */
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String serverStatus = inReader.readLine(); // Read the first line
/* If server is ready, receive the file */
while ( !serverStatus.equals("READY") ){}
/* Create a new file in the tmp directory using the filename */
recievefile(sk,filename);
recievefile(sk,"xx"+filename);
JOptionPane.showMessageDialog(this, "Transfer complete");
inReader.close();
outreader.close();
sk.close();
}
catch (Exception ex){
/* Catch any errors */
JOptionPane.showMessageDialog(this, ex.getMessage());
System.out.println(ex);
}
}
public void recievefile(Socket sk, String filename)
{
try{
InputStream input = sk.getInputStream();
FileOutputStream wr = new FileOutputStream(new File("C:/tmp/" + new File(filename).getName()));
byte[] buffer = new byte[sk.getReceiveBufferSize()];
int bytesReceived = 0;
while((bytesReceived = input.read(buffer))>0)
{
/* Write to the file */
wr.write(buffer,0,bytesReceived);
}
wr.close();
input.close();
}
catch (Exception ex){
/* Catch any errors */
JOptionPane.showMessageDialog(this, ex.getMessage());
System.out.println(ex);
}
}
}
When you send the files, you are just appending one to the end of the other so thats how the client see them.
You need to let the client know when to stop reading one file and start reading the second.
The simplest approach is to send the length of the file before the file and only read exactly than amount of data.
BTW: You cannot combine binary and text streams the way you have. This is more likely to lead to confusion. In your case you need to send binary so make everything binary with DataInputStream and DataOutputStream.
What I had in mind is something like.
public static void sendFile(DataOutput out, String filename) throws IOException {
FileInputStream fis = new FileInputStream(filename);
byte[] bytes = new byte[8192];
try {
long size = fis.getChannel().size();
out.writeLong(size);
for (int len; (len = fis.read(bytes, 0, (int) Math.min(bytes.length, size))) > 0; ) {
out.write(bytes, 0, len);
size -= len;
}
assert size == 0;
} finally {
fis.close();
}
}
public void receiveFile(DataInput in, String filename) throws IOException {
long size = in.readLong();
FileOutputStream fos = new FileOutputStream(filename);
byte[] bytes = new byte[8192];
try {
for (int len; (len = (int) Math.min(size, bytes.length)) > 0; ) {
in.readFully(bytes, 0, len);
fos.write(bytes, 0, len);
size -= len;
}
} finally {
fos.close();
}
}

Sending file from client to server and do not lost connection, is it possible?

I would like to send file from client to server and be able do it again in the future.
So my client connect to server and upload file, ok - it works but it hangs at the end..
so here is my code in client, the server side is quite similar.
private void SenderFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
IoUtil.copy(fis, os);
} catch (Exception ex) {
ex.printStackTrace();
}
}
IoUtils found on Stack :)
public static class IoUtil {
private final static int bufferSize = 8192;
public static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[bufferSize];
int read;
while ((read = in.read(buffer, 0, bufferSize)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
}
}
Explanation: my client has a socket connected to server, and I send any file to him.
My server download it but hangs at the end because he is listening for more infromation.
If I choose another file, my server will download new data to the existing one.
How could I upload any file to server, make my server work on and be able download another one file properly?
ps. If I add to ioutil.copy at the end of function out.close my server will work on but the connection will be lost. I do not know what to do :{
After update:
Client side:
private void SenderFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
DataOutputStream wrapper = new DataOutputStream(os);
wrapper.writeLong(file.length());
IoUtil.copy(fis, wrapper);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Server side (thread listening for any message from client):
public void run() {
String msg;
File newfile;
try {
//Nothing special code here
while ((msg = reader.readLine()) != null) {
String[] message = msg.split("\\|");
if (message[0].equals("file")) {//file|filename|size
String filename = message[1];
//int filesize = Integer.parseInt(message[2]);
newfile = new File("server" + filename);
InputStream is = socket.getInputStream();
OutputStream os = new FileOutputStream(newfile);
DataInputStream wrapper = new DataInputStream(is);
long fileSize = wrapper.readLong();
byte[] fileData = new byte[(int) fileSize];
is.read(fileData, 0, (int) fileSize);
os.write(fileData, 0, (int) fileSize);
System.out.println("Downloaded file");
} else
//Nothing special here too
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Ok, now I can download file - still once, another one is downloaded but unable to read. For example, second time I want send by client a file.png. I got it on server, but this file is not possible to view.
Thanks in advance :)
You need to make your server able to differentiate files. The easiest way is to tell in advance how many bytes the receiving end should expect for a single file; this way, it knows when to stop reading and wait for another one.
This is what the SenderFile method could look like:
private void SenderFile(File file)
{
try
{
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
DataOutputStream wrapper = new DataOutputStream(os);
wrapper.writeLong(file.length());
IoUtil.copy(fis, wrapper);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
And this is what the ReceiveFile method could look like:
// the signature of the method is complete speculation, adapt it to your needs
private void ReceiveFile(File file)
{
FileOutputStream fos = new File(file);
InputStream is = socket.getInputStream();
DataInputStream wrapper = new DataInputStream(is);
// will not work for very big files, adapt to your needs too
long fileSize = wrapper.readLong();
byte[] fileData = new byte[fileSize];
is.read(fileData, 0, fileSize);
fos.write(fileData, 0, fileSize);
}
Then don't close the socket.

Categories