java - Socket server stuck -
I am trying to create a server / client to send text to the server from the client, then a correct message or something Returning the same way back to the customer, but for some error which I can not see, either the customer gets stuck right before sending the customer back immediately, or the customer does not receive the message (I think this is the first). Any help is appreciated.
This is the server code:
extends the class ActiveServer thread {InputStream inStream; OutputStream Outstream; Public ActiveServer (InputStream Instream, OutputStream Outstream) {this.instream = inStream; This.outstream = outstream; } @ Override Public Wide Run () {Boolean Rate = Falls; Try (buffer reader = new bufftterder (new inputstream reader (instream)); printriter writer = new print witter (outstream);) {string line = empty; While ((line = reader. Readline ())! = Null {string [] str = line.split (";"); line = faucet; switch (str [0]) {case "insert": // ret = SQLOptions.insert (str [1], str [2]); println (str [1]); break ;; author; print (rate); writer.flush (); // as far as i can see It is stuck at the end of it, but I do not know why}} grip (IOException e) {e.printStackTrace ();}}} public class server {private constant final integer ports = 39165; public static zero main (string [ ] Args) {try (server server server = new server Port ();) {System.out.println ("Online Services"); Executable Service Service = Examiner. Niffix and Pull (10); While (True) {Socket Customer = server.accept (); InetAddress ip = client.getInetAddress (); SimpleDetermutation SDF = New SimpleDormat ("DD-MM-YAHH: mm: SS"); DateTime = New Date (); System.out.print (sdf.format (time)); System.out.println ("" + Ip + "connected"); InputStreamInstream = client.getInputStream (); OutputStream Outstream = client.getOutputStream (); Service.execute (new ActiveServer (InStream, Outstream)); }} Hold (IOException e) {e.printStackTrace (); }}} And here the client code goes:
public class telnet {static console console = system.console (); Public Static Zero Connect (String IP, String Port) (Try (Socket Socket = New Socket (IP, Integer. PRIIIN (Port)); Printer Writer = New Print Vetter (Socket. Acoustputstream); BeefFinder Reader Reader = New Buffered Reader (New InputStreamReader (socket.getInputStream ());) {string msg = null; while (true) {msg = console.readLine (); writer.println (MSG); writer.flush (); If .equals ("quit")) {System.out.println ("getting out ..."); breakage;} string input = r Fear readline (); println (input);}} hold (NumberFormatException e) {e.printStackTrace ();} hold (unknownhostexception e) {e.printStackTrace ();} hold (IOException e) {e.printStackTrace () ;}} Public static zero principal (string [] args) {if (args.length & lt; 2) {err.println ("telnet "); return; } If (console == null) {err.println ("console is not available"); return;} connect (args [0], args [1]); on the server side, you write the reaction without a canceled new line Are:
author. Print; But on the client side, you read up to the end of the line:
string input = reader. Readline (); The document says: A line of text reads one line in any one line feed ('\ n'), carries return (' \ R '), or a carriage return is terminated immediately by any linefeed.
In this way, the client will always wait for the new line sequence that the server will never send.
Comments
Post a Comment