Java Client-Server problem when sending multiple files
Posted
by
Jim
on Stack Overflow
See other posts from Stack Overflow
or by Jim
Published on 2011-01-17T08:50:40Z
Indexed on
2011/01/17
8:53 UTC
Read the original article
Hit count: 257
Client
public void transferImage() {
File file = new File(ServerStats.clientFolder);
String[] files = file.list();
int numFiles = files.length;
boolean done = false;
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
byte[] byteArray;
long count;
long len;
Socket socket = null ;
while (!done){
try{
socket = new Socket(ServerStats.imgServerName,ServerStats.imgServerPort) ;
InputStream inStream = socket.getInputStream() ;
OutputStream outStream = socket.getOutputStream() ;
System.out.println("Connected to : " + ServerStats.imgServerName);
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
for (int itor = 0; itor < numFiles; itor++) {
String fileName = files[itor];
System.out.println("transfer: " + fileName);
File sentFile = new File(fileName);
len = sentFile.length();
len++;
System.out.println(len);
out.println(len);
out.println(sentFile);
//SENDFILE
bis = new BufferedInputStream(new FileInputStream(fileName));
bos = new BufferedOutputStream(socket.getOutputStream( ));
byteArray = new byte[1000000];
count = 0;
while ( count < len ){
num = bis.read(byteArray);
bos.write(byteArray,0,num);
count++;
}
bos.close();
bis.close();
System.out.println("file done: " + itor);
}
done = true;
}catch (Exception e) {
System.err.println(e) ;
}
}
}
Server
public static void main(String[] args) {
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
File file = new File(ServerStats.serverFolder);
if (!(file.exists())){
file.mkdir();
}
try {
int i = 1;
ServerSocket socket = new ServerSocket(ServerStats.imgServerPort);
Socket incoming = socket.accept();
System.out.println("Spawning " + i);
try {
try{
if (!(file.exists())){
file.mkdir();
}
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
String length2 = inm.readLine();
System.out.println(length2);
String filename = inm.readLine();
System.out.println("Filename = " + filename);
out.println("ACK: Filename received = " + filename);
//RECIEVE and WRITE FILE
byte[] receivedData = new byte[1000000];
bis = new BufferedInputStream(incoming.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(ServerStats.serverFolder + "/" + filename));
long length = (long)Integer.parseInt(length2);
length++;
long counter = 0;
while (counter < length){
num = bis.read(receivedData);
bos.write(receivedData,0,num);
counter ++;
}
System.out.println(counter);
bos.close();
bis.close();
File receivedFile = new File(filename);
long receivedLen = receivedFile.length();
out.println("ACK: Length of received file = " + receivedLen);
} finally {
incoming.close();
}
} catch (IOException e){
e.printStackTrace();
}
} catch (IOException e1){
e1.printStackTrace();
}
}
The code is some I found, and I have slightly modified it, but I am having problems transferring multiple images over the server.
Output on Client:
run ServerQueue.Client
Connected to : localhost
transfer: Picture 012.jpg
1312743
java.lang.ArrayIndexOutOfBoundsException
Connected to : localhost
transfer: Picture 012.jpg
1312743
Cant seem to get it to transfer multiple images.
But bothsides I think crash or something because the file never finishes transfering
© Stack Overflow or respective owner