multithreading - Java socket is stuck on readUTF() -
i'm trying transfer files on socket in java, current approach server is:
- create new thread
- thread sends file name using dos.writeutf()
- thread sends file size using dos.writelong()
- thread sends file using dos.write()
where each thread represents client , dos instance of dataoutputstream.
now, on client i'm doing same thing reading instead of writing:
- read file name using dis.readutf()
- read file size using dis.readlong()
- read file using dis.read()
where dis instance of datainputstream.
problem is: when sending 1 file, goes right, when try send 3 files, 1 after another, looks server writing correctly stream expected client (after first file, means starts happening second file) stuck on dis.readutf() , can't move on.
i've tried fixing days can't work.
here's source code:
server:
main.java
public class main { public static void main(string[] args) { boolean booldebug = true;//todo remove this!! serversocket serversock = null; list<socket> clientsocks; list<clientthread> clientthreads; try { serversock = new serversocket(9090); } catch(exception e){ e.printstacktrace(); } clientsocks = new arraylist<>(); clientthreads = new arraylist<>(); serversocket finalserversock = serversock; system.out.println(); system.out.println("listening incoming connections\n"); new thread(){ @override public void run() { super.run(); while (true) { try { socket newsock = finalserversock.accept(); clientsocks.add(newsock); //fixme remove sockets when closed thread thread = new clientthread(newsock, usr, psw); thread.start(); clientthreads.add((clientthread)thread); } catch (exception e) { e.printstacktrace(); } } } }.start(); } } clientthread.java
public class clientthread extends thread { private socket socket; private datainputstream instream; private dataoutputstream outstream; private string dbuser; private string dbpassword; public clientthread(socket socket, string dbuser, string dbpass) { this.socket = socket; this.dbuser = dbuser; this.dbpassword = dbpass; } @override public void run() { try { instream = new datainputstream(socket.getinputstream()); outstream = new dataoutputstream(socket.getoutputstream()); sendfile("a.txt"); sendfile("b.txt"); sendfile("c.txt"); } catch (exception e) { e.printstacktrace(); } } void sendfile(string file){ try { file f = new file(file); outstream.writeutf(file); outstream.writelong(f.length()); fileinputstream fis = new fileinputstream(f); byte[] buffer = new byte[4096]; while (fis.read(buffer) > 0) { outstream.write(buffer); } fis.close(); }catch(exception e){ e.printstacktrace(); } } int getsize(byte[] buffer,long remaining){ try { return math.tointexact(math.min(((long) buffer.length), remaining)); }catch(arithmeticexception e){ return 4096; } } } client: main.java
class main { static int getsize(byte[] buffer, long remaining) { try { return math.tointexact(math.min(((long) buffer.length), remaining)); } catch (arithmeticexception e) { return 4096; } } static void savefile(socket clientsock,datainputstream dis) throws ioexception { string filename = dis.readutf(); file f = new file(filename); fileoutputstream fos = new fileoutputstream(f); byte[] buffer = new byte[4096]; long filesize = dis.readlong(); int read = 0; int totalread = 0; long remaining = filesize; while ((read = dis.read(buffer, 0, getsize(buffer, remaining))) > 0) { totalread += read; remaining -= read; system.out.println("read " + totalread + " bytes."); fos.write(buffer, 0, read); } fos.close(); } public static void main(string[] args) throws exception { socket sock = new socket("192.168.2.17", 9090); datainputstream dis = new datainputstream(sock.getinputstream()); savefile(sock,dis); savefile(sock,dis); savefile(sock,dis); } } many in advance, looking forward fix :(
fixed changing
while (fis.read(buffer) > 0) { outstream.write(buffer); } to
int count; while ((count = fis.read(buffer)) > 0) { outstream.write(buffer, 0, count); } inside clientthread.java on server side
Comments
Post a Comment