java - Accepting multiple simultaneous client sockets on their own threads -
i did different tutorials nothing works, can see i'm doing wrong?
private volatile boolean keeprunning = true; public filesharedserver() { } @override public void run() { try { system.out.println("binding port " + port + "..."); // bind port used clients request socket connection // server. serversocket serversocket = new serversocket(port); system.out.println("\tbound."); system.out.println("waiting client..."); socket = serversocket.accept(); system.out.println("\tclient connected.\n\n"); if (socket.isconnected()) { system.out.println("writing client serverid " + serverid + "."); // write serverid plus end character client thru // socket // outstream socket.getoutputstream().write(serverid.getbytes()); socket.getoutputstream().write(end); } while (keeprunning) { system.out.println("ready"); // receive command form client int command = socket.getinputstream().read(); // disconnect if class closes connection if (command == -1) { break; } system.out.println("received command '" + (char) command + "'"); // decide do. switch (command) { case list_files: sendfilelist(); break; case send_file: sendfile(); break; default: break; } } } catch (ioexception e) { system.out.println(e.getmessage()); } { // not close socket here because readfromclient() method // still needs // called. if (socket != null && !socket.isclosed()) { try { system.out.println("closing socket."); socket.close(); } catch (ioexception e) { e.printstacktrace(); } } } } /** * method sends names of of files in share directory. * * @throws ioexception */ private void sendfilelist() throws ioexception { file serverfilesdir = new file("serverfiles/"); if (!serverfilesdir.exists() || serverfilesdir.isfile()) { system.out.println("'serverfiles' not existing directory"); throw new ioexception("'serverfiles' directory not exist."); } file[] files = serverfilesdir.listfiles(); (file file : files) { socket.getoutputstream().write(file.getname().getbytes()); // last 1 must end end , // end_of_list. socket.getoutputstream().write(end); } socket.getoutputstream().write(end_of_list); } /** * methods sends particular file client. * * @throws ioexception */ private void sendfile() throws ioexception { stringbuilder filename = new stringbuilder(); int character = -1; while ((character = socket.getinputstream().read()) > -1 && character != end && (char) character != end_of_list) { filename.append((char) character); } system.out.println(filename); file file = new file(system.getproperty("user.dir") + system.getproperty("file.separator") + "serverfiles", filename.tostring()); string totallength = string.valueof(file.length()); socket.getoutputstream().write(totallength.getbytes()); socket.getoutputstream().write(end); fileinputstream fileinputstream = new fileinputstream(file); int nbrbytesread = 0; byte[] buffer = new byte[1024 * 2]; try { while ((nbrbytesread = fileinputstream.read(buffer)) > -1) { socket.getoutputstream().write(buffer, 0, nbrbytesread); } } { fileinputstream.close(); } } public static void main(string[] args) throws interruptedexception { // create server waits client request connection. filesharedserver server = new filesharedserver(); system.out.println("new thread"); thread thread = new thread(server); thread.start(); } }
do need class or couple of lines in main? on bottom.
it's on wifi network , need 2 clients @ once, or more :)
the problem here running single thread on server. thread accepts connection, writes server id connection, reads connection. thread continues read connection until -1 received, @ point thread exits. @ no point thread try accept second connection; serversocket.accept() called once. result, can handle 1 client.
what need split class 2 separate classes. in first class, run() method goes loop, calling serversocket.accept(), , each time method returns socket, creates instance of second class, hands socket, , starts it, after loops serversocket.accept() call.
the second class identical class you've written, except doesn't contain serversocket.accept() call. instead, socket
member variable initialized, first class, before started. can handling of socket, sending server id, receiving , handling commands, etc., existing code does.
Comments
Post a Comment