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 echoserwer;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- *
- * @author kie
- */
- public class EchoServerThread implements Runnable{
- protected Socket socket;
- public EchoServerThread(Socket clientSocket){
- this.socket = clientSocket;
- }
- public void run(){
- BufferedReader brinp = null;
- DataOutputStream out = null;
- //OutputStream out;
- // InputStream brinp;
- try{
- brinp = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new DataOutputStream(socket.getOutputStream());
- String threadName = Thread.currentThread().getName();
- // out = socket.getOutputStream();
- // brinp= socket.getInputStream();
- }
- catch(IOException e){
- System.out.println("Błąd przy tworzeniu strumieni.");
- System.exit(-1);
- } System.out.println("Zakończona inicjalizacja strumieni...");
- System.out.println("Rozpoczęcie pętli głównej...");
- while(true){
- try{
- String line = brinp.readLine();
- // int line = brinp.read();
- System.out.println("Odczytano linię: " + line);
- if(line == null || "quit".equals(line)){
- try{ socket.close();
- }
- catch(IOException e){
- System.out.println( "Błąd przy zamykaniu gniazda serwerowego.");
- }
- System.out.println("Zakończenie pracy...");
- System.exit(0);
- }
- out.writeBytes(line + "\n");
- // out.write(line + "\n\r");
- System.out.println("Wysłano linię: " + line);
- }
- catch(IOException e){
- System.out.println("Błąd wejścia-wyjścia.");
- System.exit(-1);
- }
- }
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement