I have written a program to transfer a file from client side to server side. The program runs without any errors. Also the file is created on the destination path. But when i try to open the file the gedit crashes. The program should transfer audio,video,text file.I know UDP should not be used to transfer file as it may be corrupted but i have to do it as assignment. Following code is for text file transfer.
Server.java
import java.net.*;
import java.io.*;
public class server
{
public static void main(String args[])throws IOException
{
DatagramSocket dsoc=new DatagramSocket(8008);
byte b[]=new byte[787734];
byte c[];
String pac ;
File file = new File("/home/san_16398/ip2.java");
FileOutputStream f = new FileOutputStream(file);
while(true)
{
DatagramPacket dp=new DatagramPacket(b,b.length);
dsoc.receive(dp);
pac =new String(dp.getData(),0,dp.getLength());
System.out.println(pac);
}
pac.getBytes();
f.write(b);
f.flush();
f.close();
}
}
Client.java
import java.net.*;
import java.io.*;
public class client
{
private static final int BUFFER_SIZE = 4096;
public static void main(String args[])throws Exception
{
byte b[]=new byte[787734];
FileInputStream f=new FileInputStream("/home/san_16398/ip.java");
DatagramSocket dsoc=new DatagramSocket(2000);
int i=0;
byte[] buffer = new byte[BUFFER_SIZE];
while(f.read(buffer) != -1)
{
b[i]=(byte)f.read();
i++;
}
f.close();
dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),8008));
}
}
byte b[]=new byte[787734];
// ...
while(f.read(buffer) != -1){
b[i]=(byte)f.read();
i++;
}
dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),8008));
This is complete and utter nonsense. You're reading up to 787734 bytes and throwing them away, and then reading one byte, if there is one, and then apparently attempting to send a datagram of size up to 787734, which is impossible. It should be:
byte b[]=new byte[534];
// ...
int count;
while((count = f.read(b)) != -1){
dsoc.send(new DatagramPacket(b,count,InetAddress.getLocalHost(),8008));
}
and then you have to take care of duplicate datagrams, datagrams not received, datagrams received out of order, ...
And calling File.exists() and File.createNewFile() when you're using a FileOutputStream is a complete waste of time at best, but when you call them after creating the FileOutputStream you are liable to clobber the file that is being written to, depending on the platform. Remove.
Here:
while(f.read(buffer) != -1){
b[i]=(byte)f.read();
first you are reading into your buffer object - to then never make use of the buffer array
and then you read a single byte again
to then read into the buffer again
Long story short: either use the read(buffer) or use the single byte read() consistently.
And beyond that: you also forgot to check that the file you intend to read fits into the buffer that you want to transmit to the server!
Finally: you are also trying to do *too many** things at once. Instead: focus on the different parts of this work, like:
first write code that reads a file
then enhance that to create a local copy of that file
and then, when that works, instead of creating a local copy, send to a remote server.
Related
Env: Windows 7, java 1.8, default OS encodings
I'm trying to read a byte stream of currency market data from a socket to a file, and then play that file back to simulate the market over a fixed period; however, the file has a few malformed bytes, seemingly at random.
Below, I outline the problem with metacode, where the notation "..." indicates skipped irrelevant or boilerplate code.
Bytes are coming over the socket, and I'm reading them with a non-blockingNIO selector, then writing to disk via BufferedOutputStream:
class SocketReadDiskWrite implements Runnable{
...
blobWriter = new BufferedOutputStream(new FileOutputStream(blobFileName));
sc = SocketChannel.open(addr)
sc.configureBlocking(false);
And then in the run() method, when the selector deems the socket readable,
public void run(){
...
while(keyIterator.hasNext())
{
SelectionKey key = keyIterator.next();
if (key.isReadable()) {
if(bytesRead == -1)
{
connected = false;
logger.warn("no bytes to read");
break;
}
readBuffer.flip();
// Write bytes from socket to file, then rewind and process data
while (readBuffer.hasRemaining()){
byte[] b = new byte[readBuffer.remaining()];
readBuffer.get(b);
blobWriter.write(b);
}
readBuffer.rewind();
processData(readBuffer); //<-- Further processing
...
}
The processData method works fine when reading from a live stream of the market. For example, maybe processData reads a list of currencies and prints them, and the output is,
`EUR.USD.SPOT, EUR.AUD.SPOT, ..<thousands more>.. AUD.CAD.SPOT`
However, if I instead try to play back the captured bytestream (ie. Read in the contents of the file that was just previously created), on occasion, a corrupt symbol appears,
`EUR.USD.SPOT, EUR.AUD.SPOT, ..<thousands more>.. AUD.C##$###X`
Looking at the file in notepad++, indeed I find incorrect bytes (blue = correct symbols, red = malformed).
Subsequently, when the application points to the bytefile reader (instead of live market), the app fails at exactly these lines, throwing errors like Invalid symbol: EUR.-XD##O##$.
For what it's worth, this is how I playback the file by reading it from disk and streaming to socket:
class FilePlayer implements runnable (Socket clientSocket) {
clientWriter= clientSocket.getOutputStream();
blobReader = new FileInputStream(blobFileName);
byte[] dataArray = new byte[1024]; //<-- Store 1024 bytes data at a time
...
}
public void run() {
while(true){
blobReader.read(dataArray); //<-- Read 1024 bytes of data from disk
clientWriter.write(dataArray); //<-- Write 1024 bytes of data to socket
}
}
Note, I recently opened a related thread similar thread, but that was in regard to FileChannels, which were actually not the culprit. Figured that discussion had deviated enough to warrant a fresh post.
I'm trying to make a simple transfer of a text .txt file from client to server, and no matter how much I think I know, and understand what I'm doing, and what exactly happening, I always get it wrong. I can really use some help here please.
So, this is the code, two function that transfer a .txt file from one to another:
Client side:
private void sendFileToServer(String file_name) throws IOException {
File file=new File(file_name);
int file_size=(int)file.length();
byte[] bytes=new byte[file_size];
FileInputStream os=null;
try {
os = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("The file "+file+" wasn't found");
return;
}
BufferedInputStream bos=new BufferedInputStream(os);
bos.read(bytes);
output.write(bytes,0,bytes.length);
/* 'output' is a PrintStream object, that holds the output stream
* for the client's socket, meaning:
* output=new PrintStream(client_socket.getOutputStream()); */
output.flush();
bos.close();
}
this will buffer everything into BufferedInputStream, will copy it to bytes and will then send it to the other side - the server.
Server side:
public static String receiveFileFromClient(Client client) throws IOException {
int buffer_size=client.getSocket().getReceiveBufferSize();
byte[] bytes=new byte[buffer_size];
FileOutputStream fos=new FileOutputStream("transfered_file.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataInputStream in=client.getInputStream();
int count;
System.out.println("this will be printed out");
while ((count=in.read(bytes))>0) { // execution is blocked here!
bos.write(bytes, 0, count);
}
System.out.println("this will not be printed");
bos.flush();
bos.close();
return "transfered_file.txt";
}
My intention here is to keep reading bytes from the client (the while loop), until the other side (the client) have no more bytes to send, and this is where in.read(bytes) should return 0 and the loop should break, but this is never happens, it just get blocked, even though all the bytes from the client's input-stream were successfully transferred!
Why doesn't the loop breaks?
From Javadoc:
If no byte is available because the stream is at end of file, the
value -1 is returned
doesn't the last byte is considered "end of file"? I made sure that the function sendFileToServer properly writes the entire file to the output instance (PrintStream object) and returns.
Any help would be appreciated.
As i understand it, the read() method will block until either it read[bytes] OR the socket is closed. So there is nothing for the read() what would indicate that it should stop reading, because it does not "understand" the file, its just some data.
A solution...
You could determine the number of bytes the client will send (on the client side) and then send the NUMBER over to the server. Now the server can process this number and knows how many bytes to read before the file is complete. So you can break the loop (or even don't use a loop) when the transfer is completed.
You could also process the data the server receives, and let the client send some "flag" after the file is complete, so the server knows when it is done. But this is more difficult, because you have to find something, that is not contained in the file-byte data
read() method will block for further input if you dont close the stream. So eather close the stream, or remove the loop and only read the number of bytes, you receive from the client
I am trying to send file from one client 1 to another Client 2 using an intermediate server. Both Client 1 and Client 2 are connected to server through network.For this, I have given IP-address of server to both clients.
But I am unable to transfer the file properly due to some mistake in my code. I am using the following code and its not working and at the Client 2 (receiver's) end, the file which is created is empty.
Kindly find the possible error in my code.
Server code
`
import java.net.*;
import java.io.*;
public class S1 {
public static void main(String[] args) {
try{
ServerSocket sc1=new ServerSocket(6988);
Socket c1=sc1.accept();
Socket c2=sc1.accept();
DataInputStream dis=new DataInputStream(c1.getInputStream());
int m=c1.getInputStream().available();
byte b2[]=new byte[m];
dis.read(b2);
DataOutputStream dos=new DataOutputStream(c2.getOutputStream());
dos.write(b2);
dos.flush();
dos.close();
}
catch(Exception e){}
}
}
Client 1 (Sender)
import java.io.*;
import java.net.*;
public class C11 {
public static void main(String[] args) {
try{
Socket c2=new Socket("127.0.0.1",6988);
FileInputStream fis=new FileInputStream("f:/abc.jpg");
File f1=new File("f:/abc.jpg");
long l1=f1.length();
int a=(int)l1;
byte b1[]=new byte[a];
DataInputStream dis=new DataInputStream(fis);
dis.read(b1);
DataOutputStream dout=new DataOutputStream(c2.getOutputStream());
dout.write(b1);
}
catch(Exception e){}
}
}
Client 2 (Receiver)
import java.io.*;
import java.net.*;
public class C22 {
public static void main(String[] args) {
try{
Socket c2=new Socket("127.0.0.1",6988);
DataInputStream dis=new DataInputStream(c2.getInputStream());
int m=c2.getInputStream().available();
byte b2[]=new byte[m];
dis.read(b2);
FileOutputStream fout=new FileOutputStream("E:\\PRACTICE\\xyz.txt");
DataOutputStream dos=new DataOutputStream(fout);
dos.write(b2);
dos.close();
}
catch(Exception e){}
}
}
Usual problems.
nt m=c1.getInputStream().available();
byte b2[]=new byte[m];
From the Javadoc: "It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream."
dis.read(b2);
The read() method returns a value. You are ignoring it. The value can be -1, or a postive number between 1 and the buffer size. You're assuming the read filled the buffer. It isn't obliged to do that.
dout.write(b1);
That should be
dout.write(b1, 0, count);
where count was the length returned by read(), and it should be in a loop:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Use this at both ends, with any buffer size greater than zero. I usually use 8192.
The most obvious mistake is here:
int m=c2.getInputStream().available();
The available method only tells you how much data can be read without blocking, not how much data could potentially be read from the socket. To read all of the data that was sent you need to write a loop that reads from the socket until read returns -1.
This question already has answers here:
Can't get out the receive file loop in send file using socket java
(2 answers)
Closed 9 years ago.
I am developing a simple file tranfer app between client and server.
Here is my code:
My Client(Sender):
try{
File file_sender=XMLParser.register(peername, address, port, listfile);
int count;
byte[] buffer = new byte[1024];
int len=(int) file_sender.length();
byte[] mybytearray = new byte[len];
DataOutputStream output=new DataOutputStream(Info.connection.getOutputStream());
output.writeInt(len);
System.out.println(len);
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file_sender));
bis.read(mybytearray, 0, len);
output.write(mybytearray, 0, len);
bis.close();
output.close();
// Info.connection.close();
}catch(Exception ex){
System.out.println(ex.toString());
}
My server(Receiver):
public class IOThread extends Thread {
private Socket connection;
private DataInputStream input;
public IOThread(Socket connection) throws IOException{
this.connection=connection;
input=new DataInputStream(connection.getInputStream());
}
#Override
public void run(){
while(true){
try{
int filesize=12022386;
int bytesRead;
int currentTot = 0;
byte[] bytearray = new byte [filesize];
int len=input.readInt();
FileOutputStream fos = new FileOutputStream("data.xml");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead=input.read(bytearray, 0,bytearray.length);
System.out.println(len);
currentTot=bytesRead;
do{
bytesRead=input.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead>=0) currentTot+=bytesRead;
System.out.println("pos: "+currentTot);
} while(len!=currentTot);
bos.write(bytearray, 0, currentTot);
bos.close();
//connection.close();
System.out.println("Done");
}catch(EOFException ex){
System.out.println(ex.toString());
break;
}catch( Exception ex){}
}
}
I want to tranfer more than one file, so I don't want to close socket. So, I use variable "len" to check the file is tranfered completely.
If I close "output" after send file then server thows EOFException and the file is sent success.
When I don't close output, server can't receive file success. But Server doesn't throws EOFException.
Can you help me solve this problem?
Update: Here is my output on the console screen if I don't close variable "output" at client:
246
pos: 496
Thanks.
Sorry for my poor English
Two things:
First of all, you seem to be ignoring the file length in your receiving code? You have:
int filesize=12022386;
int bytesRead;
int currentTot = 0;
byte[] bytearray = new byte [filesize];
int len=input.readInt();
You are sizing bytearray to 12022386 bytes regardless of the value of len, and you are asking for that many bytes from the input stream.
Secondly, when you close the output stream, any cached/buffered data that hasn't been written yet is automatically flushed (i.e., in your case, sent to the server).
Flush the output stream explicitly when you're done sending the first file to force it to finish sending all of the data to the server. You can then continue using that output stream for other things.
Do output.flush() in your sending code when the transfer is complete.
The problem is that you read beyond the size of your file, i.e. instead of reading len bytes, you read bytearray.length bytes, which is larger.
Consequently, you read more than len bytes, so len!=currentTot is never satisfied, because, as you can see from your sample output, currentTot == 496 and len == 246.
Make the following changes in your while loop:
do{
bytesRead=input.read(bytearray, currentTot, (len - currentTot));
if(bytesRead>=0) currentTot += bytesRead;
System.out.println("pos: " + currentTot);
} while(len != currentTot);
Just to make sure you don't end up in an endless loop due to a similar error, you could use currentTot < len instead of len != currentTot as your condition.
Also, since you're already using DataInputStream, consider reading the socket contents using readFully. This blocks until a given number of bytes is read from the socket, essentially eliminating your need for the while loop. Read more here.
Throw all the loops away and use DataInputStream.readFully(); close the socket after sending the file; don't s end the file length; and use a normal size buffer, say 8192 bytes.
Your BufferedOutputStream filling seems incorrect. Below code must be inside the while loop.
bos.write(bytearray, 0, currentTot);
Try something like this instead:
BufferedOutputStream buffOut=new BufferedOutputStream(fos);
byte []arr = new byte [1024 * 1024];
int available = -1;
while((available = buffIn.read(arr)) != -1) {
buffOut.write(arr, 0, available);
}
buffOut.flush();
buffOut.close();
And test again.
Edit: Updated my answer with #Jason's correct comment.
I'm trying to write an upload system for a fairly complex java server. I have reproduced the error in the two small programs listed below. Basically, I am using an ObjectOutputStream/ObjectInputStream to communicate via the client/server. This is a requirement; I have thousands of lines of code working perfectly fine around this ObjectOutputStream/ObjectInputStream setup, so I must be able to still use these streams after an upload is complete.
To access the files(the one being read on the client and the one being written on the server), FileInputStream and FileOutputStream is used. My client appears to be functioning perfectly; it reads in the file and sends a different byte array each iteration(it reads in 1MB at a time, so large files can be handled without overflowing the heap). However, on the server it appears as though the byte array is ALWAYS just the first array sent(the first 1MB of the file). This does not conform to my understanding of ObjectInputStream/ObjectOutputStream. I am seeking either a working solution to this issue or enough education on the matter to form my own solution.
Below is the client code:
import java.net.*;
import java.io.*;
public class stupidClient
{
public static void main(String[] args)
{
new stupidClient();
}
public stupidClient()
{
try
{
Socket s = new Socket("127.0.0.1",2013);//connect
ObjectOutputStream output = new ObjectOutputStream(s.getOutputStream());//init stream
//file to be uploaded
File file = new File("C:\\Work\\radio\\upload\\(Op. 9) Nocturne No. 1 in Bb Minor.mp3");
long fileSize = file.length();
output.writeObject(file.getName() + "|" + fileSize);//send name and size to server
FileInputStream fis = new FileInputStream(file);//open file
byte[] buffer = new byte[1024*1024];//prepare 1MB buffer
int retVal = fis.read(buffer);//grab first MB of file
int counter = 0;//used to track progress through upload
while (retVal!=-1)//until EOF is reached
{
System.out.println(Math.round(100*counter/fileSize)+"%");//show current progress to system.out
counter += retVal;//track progress
output.writeObject("UPACK "+retVal);//alert server upload packet is incoming, with size of packet read
System.out.println(""+buffer[0]+" "+buffer[1]+" "+buffer[2]);//preview first 3 bytes being sent
output.writeObject(buffer);//send bytes
output.flush();//make sure all bytes read are gone
retVal = fis.read(buffer);//get next MB of file
}
System.out.println(Math.round(100*counter/fileSize)+"%");//show progress at end of file
output.writeObject("UPLOAD_COMPLETE");//let server know protocol is finished
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
The following is my server code:
import java.net.*;
import java.io.*;
public class stupidServer
{
Socket s;
ServerSocket server;
public static void main(String[] args)
{
new stupidServer();
}
public stupidServer()
{
try
{
//establish connection and stream
server = new ServerSocket(2013);
s = server.accept();
ObjectInputStream input = new ObjectInputStream(s.getInputStream());
String[] args = ((String)input.readObject()).split("\\|");//args[0] will be file name, args[1] will be file size
String fileName = args[0];
long filesize = Long.parseLong(args[1]);
String upack = (String)input.readObject();//get upload packet(string reading UPACK [bytes read])
FileOutputStream outStream = new FileOutputStream("C:\\"+fileName.trim());
while (!upack.equalsIgnoreCase("UPLOAD_COMPLETE"))//until protocol is complete
{
int bytes = Integer.parseInt(upack.split(" ")[1]);//get number of bytes being written
byte[] buffer = new byte[bytes];
buffer = (byte[])input.readObject();//get bytes sent from client
outStream.write(buffer,0,bytes);//go ahead and write them bad boys to file
System.out.println(buffer[0]+" "+buffer[1]+" "+buffer[2]);//peek at first 3 bytes received
upack = (String)input.readObject();//get next 'packet' - either another UPACK or a UPLOAD_COMPLETE
}
outStream.flush();
outStream.close();//make sure all bytes are in file
input.close();//sign off
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
As always, many thanks for your time!
Your immediate problem is that ObjectOutputStream uses an ID mechanism to avoid sending the same object over the stream multiple times. The client will send this ID for the second and subsequent writes of buffer, and the server will use its cached value.
The solution to this immediate problem is to add a call to reset():
output.writeObject(buffer);//send bytes
output.reset(); // force buffer to be fully written on next pass through loop
That aside, you're misusing object streams by layering your own protocol on top of them. For example, writing the filename and filesize as a single string delimited by "|"; just write them as two separate values. Ditto for the number of bytes on each write.