i am trying to develop an application to transfer large files over a network.Am trying to implement the same splitting files into chunks
how to split a large file into chunks of 50kb each?
And how to put it back to original on basis of hashcode( for error control)?
i know this is not really what you asked for but you can transfer large files like this
Sender :
try
{
ss = new ServerSocket(9244); // try to open ServerSocket
ready =true;
}
catch(Exception e)
{
}
Socket bs = ss.accept(); // reciever joins
OutputStream out = bs.getOutputStream();
fis = new FileInputStream(f); // you open fileinputstream of the file you want to send
int x = 0;
while (true) {
x = fis.read();
if (x == -1) { // until fis.read() doesn't return -1 wich means file is completly read write bytes out
break;
}
out.write(x);
}
fis.close();
out.close();
bs.close();
ss.close();
Reciever:
try {
Socket socket = new Socket("127.0.0.1",9244); // connect to server
InputStream in = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(f);
int x = 0;
while (true) {
x = in.read();
if (x == -1) { // write data into file until in.read returns "-1"
break;
}
fos.write(x);
}
in.close();
fos.close();
socket.close();
hope this was a little helpful
Related
I am learning sockets and now I want to write file transfer program. I have server part and client part. Server part contains 2 ports: 5000 (commands) and 5001 (files). Now I want to send a file via socket and when I did something is wrong because only 425B of data is sending.
Here is client send method:
private void sendFile(Socket socket) {
File file2 = new File("C:\\Users\\barte\\Desktop\\dos.png");
byte[] bytes = new byte[16 * 1024];
System.out.println(file2.exists());
try (InputStream inputStream = new FileInputStream(file2);
OutputStream outputStream = socket.getOutputStream();
OutputStream secondOutput = new FileOutputStream("C:\\Users\\barte\\Desktop\\received\\dos.png")) {
int count;
while ((count = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, count);
secondOutput.write(bytes, 0, count);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
As you can see (image below) I am writing this file also locally and everything is ok, all of 73KB of data is writed.
Now, on server side I am trying to receive this file:
case SEND: {
new Thread(() -> {
printWriter.println("Server is receiving files right now...");
try (ServerSocket serverSocket = new ServerSocket(5001)) {
while (true) {
new FilesTransfer(serverSocket.accept()).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
break;
}
And inside FilesTransfer run method:
#Override
public void run() {
System.out.println("Hello there");
try {
InputStream inputStream = inSocket.getInputStream();
OutputStream outputStream = new FileOutputStream("C:\\Users\\barte\\Desktop\\received\\file");
byte[] bytes = new byte[16 * 1024];
int count;
while ((count = inputStream.read()) > 0) {
outputStream.write(bytes, 0, count);
}
outputStream.close();
inputStream.close();
inSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Where is a bug? Why only empty bytes are sending when locally everything it's fine?
The problem is:
while ((count = inputStream.read()) > 0) {
Your code uses InputStream.read(), which reads individual bytes (or -1 when end-of-stream). Right now, you are reading individual bytes, interpreting that as a length, and then writing that number of 0x00 bytes from bytes to the file. This stops when you read a 0x00 byte from the stream.
You need to change this to use InputStream.read(byte[]):
while ((count = inputStream.read(bytes)) != -1) {
That is, you need to pass bytes in, and check for the result being unequal to -1, not if it is greater than zero (0), although read(byte[]) will only return 0 if the passed in byte array has length zero, so that is not a real concern.
You could do it in this way:
#Override
public void run() {
System.out.println("Hello there");
try {
InputStream inputStream = inSocket.getInputStream();
OutputStream outputStream = new FileOutputStream("C:\\Users\\barte\\Desktop\\received\\file");
byte[] bytes = new byte[16 * 1024];
int byteRead= 1;
while (byteRead > -1) {
byteRead= inputStream.read();
outputStream.write(byteRead);
}
outputStream.close();
inputStream.close();
inSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Actually END OF FILE or EOF means -1 and you did > 0 so 0 was taken and it stopped the connection saving the file.
I also recommend to write a logic to transfer the filename as a command to the server so that the file is saved with the correct name and extension!
I am just trying to send some files from a socket and i am able to send those files without any interruption: also whether the size file is small or large that does not matter it sends like a charm.
But the problem in my case that is arising is the file that i sent is being corrupted, i.e. it is not playing like audio or video. I have already gone through this but it did not helped.
The code that I am using is below.
Server Side:
File file = new File(
Environment.getExternalStorageDirectory(),
"testingFile.mp4");
byte[] mybytearray = new byte[4096];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
OutputStream os;
DataOutputStream dos = null;
try {
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
dos.writeLong(mybytearray.length);
int read;
while ((read = dis.read(mybytearray)) != -1) {
dos.write(mybytearray, 0, read);
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
And the Client Side :
File file = new File(
Environment.getExternalStorageDirectory(),
"TEST SUCCESS.mp4");
InputStream in = null;
int bufferSize;
try {
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.println(fileName);
OutputStream output = new FileOutputStream(
file);
byte[] buffer = new byte[bufferSize];
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
Thanks in advance.
So after the conversations in comments and as #MarquisofLorne told to delete the line that i have written in my server side code. i.e either delete this line from server side code:
dos.writeLong(mybytearray.length);
or write this below line code in client side code:
long sizeOfFile = clientData.readLong();
It solves the problem.
Server Side
Your code sends buffer length(4096), which is a bug.
It should send file length.
File file = new File( ... );
try {
//dos.writeLong(mybytearray.length);
dos.writeLong(file.length());
}
Client Side
Server sends two meta data
file name( F bytes, encoded by utf-8)
file length (8 bytes)
And then sends entire contents( N bytes)
But client code ignores file length(8bytes), just reads file name and contents N bytes
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF(); // ok read F bytes
// missing readLong(..) 8 bytes
// long fileLen = clientData.readLong(); <= read file length before reading contents
// read N bytes, but first 8 bytes are file length, which are written into file.
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
Don't rely on -1
Your codes keep relying on -1 in while loop
while ((read = dis.read(mybytearray)) != -1) {
dos.write(mybytearray, 0, read);
}
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
-1 means abnormal state.
Because server knows the exact size of a file and writes out the file, client should read the exact length of bytes from stream.
If server send 1234 bytes, when client read -1 from clientData.read(..), it fails to read contents from stream, not end of stream.
How can I convert Binary PGM Files to ASCII PGM Files using Java?
When I use the following code, I am unable to write ASCII values in the B.pgm. I've tried using dos.writeInt also.
FileInputStream inRaw = null;
FileOutputStream outRaw = null;
try {
inRaw = new FileInputStream("A.pgm");
outRaw = new FileOutputStream("B.pgm");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream dis = new DataInputStream(inRaw);
DataOutputStream dos = new DataOutputStream(outRaw);
int i = 0;
String temp = null;
temp = dis.readLine();
System.out.println(temp);
dos.writeBytes("P2");
dos.writeBytes("\n");
while(i < 3){
temp = dis.readLine();
System.out.println(temp);
dos.writeBytes(temp);
dos.writeBytes("\n");
i++;
}
int t = 0;
while(dis.available() != 0){
t = dis.read();
System.out.println(t);
fileWriter.write(t);
dos.writeInt(t);
dos.writeBytes("\n");
}
dis.close();
dos.close();
I tried to use FileWriter instead of DataOutputStream and the code produces an empty file, I can't figure out why?
FileInputStream inRaw = null;
FileOutputStream outRaw = null;
try {
inRaw = new FileInputStream("A.pgm");
outRaw = new FileOutputStream("B.pgm");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream dis = new DataInputStream(inRaw);
FileWriter fileWriter = new FileWriter("B.pgm");
int i = 0;
String temp = null;
temp = dis.readLine();
System.out.println(temp);
fileWriter.write("P2");
fileWriter.write("\n");
while(i < 3){
temp = dis.readLine();
System.out.println(temp);
fileWriter.write(temp);
fileWriter.write("\n");
i++;
}
int t = 0;
while(dis.available() != 0){
t = dis.read();
System.out.println(t);
fileWriter.write(t);
fileWriter.write(t);
fileWriter.write("\n");
}
dis.close();
(This might not solve your issue, but it will make your code much more robust).
One of the possible reasons why data isn't written to a file is that you don't actually close the stream - it can be held in an in-memory buffer until then, and is never written out. You certainly don't make an explicit call to fileWriter.close().
You can try to close the streams manually, but it's normally unnecessary, and surprisingly tricky to do it correctly in all circumstances.
It is easier to use try-with-resources - this automatically manages your streams, and guarantees they are closed by the end of the block.
try (FileInputStream inRaw = new FileInputStream("A.pgm");
FileOutputStream outRaw = new FileOutputStream("B.pgm");
DataInputStream dis = new DataInputStream(inRaw);
DataOutputStream dos = new DataOutputStream(outRaw)) {
// The logic to read dis and write it to dos.
} catch (IOException e) {
e.printStackTrace();
}
// No need to close the streams - that happened automatically.
You can do similarly with the second version by substituting the FileWriter fileWriter = new FileWriter(outRaw) for DataOutputStream dos = ....
If this doesn't solve the immediate issue, I would suggest that there is an error in your logic. Try stepping through the code with a debugger - make sure that the write calls are actually being performed.
I am using Java.net at one of my project.
and I wrote a App Server that gets inputStream from a client.
But some times my (buffered)InputStream can not get all of OutputStream that client sent to my server.
How can I write a wait or some thing like that, that my InputStream gets all of the OutputStream of client?
(My InputStream is not a String)
private Socket clientSocket;
private ServerSocket server;
private BufferedOutputStream outputS;
private BufferedInputStream inputS;
private InputStream inBS;
private OutputStream outBS;
server = new ServerSocket(30501, 100);
clientSocket = server.accept();
public void getStreamFromClient() {
try {
outBS = clientSocket.getOutputStream();
outputS = new BufferedOutputStream( outBS);
outputS.flush();
inBS = clientSocket.getInputStream();
inputS = new BufferedInputStream( inBS );
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks.
The problem you have is related to TCP streaming nature.
The fact that you sent 100 Bytes (for example) from the server doesn't mean you will read 100 Bytes in the client the first time you read. Maybe the bytes sent from the server arrive in several TCP segments to the client.
You need to implement a loop in which you read until the whole message was received.
Let me provide an example with DataInputStream instead of BufferedinputStream. Something very simple to give you just an example.
Let's suppose you know beforehand the server is to send 100 Bytes of data.
In client you need to write:
byte[] messageByte = new byte[1000];
boolean end = false;
String dataString = "";
try
{
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
while(!end)
{
int bytesRead = in.read(messageByte);
dataString += new String(messageByte, 0, bytesRead);
if (dataString.length == 100)
{
end = true;
}
}
System.out.println("MESSAGE: " + dataString);
}
catch (Exception e)
{
e.printStackTrace();
}
Now, typically the data size sent by one node (the server here) is not known beforehand. Then you need to define your own small protocol for the communication between server and client (or any two nodes) communicating with TCP.
The most common and simple is to define TLV: Type, Length, Value. So you define that every message sent form server to client comes with:
1 Byte indicating type (For example, it could also be 2 or whatever).
1 Byte (or whatever) for length of message
N Bytes for the value (N is indicated in length).
So you know you have to receive a minimum of 2 Bytes and with the second Byte you know how many following Bytes you need to read.
This is just a suggestion of a possible protocol. You could also get rid of "Type".
So it would be something like:
byte[] messageByte = new byte[1000];
boolean end = false;
String dataString = "";
try
{
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
int bytesRead = 0;
messageByte[0] = in.readByte();
messageByte[1] = in.readByte();
int bytesToRead = messageByte[1];
while(!end)
{
bytesRead = in.read(messageByte);
dataString += new String(messageByte, 0, bytesRead);
if (dataString.length == bytesToRead )
{
end = true;
}
}
System.out.println("MESSAGE: " + dataString);
}
catch (Exception e)
{
e.printStackTrace();
}
The following code compiles and looks better. It assumes the first two bytes providing the length arrive in binary format, in network endianship (big endian). No focus on different encoding types for the rest of the message.
import java.nio.ByteBuffer;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Test
{
public static void main(String[] args)
{
byte[] messageByte = new byte[1000];
boolean end = false;
String dataString = "";
try
{
Socket clientSocket;
ServerSocket server;
server = new ServerSocket(30501, 100);
clientSocket = server.accept();
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
int bytesRead = 0;
messageByte[0] = in.readByte();
messageByte[1] = in.readByte();
ByteBuffer byteBuffer = ByteBuffer.wrap(messageByte, 0, 2);
int bytesToRead = byteBuffer.getShort();
System.out.println("About to read " + bytesToRead + " octets");
//The following code shows in detail how to read from a TCP socket
while(!end)
{
bytesRead = in.read(messageByte);
dataString += new String(messageByte, 0, bytesRead);
if (dataString.length() == bytesToRead )
{
end = true;
}
}
//All the code in the loop can be replaced by these two lines
//in.readFully(messageByte, 0, bytesToRead);
//dataString = new String(messageByte, 0, bytesToRead);
System.out.println("MESSAGE: " + dataString);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
You can read your BufferedInputStream like this. It will read data till it reaches end of stream which is indicated by -1.
inputS = new BufferedInputStream(inBS);
byte[] buffer = new byte[1024]; //If you handle larger data use a bigger buffer size
int read;
while((read = inputS.read(buffer)) != -1) {
System.out.println(read);
// Your code to handle the data
}
int c;
String raw = "";
do {
c = inputstream.read();
raw+=(char)c;
} while(inputstream.available()>0);
InputStream.available() shows the available bytes only after one byte is read, hence do .. while
I was playing around with Java sockets and I was trying to trasnfer files from a server to client, however, when they get transfer they are corrupted. This is the code from the server:
DataInputStream input;
DataOutputStream ouput;
//these two variable are initialized somewhere else in the code.
private void downloadFile() {
try {
String fileName= input.readUTF();
File f = new File(path + fileName);
size= f.length();
file= new FileInputStream(path+ fileName);
ouput.writeLong(size);
byte[] buffer = new byte[1024];
int len;
while ((len = file.read(buffer)) > 0) {
output.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
on the client side:
public void downloadFile(String fileName) {
try {
this.client= new Socket(ip,port);
DataInputStream input= new DataInputStream(this.client.getInputStream());
DataOutputStream ouput= new DataOutputStream(this.client.getOutputStream());
output.writeUTF("DOWNLOAD");
output.writeUTF(fileName);
File f = new File(path+ fileName);
file = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > 0) {
file.write(buffer, 0, len);
}
file.flush();
file.close();
this.client.close();
} catch (Exception e) {
System.out.println("something went wrong");
}
}
I dont know what am I doing wrong, the file gets completely transfer but not correctly.
on the server:
ouput.writeLong(size);
you dont seem to handle this on the client side, you just append it to the downloaded file as if it was part of the binary data.
It looks like you send the length of the file from the server to the client:
ouput.writeLong(size);
but your client code never does anything with the transmitted size, so it takes up the first few bytes of the file.