sockets - Java TCP Client Server Hangs Up? -
I'm currently having difficulty understanding why my code is not working, I have my client and server code down I've come to know that my problem is somewhere in the loops, but I'm not sure how to fix it so that it does not get stuck. I have searched for the forum for a while and some said that by adding a new letter it will be fixed, but I still have trouble.
My main question is how can I avoid this process and communicate properly. Can anyone point me in the right direction?
import java.io.buffferedReader; Import java.io.IOException; Import java.io.InputStreamReader; Import java.io.PrintStream; Import Java.net.Socket; Import java.net.UnknownHostException; Public class My_Client {public static zero main (string [] args) unknownhost extension, IOException {socket s = new socket ("localhost", 5555); BufferedReader R = New BufferedReader (new InputStreamReader (s.getInputStream ())); PrintStream w = new printstream (s.getOutputStream ()); W.print ("Hello world"); W.print ('\ n'); String line; While ((line = r. radline ())! = Null {System.out.println ("Received:" + line); //System.out.println""Rriter ");} W.close ();} } Import java.io.buffferedReader; Import java.io.IOException; Import java.io.InputStreamReader; Import java.io.PrintStream; Import java.net.ServerSocket; Import Java.net.Socket; --- ----------------------------------------------- --- ------------ Public class my_Server {Private static last full port = 5555; Public static zero main (string [] args) {try {ServerSocket ss = new ServerSocket (PORT); System.out Println ("server socket created"); while (true) {system.out.println ("waiting on connection Socket cs = ss.accept (); System.out.println ("client connected"); BufferedReader r = new BufferedReader (new InputStreamReader (cs.getInputStream ()); PrintStream w = new printstream (Cs.getOutputStream ()); String line; while ((line = r.readline ()) = faucet {w.print (line + "!!!!"); w.print ('\ n'); } System.out.println ("Client Disconnected"); R.close (); }} Hold (IOException e) {// TODO auto-generated cal block e.printStackTrace (); Both are ending up to EOS, and neither is closed until then. Then you have a classic deadlock. You have to reconsider your application protocol. You also on your PrintStream or PrintWriter at autoflush, or other flush () , but it is a minor matter compared to the above mistakes.
Comments
Post a Comment