multithreading - Java socketserver how to test? -
i want receive text files through socket connection in java, set server end before continue client know if code made works, except have no idea how test this.
any appreciated..
edit: know port open , listening requests, want test happens if receives anything, create file input , can test simulation(sending file or bytes dont know)?
public class server { private static int port = 8080; private static int maxconnections = 100000; // listen incoming connections , handle them public static void startserver() { int = 0; try { serversocket listener = new serversocket(port); socket server; system.out.println("started server on port:" + port); while ((i++ < maxconnections) || (maxconnections == 0)) { runserver connection; server = listener.accept(); runserver conn_c = new runserver(server); thread t = new thread(conn_c); t.start(); system.out.println("created new thread"); } } catch (ioexception e) { e.printstacktrace(); } } } class runserver implements runnable { private socket server; runserver(socket server) { this.server = server; } public void run() { int bytesread; int current = 0; bufferedoutputstream bufferedoutputstream = null; fileoutputstream fileoutputstream = null; datainputstream clientdata = null; file file = null; try { // creating connection. system.out.println("connected."); // receive file byte[] bytearray = new byte[6022386]; system.out.println("please wait downloading file"); // reading file socket inputstream inputstream = server.getinputstream(); file = new file("toread.txt"); clientdata = new datainputstream(inputstream); fileoutputstream = new fileoutputstream(file); bufferedoutputstream = new bufferedoutputstream(fileoutputstream); bytesread = inputstream.read(bytearray, 0, bytearray.length); current = bytesread; { bytesread = inputstream.read(bytearray, current, (bytearray.length - current)); if (bytesread >= 0) current += bytesread; } while (bytesread > -1); bufferedoutputstream.write(bytearray, 0, current); bufferedoutputstream.flush(); readerhelper.readtextfile(file); } catch (ioexception e) { e.printstacktrace(); } catch (numberformatexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } { try { if (fileoutputstream != null) fileoutputstream.close(); if (bufferedoutputstream != null) bufferedoutputstream.close(); if (clientdata != null) clientdata.close(); if (server != null) server.close(); } catch (ioexception e) { e.printstacktrace(); } } }
well, socket on port 8080 right?
you can open browser , type: http://localhost:8080. browser create connection , line
server = listener.accept();
will "unlock". if wanna test if socket listenning do. reading stream see first message of http protocol.
Comments
Post a Comment