java - send file then message through socket -
i trying send file client server. server receives file , send confirmation message client.
my problem client becomes unresponsive when waiting confirmation.
server :
import java.net.*; import java.io.*; public class server { public static void main(string args[]) throws ioexception { int port = 1234; serversocket server_socket; server_socket = new serversocket(port); system.out.println("server waiting client on port " + server_socket.getlocalport()); // server infinite loop while (true) { socket socket = server_socket.accept(); system.out.println("new connection accepted " + socket.getinetaddress() + ":" + socket.getport()); try { byte[] b = new byte[1024]; int len = 0; int bytcount = 1024; string filename = "c:\\test\\java\\tmp\\sentfile.pdf"; fileoutputstream infile = new fileoutputstream(filename); inputstream = socket.getinputstream(); bufferedinputstream in2 = new bufferedinputstream(is, 1024); while ((len = in2.read(b, 0, 1024)) != -1) { bytcount = bytcount + 1024; infile.write(b, 0, len); } system.out.println("bytes writen : " + bytcount); //sending response client. objectoutputstream oos = new objectoutputstream(socket.getoutputstream()); oos.flush(); oos.writeobject("ok"); system.out.println("message sent client "+"ok"); in2.close(); infile.close(); } catch (ioexception e) { system.out.println("unable open file" + e); return; } socket.close(); system.out.println("connection closed client"); } } }
client :
import java.net.*; import java.io.*; public class client { public static void main(string[] args) throws unknownhostexception, ioexception, classnotfoundexception { int port = 1234; socket socket = null; // connect server socket = new socket("127.0.0.1", port); try { byte[] buf = new byte[1024]; outputstream os = socket.getoutputstream(); bufferedoutputstream out = new bufferedoutputstream(os, 1024); string file = "c:\\test\\java\\client\\source.pdf"; fileinputstream in = new fileinputstream(file); int = 0; int bytecount = 1024; while ((i = in.read(buf, 0, 1024)) != -1) { bytecount = bytecount + 1024; out.write(buf, 0, i); out.flush(); } system.out.println("bytes sent :" + bytecount); objectinputstream ois = new objectinputstream(socket.getinputstream()); string confirmation = (string) ois.readobject(); system.out.println("from server : " + confirmation); out.close(); in.close(); } catch (ioexception e) { system.out.println(e); } socket.close(); } }
here complete working example. server:
package so6540787; import java.io.bufferedinputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.objectoutputstream; import java.net.serversocket; import java.net.socket; public class server { private final socket socket; public server(socket socket) { this.socket = socket; } public void receivefile(file file) throws ioexception { byte[] b = new byte[1024]; int len = 0; int bytcount = 1024; fileoutputstream infile = new fileoutputstream(file); inputstream = socket.getinputstream(); bufferedinputstream in2 = new bufferedinputstream(is, 1024); while ((len = in2.read(b, 0, 1024)) != -1) { bytcount = bytcount + 1024; infile.write(b, 0, len); } system.out.println("bytes writen : " + bytcount); // sending response client. objectoutputstream oos = new objectoutputstream(socket.getoutputstream()); oos.flush(); oos.writeobject("ok"); system.out.println("message sent client " + "ok"); in2.close(); infile.close(); } public static void main(string[] args) throws ioexception { serversocket listen = new serversocket(10000, 1); socket socket = listen.accept(); server server = new server(socket); server.receivefile(new file("c:/received.pdf")); } }
and client:
package so6540787; import java.io.bufferedoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.outputstream; import java.net.socket; public class client { private final socket socket; public client(socket socket) { this.socket = socket; } public void sendfile(file file) throws ioexception, classnotfoundexception { byte[] buf = new byte[1024]; outputstream os = socket.getoutputstream(); bufferedoutputstream out = new bufferedoutputstream(os, 1024); fileinputstream in = new fileinputstream(file); int = 0; int bytecount = 1024; while ((i = in.read(buf, 0, 1024)) != -1) { bytecount = bytecount + 1024; out.write(buf, 0, i); out.flush(); } socket.shutdownoutput(); /* important */ system.out.println("bytes sent :" + bytecount); objectinputstream ois = new objectinputstream(socket.getinputstream()); ois.skip(long.max_value); string confirmation = (string) ois.readobject(); system.out.println("from server : " + confirmation); out.close(); in.close(); } public static void main(string[] args) throws ioexception, classnotfoundexception { client client = new client(new socket("localhost", 10000)); client.sendfile(new file("c:/tobesent.pdf")); } }
the line missing "socket.shutdownoutput()". if leave line out, server never see end-of-file marker -1
read
call.
please fix way count bytes have been sent. should start 0 instead of 1024 , increment counter many bytes have read.
Comments
Post a Comment