echoclient.java


  1. package hsz.ITDP.rac.uebungen.echoClientSrv;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.Socket;
  7.  
  8.  
  9. public class echoclient {
  10. public static void main(String[] args) throws IOException {
  11.         BufferedReader in = null;
  12.         PrintWriter out = null;
  13.         BufferedReader user = null;
  14.         Socket sock = null;
  15.         String srvIP = "localhost"; //args[0];
  16.         int srvPort = 9998;  // Integer.parseInt(args[1]);
  17.        
  18.         try {
  19.                 sock = new Socket(srvIP, srvPort);
  20.                 in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  21.                 user = new BufferedReader(new InputStreamReader(System.in));
  22.                 out = new PrintWriter(sock.getOutputStream(),true);
  23.                 boolean exit = false;
  24.                 while(!exit) {
  25.                         String userwrote = user.readLine();
  26.                         if(userwrote.equals("exit")) {
  27.                                 exit = true;
  28.                         } else {
  29.                                 out.println(userwrote);
  30.                                 System.out.println(in.readLine());
  31.                         }
  32.                 }
  33.                
  34.         } catch (Exception e) {
  35.                 // TODO: handle exception
  36.         } finally {
  37.                 if (in != null)
  38.                         in.close();
  39.                 if (user != null)
  40.                         user.close();
  41.                 if (out != null)
  42.                         out.close();
  43.                 if (sock != null)
  44.                         sock.close();
  45.         }
  46. }
  47. }

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Use <fn>...</fn> to insert automatically numbered footnotes.
  • You can use the <go> tags just like the <a> for nicer urls.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.