Sunday, 15 May 2011

sockets - A simple java client server program -


So I have programmed this client server in Java for my Cloud Mini project. Note that this is a small module of a big project on which I am working. I need a string to send from the client to the server The server will return because it has returned to the client. (The code will be modified later as the string is processed before sending it back) The client will send the string when necessary on the server. This means that it is mandatory for the server to run indefinitely.

The problem I face here is that my server only works for the first time when the client sends the string. If I run the client with the second string the second time, then I will get back the same string I sent to the server before!

Here is my server program:

  public square server {public static Boolean x = true; Public stable string reply; Public Static Zero Main (String A []) Exception {System.out.println ("Access Server Console ..") throws; Socket echosocket = null; ServerSocket ServerSocket = Null; PrintWriter = Null Out; Buffet = thread in the thread; System.out.println ("Starting connection."); Boolean run flange = true; Try {serverSocket = new ServerSocket (77); While (runflags) {echoSocket = serverSocket.accept (); Out = new printIter (echoSocket.getOutputStream (), true); BufferedReader stdIn = new BufferedReader (New InputStreamReader (System.in)); While (x) {new = BufferedReader (new InputStreamReader (echoSocket.getInputStream ())); Answer = in.readLine (); If (answer! = Null) {x = false; }} System.out.println ("Received:" + Answer); Out.println (responses); System.out.println ("Sent Back:" + Answer); StdIn.close (); }} Hold (exception e) {System.out.println ("Exception in starting server:" + e.getMessage ()); } Finally {out.close (); In.close (); EchoSocket.close (); }}}   

This is my client program:

  public class client {public fixed string reply, temporary; Public stable boolean x = true; Public customer () {temp = "lala"; } Public Clients (String T) {Temp = t; } Public static zero principal (string [] args) throws IOException {socket eCoscat = null; PrintWriter = Null Out; Buffet = thread in the thread; Try {echoSocket = new socket ("localhost", 77); Out = new printIter (echoSocket.getOutputStream (), true); In = new BufferedReader (new InputStreamReader (echoSocket.getInputStream ())); } Hold (unknownHostException E) {System.err.println ("Host does not know about: localhost."); System.exit (1); } Hold (IOException e) {System.err.println ("Could not find I / O for connection: localhost."); System.exit (1); } BufferedReader stdIn = new BufferedReader (new InputStreamReader (System.in)); Temporary = "lala"; // This is the string to be sent. Println (temp); While (x) {Reply = in.readLine (); If (answer! = Null) {x = false; }} System.out.println ("Answer:" + Answer); Out.close (); In.close (); StdIn.close (); EchoSocket.close (); }}   

Can anyone know what is the problem here?

  while (x) {new = BufferedReader (new InputStreamReader (echoSocket.getInputStream ()) ); Answer = in.readLine (); If (answer! = Null) {x = false; }}   

Your server connects a client for the first time in this loop, and it sets the string to some input from the customer. However, it never enters this loop, because the values ​​of x do not change back to the right.

No comments:

Post a Comment