Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package klientserwer;
- import java.net.*;
- import java.io.*;
- /**
- *
- * @author hubert
- */
- class Klient_Watek1 extends Thread
- {
- Socket socket;
- InputStream in;
- OutputStream out;
- BufferedReader fromKeyboard = new BufferedReader(new InputStreamReader(System.in));
- Klient_Watek1(Socket s, InputStream in, OutputStream out)
- {
- socket =s;
- this.in = in;
- this.out=out;
- }
- @Override
- public void run()
- {
- try
- {
- int inf=0;
- while(inf==0)
- {
- System.out.print("Klient wysyła-> ");
- //Czytanie danych ze standardowego urzadzenia wejscia(klawiatury) po linii
- String data = fromKeyboard.readLine();
- //wysylanie danych strumieniem wyjsciowym do serwera
- out.write(data.getBytes());
- out.write("\r\n".getBytes());// dokladanie znakĂłw konca wiersza
- if(data.toLowerCase().equals("exit"))
- {
- // W pzypadku gdy uzytkownik wpisal 'exit' zamykamy strumienie, polaczenie oraz aplikacje
- in.close();
- out.close();
- socket.close();
- inf=1;
- }
- }
- }catch(IOException ioe) {
- }
- }
- }
- class Odbieranie extends Thread
- {
- }
- public class Klient1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try{
- Socket socket = new Socket("localhost", 2222);
- System.out.println("Polaczony ...");
- InputStream in = socket.getInputStream();
- OutputStream out = socket.getOutputStream();
- Klient_Watek1 t = new Klient_Watek1(socket, in, out);
- t.start();
- while(true){
- int k = 0;
- StringBuffer sb = new StringBuffer();
- while((k=in.read())!=-1 && k!='\n')// czytanie po jednym bajcie
- sb.append((char)k); // zamiana bajtu na char i wstawianie do bufora
- //pobieranie danych z bufora i obcinanie pustych znakĂłw
- String data = sb.toString().trim();
- if(data.toLowerCase().equals("exit"))
- {
- in.close();
- out.close();
- socket.close();
- System.exit(0);
- }
- System.out.println(data);
- }
- // obsluga wyjatkow
- }catch(UnknownHostException uhe){
- System.err.println(uhe);
- }catch(IOException ioe){
- System.err.println(ioe);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement