示例#1
0
 void run() {
   try {
     // 1. creating a socket to connect to the server
     requestSocket = new Socket("192.168.5.50", 2004);
     System.out.println("Connected to localhost in port 2004");
     // 2. get Input and Output streams
     out = new ObjectOutputStream(requestSocket.getOutputStream());
     out.flush();
     in = new ObjectInputStream(requestSocket.getInputStream());
     // 3: Communicating with the server
     do {
       try {
         message = (String) in.readObject();
         System.out.println("server>" + message);
         sendMessage("Hi server, Brian's Computer.");
         message = "bye";
         sendMessage(message);
       } catch (ClassNotFoundException classNot) {
         System.err.println("data received in unknown format");
       }
     } while (!message.equals("bye"));
   } catch (UnknownHostException unknownHost) {
     System.err.println("You are trying to connect to an unknown host!");
   } catch (IOException ioException) {
     ioException.printStackTrace();
   } finally {
     // 4: Closing connection
     try {
       in.close();
       out.close();
       requestSocket.close();
     } catch (IOException ioException) {
       ioException.printStackTrace();
     }
   }
 }