I am sending an image from my android gallery to a java server; however, I am getting a NegativeArraySizeException. Can anyone tell me what I am doing wrong? I do not understand what the problem is. Thanks in advance.
Android Client:
Socket sock;
sock = new Socket("IP", PORT);
System.out.println("Connecting...");
OutputStream os = sock.getOutputStream();
File myFile = new File (selectedImagePath);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
Java Server
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket = serverSocket.accept();
byte[] sizeAr = new byte[4];
InputStream inputStream = socket.getInputStream();
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
System.out.println(size); // getting negative value
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("C:/Users/Dan/Pictures/NewCat.jpg"));
serverSocket.close();
}
}
Related
I am trying to transfer a file from server to client using Java and TCP, however on the client-side I am getting a socket closed exception, whereas the server has no errors when attempting to transfer the file. I am confused about this error because I did not close the socket before trying to read from it. The server accepts the connection and sends the file, but the client gets that error. Any suggestion?
The error is:
java.net.SocketException: Socket closed
Server thread's run function:
public void run() {
System.out.println("Service thread running for client at " + socket.getInetAddress() + " on port " + socket.getPort());
try {
File file = new File("hank.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = socket.getOutputStream();
byte[] contents;
long fileLength = file.length();
long current = 0;
long start = System.nanoTime();
while(current!=fileLength) {
int size = 1000;
if(fileLength - current >= size) {
current += size;
}
else {
size = (int)(fileLength - current);
current = fileLength;
}
contents = new byte[size];
bis.read(contents,0,size);
os.write(contents);
System.out.println("sending file..." + (current*100)/fileLength+"% complete!");
}
os.flush();
this.socket.close();
}catch(Exception e) {
e.printStackTrace();
}
}
Client receiving the file code:
System.out.println("Going to get the file...");
socket = new Socket(response.getIP().substring(1), response.getPort());
byte[] contents = new byte[10000];
FileOutputStream fos = new FileOutputStream("hank.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream in = socket.getInputStream();
int bytesRead = 0;
System.out.println("Starting to read file...");
while((bytesRead = is.read(contents))!=-1) { //the error points to this lin
bos.write(contents,0,bytesRead);
}
bos.flush();
bos.close();
in.close();
//
socket.close();
Input stream for this socket is available in variable in
InputStream in = socket.getInputStream();
So
Change is.read(contents)) to in.read(contents))
im having a trouble here, here i built this Socket server to download files, it downloades the files inside of a folder, but it doesnt download a specified file name an returns null, could you guys help me where should i change in this server to do so?
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String Request = (String) sInput.readObject();
System.out.println("request is:"+Request);
String[] todoname=Request.split("\\#reza-hp");
String name=todoname[0];
System.out.println("Connecting...");
File filcheck = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
System.out.println(filcheck);
if (filcheck.exists()){
fil = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
}else{
fil = new File("D://Users//Default//");
}
System.out.println(fil);
File[] Files=fil.listFiles();
System.out.println(Files);
for (int count=0;count < Files.length;count ++){
System.out.println(Files[count].getName());
}
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeInt(Files.length);
for (int count=0;count<Files.length;count ++){
dos.writeUTF(Files[count].getName());
}
for (int count=0;count<Files.length;count ++){
int filesize = (int) Files[count].length();
dos.writeInt(filesize);
}
for (int count=0;count<Files.length;count ++){
int filesize = (int) Files[count].length();
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(Files[count].toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length); //This line is important
dos.write(buffer, 0, buffer.length);
fis.close();
dos.flush();
//close socket connection
// socket.close();
}
// Toast.makeText(getApplicationContext(),"Transfer file is
completed!!", Toast.LENGTH_LONG).show();
dos.close();
os.close();
//socket.close();
thanks
Well fixed it , here i post the singe file downloader, and in the question is the File's containing's download from any type,image,music,video , both are very viable for who ever needs to use, good luck.
String Type;
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
// my unique id (easier for deconnection)
int id;
// Constructore
ClientThread(Socket socket) throws InterruptedException {
// a unique id
id = ++uniqueId;
this.socket = socket;
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output
Streams");
while (!jobdone){
try
{
// create output first
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String Request = (String) sInput.readObject();
System.out.println("request is:"+Request);
String[] todoname=Request.split("\\#reza-hp");
String name=todoname[0];
System.out.println("Connecting...");
File filcheck = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
System.out.println(filcheck);
if (filcheck.exists()){
fil = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
}else{
fil = new File("D://Users//Default//");
}
System.out.println(fil);
// File[] Files=fil.listFiles();
// for (int count=0;count < Files.length;count ++){
System.out.println(fil.getName());
// }
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeInt(1);
// for (int count=0;count<Files.length;count ++){
dos.writeUTF(fil.getName());
// }
// for (int count=0;count<Files.length;count ++){
int filesize = (int) fil.length();
dos.writeInt(filesize);
// }
// for (int count=0;count<Files.length;count ++){
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(fil.toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length); //This line is important
dos.write(buffer, 0, buffer.length);
fis.close();
dos.flush();
//close socket connection
// socket.close();
// Toast.makeText(getApplicationContext(),"Transfer file is
completed!!", Toast.LENGTH_LONG).show();
dos.close();
os.close();
//socket.close();
}
1.client is sending file succesfully to server
2.server receives file successfully
But
3. when server tries to send this file back to client ,after working on received file( sent by client) following error occurs :
socket:null
connection is closed
This is sending file method back to client
"server code: "
public void sendFileBack(String fileName)
throws UnknownHostException,FileNotFoundException, IOException {
Socket sock = new Socket("192.168.0.103", 13267);
String tmpName=fName.replace(".mcrl2", ".lps");
File myFile = new File("out" + "\\" +tmpName );
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
//bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
//Sending file name and file size to the server
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
//Sending file data to the server
os.write(mybytearray, 0, mybytearray.length);
os.flush();
console.append("\n\n Output file transfered successfully \n \n");
//Closing socket
sock.close();
}
This is receiving file method back from server
"client code:"
private void receiveFileBack() throws IOException {
int bytesRead;
int port;
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(4444);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.print("accepted connection"+clientSocket);
InputStream in = clientSocket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.print(fileName+"..........");
File f =new File("serverOut\\"+fileName);
OutputStream output = new FileOutputStream("serverOut\\"+fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 &&
(bytesRead = clientData.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1) {
serverStatus.append("Reading file..\n");
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
// Closing the FileOutputStream handle
output.close();
output.flush();
}
}
I have a server client file transfer program which works fine in java .but i copied it and changed the file location d:/ to environm.....
The problem is the filesize is lesser than sending one, hence its shown as corrupted.. Any idea .
SERVER SIDE
public class Server
{
// create socket
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
while(true)
{
ServerSocket servsock = new ServerSocket(13000);
System.out.println("Main Waiting...");
Socket socket = servsock.accept();
System.out.println("Accepted connection : " + socket);
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
long filesize = new Long(in.readLong());
System.out.println("received Size:"+filesize);
// read file name
String fname=in.readUTF();
System.out.println("server : file name: "+fname);
String prefix=fname.substring(0,fname.lastIndexOf('.')).trim();
String suffix=fname.substring(fname.lastIndexOf('.'),fname.length()).trim();
String file = prefix + suffix;
System.out.println("received name:"+file);
File f=new File("F:/Users/achu/Desktop/"+ file);// changed this in android
f.createNewFile();
// receive file
byte [] mybytearray = new byte [(int)(filesize)];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
System.out.println("current"+current);
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(current<filesize && bytesRead >-1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
is.close();
socket.close();
servsock.close();
}
}
CLIENT SIDE
public class Client
{
Socket socket=new Socket("101.59.43.58",13000);
System.out.println("Connectedddd");
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
File file = new File("d:/my.pdf");
out.writeLong(file.length());
out.flush();
out.writeUTF(file.getName());
byte [] mybytearray = new byte [(int)file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Send:"+mybytearray.length);
OutputStream ous = socket.getOutputStream();
System.out.println("Sending...");
ous.write(mybytearray,0,mybytearray.length);
ous.flush();
bis.close();
ous.close();
System.out.println("closing socket...");
socket.close();
}
My client can send Images normally to server, but when it comes to text files they arrive empty. Any ideas what am I doing wrong? I'd really appreciate help, because I have been trying to make this work for many days now. Thanks.
Here is the server code:
class TheServer {
public void setUp() throws IOException { // this method is called from Main class.
ServerSocket serverSocket = new ServerSocket(1991);
System.out.println("Server setup and listening...");
Socket connection = serverSocket.accept();
System.out.println("Client connect");
System.out.println("Socket is closed = " + serverSocket.isClosed());
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String str = rd.readLine();
System.out.println("Recieved: " + str);
rd.close();
InputStream is = connection.getInputStream();
int bufferSize = connection.getReceiveBufferSize();
FileOutputStream fos = new FileOutputStream("C:/" + str);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[bufferSize];
int count;
while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
}
bos.flush();
bos.close();
is.close();
connection.close();
serverSocket.close();
}
}
and here is the client code:
public class TheClient {
public void send(File file) throws UnknownHostException, IOException { // this method is called from Main class.
Socket socket = null;
String host = "127.0.0.1";
socket = new Socket(host, 1991);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large.");
}
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
wr.write(file.getName());
wr.newLine();
wr.flush();
byte[] bytes = new byte[(int) length];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count;
while ((count = bis.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
out.flush();
out.close();
fis.close();
bis.close();
socket.close();
}
}
You are prematurely closing BufferedReader on server side before reading all the data. This essentially closes the connection.
You should not use Reader or Writer for non-character streams like binary image data. And you should not mix BufferedReader with any other stream wrapper for the same stream since it may read as many data as it fills in buffer.