Передача данных теперь работает корректно.

This commit is contained in:
Victor 2013-06-01 17:29:25 +03:00
parent 1bcc8bdcb1
commit 06525704de
5 changed files with 9 additions and 24 deletions

View File

@ -33,20 +33,13 @@ public class FileOperation implements Operation {
FileOutputStream fout = null; FileOutputStream fout = null;
try { try {
String name = dis.readUTF(); String name = dis.readUTF();
long length = dis.readLong();
System.out.println("Filename: " + name); System.out.println("Filename: " + name);
System.out.println("Size: " + (length / 1024) + "kb");
fout = new FileOutputStream(Config.getTransferDir() + name); fout = new FileOutputStream(Config.getTransferDir() + name);
byte[] buffer = new byte[BUFFER_SIZE];
for (long i = 0; i < length; i++) {
byte read = dis.readByte();
fout.write(read);
}
/*byte[] buffer = new byte[BUFFER_SIZE];
int count; int count;
while ((count = dis.read(buffer, 0, BUFFER_SIZE)) > 0) { while ((count = dis.read(buffer, 0, BUFFER_SIZE)) != -1) {
fout.write(buffer, 0, count); fout.write(buffer, 0, count);
}*/ }
fout.flush(); fout.flush();
fout.close(); fout.close();
} catch (IOException ex) { } catch (IOException ex) {
@ -69,8 +62,6 @@ public class FileOperation implements Operation {
String name = file.getName(); String name = file.getName();
dos.writeUTF(name); dos.writeUTF(name);
dos.writeLong(file.length());
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
int count; int count;

View File

@ -25,6 +25,7 @@ public class MessageOperation implements Operation {
public void startServerSide() throws IOException { public void startServerSide() throws IOException {
String text = dis.readUTF(); String text = dis.readUTF();
MessageHistory.addMessage(text); MessageHistory.addMessage(text);
System.out.println(text);
} }
@Override @Override

View File

@ -30,11 +30,9 @@ public class OperationListener {
public void listenOperation() throws Exception { public void listenOperation() throws Exception {
int mode = dis.readInt(); int mode = dis.readInt();
System.out.println("Read mode: " + mode);
Operation operation; Operation operation;
switch(mode) { switch(mode) {
case MODE_FILE_TRANSFER: case MODE_FILE_TRANSFER:
System.out.println("MODE_FILE_TRANSFER");
operation = new FileOperation(dis); operation = new FileOperation(dis);
break; break;
case MODE_MESSAGE_TRANSFER: case MODE_MESSAGE_TRANSFER:

View File

@ -43,5 +43,5 @@ public class Server {
} }
} }
} }
} }

View File

@ -19,15 +19,10 @@ public class TransferServer implements Runnable {
@Override @Override
public void run() { public void run() {
Thread thr = Thread.currentThread(); try {
while(Thread.currentThread() == thr) { listener.listenOperation();
try { } catch (Exception ex) {
listener.listenOperation(); ExceptionHandler.handle(ex);
//Thread.sleep(1);
} catch (Exception ex) {
ExceptionHandler.handle(ex);
break;
}
} }
listener.close(); listener.close();
} }