Advertisement
fak123

PWSI Z1 zajecia4 Klient

May 30th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package zad_ii_1;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6.     public class Klient
  7.     {
  8.         private Socket socket;
  9.     /* Konstruktor próbuje połączyć się z serwerem */
  10.         Klient()
  11.         {
  12.             try{
  13.                 socket = new Socket("localhost", 2020);
  14.                 System.out.println("Klient dziala");
  15.             }catch(IOException e) {
  16.                 System.out.println("Uruchom serwer");
  17.                 System.exit(1);
  18.             }
  19.         }  
  20.     void uruchom() throws Exception
  21.     {
  22.     // 1. Otworzenie strumienia (wejściowego lub wyjściowego) i
  23.     // skojarzenie go z gniazdem
  24.        BufferedReader in = new BufferedReader(
  25.                new InputStreamReader(
  26.                        socket.getInputStream()));
  27.                
  28.     /* 2. Działanie klienta */
  29.        String komunikat;
  30.        while(true) {
  31.            komunikat = in.readLine();
  32.            if(komunikat==null) break;
  33.            System.out.println(komunikat);
  34.        }
  35.     /* 3. Czynności po zakończeniu współpracy z serwerem */
  36.     socket.close();
  37.     }//koniec funkcji uruchom()
  38.    
  39.    
  40.     public static void main(String args[]) throws Exception
  41.     {
  42.         Klient client = new Klient();
  43.         client.uruchom();
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement