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

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

View File

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

View File

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

View File

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