Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad_ii_1;
- import java.io.*;
- import java.net.*;
- public class Klient
- {
- private Socket socket;
- /* Konstruktor próbuje połączyć się z serwerem */
- Klient()
- {
- try{
- socket = new Socket("localhost", 2020);
- System.out.println("Klient dziala");
- }catch(IOException e) {
- System.out.println("Uruchom serwer");
- System.exit(1);
- }
- }
- void uruchom() throws Exception
- {
- // 1. Otworzenie strumienia (wejściowego lub wyjściowego) i
- // skojarzenie go z gniazdem
- BufferedReader in = new BufferedReader(
- new InputStreamReader(
- socket.getInputStream()));
- /* 2. Działanie klienta */
- String komunikat;
- while(true) {
- komunikat = in.readLine();
- if(komunikat==null) break;
- System.out.println(komunikat);
- }
- /* 3. Czynności po zakończeniu współpracy z serwerem */
- socket.close();
- }//koniec funkcji uruchom()
- public static void main(String args[]) throws Exception
- {
- Klient client = new Klient();
- client.uruchom();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement