Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Serwer extends Thread {
- protected int port;
- public Serwer(int port) {
- this.port = port;
- }
- @Override public void run() {
- ServerSocket serwer = null;
- try {
- serwer = new ServerSocket(port);
- } catch (IOException e) {}
- ServerThread t;
- while(true) {
- try {
- Socket channel = serwer.accept();
- t = new ServerThread(channel);
- t.start();
- } catch (IOException e) {}
- }
- }
- }
- class ServerThread extends Thread {
- protected Socket channel;
- protected PrintWriter out;
- public static BufferedReader in = null;
- public ServerThread(Socket s) {
- channel = s;
- try {
- in = new BufferedReader(new InputStreamReader(s.getInputStream()));
- out = new PrintWriter(s.getOutputStream(), true);
- } catch (Exception e) {}
- }
- public void wyslij(String wiadomosc) {
- try {
- out.println(wiadomosc);
- out.flush();
- } catch (Exception e) {}
- }
- public String odbierz() {
- String wiadomosc="";
- try {
- wiadomosc = in.readLine();
- } catch (Exception e) {}
- return wiadomosc;
- }
- protected void rozlacz() {
- try {
- channel.close();
- } catch(Exception ex) {}
- }
- @Override public void run() {
- String wiadomosc;
- while (channel.isConnected()) {
- wiadomosc = odbierz();
- System.out.print(wiadomosc);
- if (wiadomosc!="") {
- wyslij(wiadomosc);
- wiadomosc="";
- }
- }
- }
- }
- public class SerwerTest{
- public static void main(String[] args){
- Serwer serwer = new Serwer(8000);
- serwer.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement