Search
/* * 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 cz.cvut.fel.pjv.cv8_2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.LinkedList; /** * * @author mudromar */ public class Server { private static LinkedList<String> data = new LinkedList<String>(); private static LinkedList<Thread> list = new LinkedList<Thread>(); public static void main(String[] args) throws IOException { int clientCount=0; ServerSocket ss = new ServerSocket(20000); System.out.println("Socket vytvoren"); while (clientCount<5) { Socket s = ss.accept(); System.out.println("Klient pripojen"); clientCount++; ServerProcess proc = new ServerProcess(data,s); Thread t = new Thread(proc); list.add(t); t.start(); } for (String clientData : data) { System.out.println(clientData); } } }
/* * 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 cz.cvut.fel.pjv.cv8_2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.LinkedList; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author mudromar */ public class ServerProcess implements Runnable { private LinkedList<String> data; private Socket s; public ServerProcess(LinkedList<String> data, Socket s) { this.data = data; this.s = s; } @Override public void run() { try (InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintWriter pw = new PrintWriter(os);) { String clientData = br.readLine(); System.out.println("Klient poslal:" + clientData); synchronized (data) { data.add(clientData); } pw.println("KONEC SPOJENI"); pw.flush(); synchronized (data) { for (String string : data) { pw.println(string); } } } catch (IOException ex) { Logger.getLogger(ServerProcess.class.getName()).log(Level.SEVERE, null, ex); } try { s.close(); } catch (IOException ex) { Logger.getLogger(ServerProcess.class.getName()).log(Level.SEVERE, null, ex); } } }