server and client in java -
i have problem client-server. when run server, after client, server write client connected server can write nothing in console. can check problem? because i'm new in java. try scanner doesn't work too.
server:
package ts_server; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class server { private static final int port = 50000; static boolean flaga = true; private static serversocket serversocket; private static socket clientsocket; public static void main(string[] args) throws ioexception { serversocket = null; try { serversocket = new serversocket(port); } catch(ioexception e) { system.err.println("could not listen on port: "+port); system.exit(1); } system.out.print("waiting connection..."); thread t = new thread(new runnable() { public void run() { try { while(flaga) { system.out.print("."); thread.sleep(1000); } } catch(interruptedexception ie) { // } system.out.println("\nclient connected on port "+port); } }); t.start(); clientsocket = null; try { clientsocket = serversocket.accept(); flaga = false; } catch(ioexception e) { system.err.println("accept failed."); t.interrupt(); system.exit(1); } final printwriter out = new printwriter(clientsocket.getoutputstream(),true); final bufferedreader in = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); t = new thread(new runnable() { public void run() { try { thread.sleep(5000); while(true) { out.println("ping"); system.out.println(system.currenttimemillis()+" ping sent"); string input = in.readline(); if(input.equals("pong")) { system.out.println(system.currenttimemillis()+" pong received"); } else { system.out.println(system.currenttimemillis()+" wrong answer"); out.close(); in.close(); clientsocket.close(); serversocket.close(); break; } thread.sleep(5000); } } catch(exception e) { system.err.println(system.currenttimemillis()+" unexpected error"); } } }); t.start(); } }
client:
package ts_client; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; public class client { private static final int port = 50000; private static final string host = "localhost"; public static void main(string[] args) throws ioexception { socket socket = null; try { socket = new socket(host, port); } catch(exception e) { system.err.println("could not connect "+host+":"+port); system.exit(1); } final printwriter out = new printwriter(socket.getoutputstream(),true); final bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); thread t = new thread(new runnable() { public void run() { long start = system.currenttimemillis(); while (true) { try { string input = in.readline(); if (input != null) { system.out.println(system.currenttimemillis() + " server: " + input); } if (input.equals("ping")) { if(system.currenttimemillis()-start>30000) { out.println("pon g"); system.out.println(system.currenttimemillis() + " client: pon g"); break; } out.println("pong"); system.out.println(system.currenttimemillis() + " client: pong"); } } catch (ioexception ioe) { // } } } }); t.start(); out.close(); in.close(); socket.close(); } }
don't close streams , socket until client running.
out.close(); in.close(); socket.close();
Comments
Post a Comment