Wednesday, 18 September 2013

TCP sockets sending files only works in one direction, how to fix?

TCP sockets sending files only works in one direction, how to fix?

with this sockets code I can send a file like a pdf or datbase file from
the client to the server with no problems but it does not work the other
way, the file that is sent from server to the client arrives with size of
0 bytes, how to get this to work?
it is supposed to send files in both directions
main server code, the part that launches threads for each connection
server code runs on windows 7 PC as java desktop application some try
catch exceptions removed for readability
serversocket = new ServerSocket(6789, 100);
while(runner){
socket = serversocket.accept();
MultiThreader multi = new MultiThreader(socket);
Thread t = new Thread(multi);
t.start();
server code that launched for each connection as a thread, class is called
MultiThreader
public MultiThreader(Socket s) {
countForTimer = 0;
socket = s;
}
// this code is for receiving files from the client
@Override
public void run() {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
buffer = new byte[bufferSize];
while ((count = is.read(buffer)) > 0) {
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
is.close();
socket.close();
} // end run
// this code is for sending a file from this server to the client
private void sendOutPack(File file) {
long length = file.length();
System.out.println("lengtyh of fole " + file.length());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
if (socket != null) {
byte[] bytes = new byte[(int) length];
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(socket.getOutputStream());
int count;
while ((count = bis.read(bytes)) > 0) {
bos.write(bytes, 0, count);
}
// bos.flush();
// bos.close();
fis.close();
bis.close();
socket.close();
} // end socket null check
} // end sendoutpack
Client code that runs on Android tablet, shows methods for connecting to
server for sending and receiving files
@Override
public void run() {
FileOutputStream fos = null;
FileInputStream fis = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
InputStream is = null;
File file = new File("/mnt/sdcard/JIS.db");
int count;
int bufferSize = 0;
byte[] buffer;
socket = new Socket(InetAddress.getByName(serverIP), 6789);
if (socket != null) {
connected = true;
}
// receive db file from server and put into directory on sd card
file2 = new File("/mnt/sdcard/JISSend.db");
new Thread(new SendOutPack()).start();
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[bufferSize];
while (((count = is.read(bytes)) > 0) && (connected == true)) {
bos.write(bytes, 0, count);
}
bos.flush();
bos.close();
bos = null;
} // end if
} // end run method
// send file from local sd card to server
public class SendOutPack implements Runnable {
long length = file2.length();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
FileOutputStream fos = null;
@Override
public void run() {
if (socket != null) {
try {
byte[] bytes = new byte[(int) length];
fis = new FileInputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(socket.getOutputStream());
int count;
while ((count = bis.read(bytes)) > 0) {
bos.write(bytes, 0, count);
}
fis.close();
bis.close();
fis = null;
bis = null;
socket.close();
// bos.flush();
// bos.close();
// socket.close();
} // end socket null check
} // end run method
} // end sendoutpack
} // end run
} // end StopTimer nested class

No comments:

Post a Comment