Sending compressed JPG image over socket - java

I am trying to create a program with Java which takes a screenshot of the user's screen, compresses the image, and sends it to a server over sockets.
For some reason the image is saved corrupted in the end (it's unreadable). Can you maybe help me find what the problem could be?
CLIENT: (the screenshot is entered as a BufferedImage, and the returned byte array is then returned to the second function which sends it to the server)
public static byte[] compressImage(BufferedImage image) throws IOException {
System.out.println("starting compression");
ByteArrayOutputStream os = new ByteArrayOutputStream(37628);
float quality = 0.16f;
// create a BufferedImage as the result of decoding the supplied InputStream
// get all image writers for JPG format
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
//Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
// compress to a given quality
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
// appends a complete image stream containing a single image and
//associated stream and image metadata and thumbnails to the output
writer.write(null, new IIOImage(image, null, null), param);
os.flush();
return os.toByteArray();
}
public void uploadShot(byte[] imgData, String nickname) {
try {
/* Try to connect to the server on localhost, port 5555 */
Socket sk = new Socket("localhost", 23232);
OutputStream output = sk.getOutputStream();
/* Send filename to server */
OutputStreamWriter outputStream = new OutputStreamWriter(sk.getOutputStream());
outputStream.write(nickname + "\n");
outputStream.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, send the file */
if (serverStatus.equals("READY")){
int len = imgData.length;
int start = 0;
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= imgData.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
// Other checks if needed.
// May be better to save the streams in the support class;
// just like the socket variable.
OutputStream out = sk.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(len);
if (len > 0) {
dos.write(imgData, start, len);
}
dos.close();
output.close();
sk.close();
System.out.println("Transfer complete.");
}
} catch (Exception ex){
/* Catch any errors */
System.out.println(ex.getMessage());
}
}
SERVER: (the received image is saved to the folder mentioned with a timestamp)
public static void main(String args[]) throws Exception{
System.out.println("Server running...");
/* Listen on port 5555 */
ServerSocket server = new ServerSocket(23232);
/* Accept the sk */
Socket sk = server.accept();
System.out.println("Server accepted client");
InputStream input = sk.getInputStream();
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
/* Read the filename */
String nickname = inReader.readLine();
if ( !nickname.equals("") ){
/* Reply back to client with READY status */
outReader.write("READY\n");
outReader.flush();
}
String current = "/home/kasgel/screenshots";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy__HH:mm:ss");
Date timestamp = new Date();
File filename = new File(current + "/" + nickname + "-" + dateFormat.format(timestamp) + ".jpg");
if (!filename.exists()) {
filename.createNewFile();
}
FileOutputStream wr = new FileOutputStream(filename);
byte[] buffer = new byte[sk.getReceiveBufferSize()];
int bytesReceived = 0;
while((bytesReceived = input.read(buffer))>0) {
wr.write(buffer,0,bytesReceived);
}
wr.close();
}
And the error message which I get when opening the saved screenshot is the following:
display.im6: Not a JPEG file: starts with 0x00 0x03 `MyNick-30-03-2015__19:27:58.jpg' # error/jpeg.c/JPEGErrorHandler/316.

When you're writing your image, you first write a 32-bit signed integer containing the length in bytes of the image:
dos.writeInt(len);
if (len > 0) {
dos.write(imgData, start, len);
}
But when you're reading the image back, you don't read the length first; you're reading all the data (including the length) as if they were part of the image.
You have a second problem though, that will by itself also cause this problem. When you create a BufferedReader, and call readLine on it, it will read beyond the newline - it will read until its buffer is full. Which is not a problem if you keep reading from it, but after reading the line, you continue reading from the underlying InputStream, which will often have more bytes consumed from it after the newline.
The solution is: only use one abstraction to read/write data. In this case, the easiest is absolutely to use DataOutputStream and DataInputStream. Write the file name using writeUTF and read it back using readUTF. Write the length of the file with writeInt and read it back with readInt. Write the data with write and read it with read - and make sure to read only as many bytes as you received from the readInt call. And most important of all, keep using the same DataOutputStream and DataInputStream instances; don't constructor buffered readers and input-streams on the same underlying InputStream

Related

Error while sending large file over socket in java [duplicate]

I have stucked for 4h already with the sockets, the way I am using is is that there is only one application as client and server, once the client connect it is opening the theard with new client and waiting for message.
Once the message is send to the server, the client will receive respond, that part is working without any problems.
Part of the Client Theard:
while (true)
{
InputStreamReader IR = new InputStreamReader(clientSocket.getInputStream());
BufferedReader BR = new BufferedReader(IR);
PrintStream PS = new PrintStream(clientSocket.getOutputStream());
String message = BR.readLine();
if (message != null)
{
System.out.println(clientSocket.getInetAddress() + ":" + clientSocket.getPort() + " has connected."+message);
if (message.equals("exit"))
{
PS.println("Exiting...");
exit();
}
else if (message.equals("list"))
{
getList(PS);
}
else if ((message.contains("get") && (message.contains(",") && (message.contains(" ")))))
{
String[] spliter = message.split(" ");
String[] file = spliter[1].split(",");
String file_name = file[0];
String file_md5 = file[1];
getFile(file_name, file_md5, clientSocket);
}
}
else
{
break;
}
}
There are 2 messages that the server is supporting, the first one is "list" and the send one command is "get with values".
if client will request command "list" it will run this:
There is a "server/client", it is sending request and receive the one line string and it is working without any problem, I am receiving the list of files from the server.
PrintStream PS = new PrintStream(clientSocket.getOutputStream());
PS.println("list");
InputStreamReader IR = new InputStreamReader(clientSocket.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String lista_plikow = BR.readLine();
if ( lista_plikow != null)
{
return lista_plikow;
}
But I have problems to send the files over the sockets using code found on stackoverflow, but the "receiving" is not working, there is my receive function, the loop is always as 0 (even if first bytes length is correct), but the length of the bytes is correct, it is using newly created file but nothing is happening, the file is always on use, and has 0 bytes instead of content of the PS.println.
PrintStream PS = new PrintStream(clientSocket.getOutputStream());
PS.println("get "+filename+","+file_md5);
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try
{
byte [] mybytearray = new byte [Integer.parseInt(size)];
InputStream is = clientSocket.getInputStream();
fos = new FileOutputStream(filename + ".recived");
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
System.out.println("X" + bytesRead);
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
System.out.println(bytesRead + " = " + current + " " + (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
System.out.println(bytesRead);
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("File " + "recived." +filename.replace(":", " ")
+ " downloaded (" + current + " bytes read)");
}catch (Exception e)
{
System.out.println(e.getMessage());
}
And last part of the scrip the "PS.println("get "+filename+","+file_md5);" is doing exactly this one, the sending is working fine:
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
String the_file = TorrentAppGui.folder+"\\"+file_name.replace(":", " ");
File myFile = new File (the_file);
byte [] mybytearray = new byte [(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = clientSocket.getOutputStream();
System.out.println("Sending " + the_file + "(" + mybytearray.length + " bytes)");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
System.out.println("Done.");
I have no idea why I cannot save the bytes received by the "get" command, do you have any ideas?
I know that only the "receve" function is not working, because if I looged to the application via telnet I could get the file in the console, but it doesnt reach my target. See the screen from cli.
You can't mixed buffered and unbuffered streams/readers/writers on the same socket. You will lose data in the buffers. Use the same stream pair for the life of the socket. In this case I would use DataInputStream and DataOutputStream, and the readUTF()/writeUTF() methods for the messages and filenames. You will also need to send the file length ahead of the file, unless the file is the last thing sent over the connection: otherwise the peer won't know when to stop reading the file and go back and start reading messages again.

Send and then receive file in the same socket? [Java] [duplicate]

I am sending files to remote Android client from java server. I write the bytes using outputstream. On reading these bytes read() method keep trying to read bytes after the stream is ended. if I close the outputstream on server-side, read operation work fines. But I have to write file on the same socket again so can't close output stream any solution?
NOTE: MY CODE WORKS FINE FOR SHARING SINGLE FILE
CODE FOR WRITING FILE
public static void writefile(String IP, String filepath, int port, OutputStream out) throws IOException {
ByteFileConversion bfc = new ByteFileConversion();
byte[] file = bfc.FileToByteConversion(filepath);
out.write(file, 0, file.length);
out.close(); // i donot want to close this and how can I tell reading side that stream is ended.
System.out.println("WRITTEN");
}
Here Am I reading the file on Android :
public Bitmap fileReceived(InputStream is) {
Bitmap bitmap = null;
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "a.png";
String imageInSD = baseDir + File.separator + fileName;
// System.out.println(imageInSD);
if (is != null) {
FileOutputStream fos = null;
OutputStream bos = null;
try {
bos = new FileOutputStream(imageInSD);
byte[] aByte = new byte[1024];
int bytesRead;
int index = 0;
DataInputStream dis = new DataInputStream(is);
while ((bytesRead = is.read(aByte)) > 0) {
index = bytesRead + index;
bos.write(aByte, 0, bytesRead);
// index = index+ bytesRead;
System.out.println("Loop" + aByte + " byte read are " + bytesRead + "whree index =" + index);
}
bos.flush();
bos.close();
Log.i("IMSERVICE", "out of loop");
java.io.FileInputStream in = new FileInputStream(imageInSD);
bitmap = BitmapFactory.decodeStream(in);
bitmap = BitmapFactory.decodeFile(imageInSD);
Log.i("IMSERVICE", "saved");
// if (bitmap != null)
// System.out.println("bitmap is "+ bitmap.toString());
} catch (IOException ex) {
// Do exception handling
// Log.i("IMSERVICE", "exception ");
System.out.println("ex");
}
}
return bitmap;
}
Actually, I want to reset socket connection
Thanks in advance
You need to:
Send the length of the file ahead of the file. You can use DataOutputStream.writeLong() for that, and DataInputStream.readLong() at the receiver.
Read exactly that many bytes from the stream at the receiver:
while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0)
{
out.write(buffer, 0, count);
total += count;
}
E&OE
Actually I want to reset socket connection
Actually you don't want to do any such thing.
If i donot close outputstream the read operation on other side stuck on keep reading
That is because the client socket's InputStream is still waiting for the server to send some packets of data thus blocking your Main Thread.
Solution:
You can put each of your sending(OutputStream) and reading(InputStream) of packets of data from the socket to a Thread to prevent blocking your main thread when reading and sending.
Create a thread that reads the InputStream and another one for the OutputStream
Side note:
Don't try to close your outputStream that it cant be reopened again as the documentation is saying:
Closing the returned OutputStream will close the associated socket.
The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Resume file upload/download after lost connection (Socket programming)

I'm writing a program to download/upload a file between a client and server using socket programming. The code i've written till now works in the sense that i can sucesfully transfer files. However , if a connection fails due to problem in the network/client/server while a download / upload is occuring.. i need to RESUME the download/upload from the original point(Do not want the originally sent data to be resent). I'm not sure how to go about this. I'm reading the file into a byte array and sending it across the network. My initial idea is that everytime i'm downloading.. i should check if the file already exists and read the data into a byte array --> send the data to the server for comparison and then return the remaining data from the server file by comparing the two byte arrays. But this seems inefficient and takes away the point of resuming a download(since i'm sending the data again).
Note: The file name is an unique identifier.
I would really appreciate it if anybody could give me suggestions as to how i should implement the file resume functionality?
Server side code:
package servers;
import java.io.*;
import java.net.*;
import java.util.Arrays;
public class tcpserver1 extends Thread
{
public static void main(String args[]) throws Exception
{
ServerSocket welcomeSocket = null;
try
{
welcomeSocket = new ServerSocket(5555);
while(true)
{
Socket socketConnection = welcomeSocket.accept();
System.out.println("Server passing off to thread");
tcprunnable tcprunthread = new tcprunnable(socketConnection);
Thread thrd = new Thread(tcprunthread);
thrd.start();
System.out.println(thrd.getName());
}
}
catch(IOException e){
welcomeSocket.close();
System.out.println("Could not connect...");
}
}
}
class tcprunnable implements Runnable
{
Socket socke;
public tcprunnable(Socket sc){
socke = sc;
}
public void download_server(String file_name)
{
System.out.println("Inside server download method");
try
{
System.out.println("Socket port:" + socke.getPort());
//System.out.println("Inside download method of thread:clientsentence is:"+clientSentence);
// Create & attach output stream to new socket
OutputStream outToClient = socke.getOutputStream();
// The file name needs to come from the client which will be put in here below
File myfile = new File("D:\\ "+file_name);
byte[] mybytearray = new byte[(int) myfile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
System.out.println("Arrays on server:"+Arrays.toString(mybytearray));
outToClient.flush();
bis.close();
}
catch(FileNotFoundException f){f.printStackTrace();}
catch(IOException ie){
ie.printStackTrace();
}
}
public void upload_server(String file_name){
try{
byte[] mybytearray = new byte[1024];
InputStream is = socke.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream("D:\\ "+file_name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
do {
baos.write(mybytearray);
bytesRead = is.read(mybytearray);
}
while (bytesRead != -1);
bos.write(baos.toByteArray());
System.out.println("Array on server while downloading:"+Arrays.toString(baos.toByteArray()));
bos.close();
}
catch(FileNotFoundException fe){fe.printStackTrace();}
catch(IOException ie){ie.printStackTrace();}
}
#Override
public void run()
{
try
{
System.out.println("Server1 up and running" + socke.getPort());
// Create & attach input stream to new socket
BufferedReader inFromClient = new BufferedReader
(new InputStreamReader(socke.getInputStream()));
// Read from socket
String clientSentence = inFromClient.readLine();
String file_name = inFromClient.readLine();
System.out.println("Sever side filename:" + file_name);
try{
if(clientSentence.equals("download"))
{
download_server(file_name);
}
else if(clientSentence.equals("upload"))
{
upload_server(file_name);
System.out.println("Sever side filename:" + file_name);
}
else
{
System.out.println("Invalid input");
}
}
catch(NullPointerException npe){
System.out.println("Invalid input!");
}
socke.close();
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Exception caught");
}
}
}
Client side code:
package clients;
import java.io.*;
import java.net.*;
import java.util.Arrays;
public class tcpclient1
{
public static void main (String args[]) throws Exception
{
// Create input stream to send sentence to server
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = null;
while(true){
System.out.println("Please enter the server you want to use");
System.out.println("Enter 1 for Server 1 and 2 for Server2");
String server_choice = inFromUser.readLine();
if(server_choice.equals("1")){
// Create client socket to connect to server
// The server to use will be specified by the user
clientSocket = new Socket("localhost",5555);
break;
}
else if(server_choice.equals("2"))
{
clientSocket = new Socket("localhost",5556);
break;
}
else
{
System.out.println("Invalid entry");
}
}
System.out.println("Please enter download for dowloading");
System.out.println("Please enter upload for uploading");
// sentence is what'll be received from input jsp
String sentence = inFromUser.readLine();
if(sentence.equals("download"))
{
download_client(clientSocket,sentence);
}
else if(sentence.equals("upload"))
{
upload_client(clientSocket,sentence);
}
else
{
System.out.println("Invalid input");
}
clientSocket.close();
}
public static void download_client(Socket clientSocket , String sentence)
{
try{
// Create output stream attached to socket
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
// Send line to server
outToServer.writeBytes(sentence+'\n');
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of file to download:");
String file_to_download = inFromUser.readLine();
if(searching(file_to_download))
{
// Read local file and send that to the server for comparison
// DONT THINK THIS IS THE RIGHT WAY TO GO ABOUT THINGS SINCE IT BEATS THE PURPOSE OF RESUMING A DOWNLOAD/UPLOAD
}
// Send filetodownload to server
outToServer.writeBytes(file_to_download+'\n');
byte[] mybytearray = new byte[1024];
InputStream is = clientSocket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream("E:\\ "+file_to_download);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
do {
baos.write(mybytearray);
bytesRead = is.read(mybytearray);
}
while (bytesRead != -1);
bos.write(baos.toByteArray());
System.out.println("Array on client while downloading:"+Arrays.toString(baos.toByteArray()));
bos.close();
}
catch(FileNotFoundException fe){fe.printStackTrace();}
catch(IOException ie){ie.printStackTrace();}
}
public static void upload_client(Socket clientSocket, String sentence)
{
try{
// Create output stream attached to socket
DataOutputStream outToServer1 = new DataOutputStream(clientSocket.getOutputStream());
// Send line to server
outToServer1.writeBytes(sentence+'\n');
System.out.println("In the client upload method");
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of file to upload:");
String file_to_upload = inFromUser.readLine();
//System.out.println("Cline side file name:"+file_to_upload);
outToServer1.writeBytes(file_to_upload+'\n');
System.out.println(file_to_upload);
OutputStream outtoserver = clientSocket.getOutputStream();
File myfile = new File("E:\\ "+file_to_upload);
byte[] mybytearray = new byte[(int) myfile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));
bis.read(mybytearray, 0, mybytearray.length);
outtoserver.write(mybytearray, 0, mybytearray.length);
System.out.println("filename:"+file_to_upload+"Arrays on client while uploading:"+Arrays.toString(mybytearray));
outtoserver.flush();
bis.close();
}
catch(FileNotFoundException fe){fe.printStackTrace();}
catch(IOException ie){ie.printStackTrace();}
}
public static boolean searching(String file_name)
{
String file_path = "E:\\ "+file_name;
File f = new File(file_path);
if(f.exists() && !f.isDirectory()) { return true; }
else
return false;
}
}
The above code runs fine for transferring files between the client and server.
Again , would really appreciate any help!
There are many ways which you can do this, I suggest you to create a separate type of request to the server that accepts the file's name and file position which is the position where in the file where the connection failed.
That's how you will get the file from the server in the client's side:
int filePosition = 0;
InputStream is = clientSocket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
do {
baos.write(mybytearray);
bytesRead = is.read(mybytearray);
if(bytesRead != -1)
filePosition += bytesRead;
}
while (bytesRead != -1);
Now if the connection got interrupted for some reason you can send a request again to the server with the same file name and the filePosition, and the server will send the file back like this:
OutputStream outToClient = socke.getOutputStream();
// The file name needs to come from the client which will be put in here below
File myfile = new File("D:\\ "+file_name);
byte[] mybytearray = new byte[(int) myfile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));
bis.skip(filePosition) //Advance the stream to the desired location in the file
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
System.out.println("Arrays on server:"+Arrays.toString(mybytearray));
outToClient.flush();
bis.close();
And in the client you can open the file stream and specify append = true in the constructor like this:
FileOutputStream fos = new FileOutputStream("D:\\ "+file_name, true);
This could be one way to do this, there are a lot more options. And I also suggest verify the files after the transfer using some hash function like MD5 for example, it creates unique stamp for a given input and it always outputs same result for the same input, which means, you can create the stamp from the same file both in the server and in the client and if the file is truly the same, it will generate the same stamp. Since the stamp's size is very small relative to the file it self and it is also fixed, it can be send between the client/server without much overhead.
You can generate an MD5 hash with this code:
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"))) {
DigestInputStream dis = new DigestInputStream(is, md);
/* Read stream to EOF as normal... */
}
byte[] digest = md.digest();
(taken from: Getting a File's MD5 Checksum in Java)
Basically, when requesting a download You should attach information about how many bytes need to be skipped (0 on new download). You should get this information from part of the file that you have downloaded (read it's size). Server should skip given count of bytes and send back the remainder of file. Client should append this to the existing file. For sanity check, You could add some file hash checking in the end, to ensure You got the file correctly.

java send firmware thru telnet

Hallo i an trying to send Firmware file to modem board thru telnet.
Here is my code:
Socket s = null;
try {
SocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(addr), 23);
s = new Socket();
s.connect(socketAddress, 1000);
InputStream inputStream = s.getInputStream();
OutputStream outputStream = s.getOutputStream();
outputStream.write( (login + "\n") .getBytes());
Thread.sleep(300);
outputStream.write( (password + "\n") .getBytes());
Thread.sleep(300);
outputStream.write(("swupdate" + "\n").getBytes());
Thread.sleep(300);
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(path_frm_vdsl);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
// Read File Line By Line
while ((line = br.readLine()) != null) {
// Print the content on the console
line = br.readLine();
if (line == null) {
Thread.sleep(1000);
} else {
//System.out.println(line);
outputStream.write(line.getBytes());
Log.v("---", line.getBytes() + "" + consumeInput(500, inputStream));
//Log.v("Update_Modem","Updated " + consumeInput(500, inputStream));
//outputStream.write(line.getBytes());
Thread.sleep(10);
}
}
It simply logs in sends swupdate command and dumps firmware file to output. After first line of input i am having java.net.SocketException: Broken pipe
And i can not read all file at once, not enough memory exception. (3Mb)
You know telnet uses in-channel signalling? the data stream contains escaped command instructions. When you open a telnet connection, a whole slew of initial commands are passed back and forth, as the server and client determine each others capabilities and preferences.
You may well find your byte stream is corrupted, if you just take whatever you get turning up at the server. You will need to honour the telnet protocol, e.g. properly understand the byte stream you're receiving.
The line terminator in the Telnet protcol is \r\n.
But why would you want to read the whole file into memory? And why all the sleeps? And why are you skipping every second line? And what is consumeInput()?
Just read and write bytes.
Not sure how your receiving end wants the firmware, plain binary, hex encoded, base64 encoded?
Anyway, here is how you would send it in plain binary
Socket s = null;
try {
SocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(addr), 23);
s = new Socket();
s.connect(socketAddress, 1000);
InputStream inputStream = s.getInputStream();
OutputStream outputStream = s.getOutputStream();
outputStream.write((login + "\n").getBytes());
Thread.sleep(300);
outputStream.write((password + "\n").getBytes());
Thread.sleep(300);
outputStream.write(("swupdate" + "\n").getBytes());
Thread.sleep(300);
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(path_frm_vdsl);
byte[] buffer = new byte[1024];
int fillSize;
// Read File chunk by chunk
while ((fillSize = fstream.read(buffer)) != -1) {
outputStream.write(buffer, 0, fillSize);
}
outputStream.close();
} finally {
s.close();
}
So, read the file in chunks until there is no more data (read returns -1) and write the chunk (read returns how much was actually read).

Sending large files over socket

I got working over socket file sender, it worked perfectly, but I couldn't send large files with it. Always got heap error. Then I changed the code of client, so it would send file in chunks. Now I can send big files, but there is new problem. Now I recieve small files empty and larger files for example videos can't be played. Here is the code of client that sends file:
public void send(File file) throws UnknownHostException, IOException {
// Create socket
hostIP = "localhost";
socket = new Socket(hostIP, 22333);
//Send file
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
OutputStream os = socket.getOutputStream();
//Sending size of file.
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName() + ":" + userName);
byte[] arr = new byte[1024];
try {
int len = 0;
while ((len = dis.read(arr)) != -1) {
dos.write(arr, 0, len);
}
} catch (IOException ex) {
ex.printStackTrace();
}
dos.flush();
socket.close();
}
and here is the server code:
void start() throws IOException {
// Starts server on port.
serverSocket = new ServerSocket(port);
int bytesRead;
while (true) {
connection = serverSocket.accept();
in = connection.getInputStream();
clientData = new DataInputStream(in);
String[] data = clientData.readUTF().split(":");
String fileName = data[0];
String userName = data[1];
output = new FileOutputStream("C:/" + fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
// Build new file
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
}
}
You failed to write out the length of the file to the stream in the client:
long size = clientData.readLong();
So that call in the server is reading the first 8 bytes of the actual file and who knows what that quantity is. You don't have to read the length from the stream since you only wrote a single file. After reading the filename, and username (not very secure is it?) you can just read the stream until EOF. If you ever wanted to send multiple files over the same open socket then you'd need to know the length before reading the file.
Also your buffers for reading are way to small. You should be at a minimum of 8192 instead of 1024. And you'll want to put all .close() in a finally block to make sure your server and clients shutdown appropriately if there is an exception ever.

Categories