Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class WhiteboardChatServer extends Thread {
- //Sprivate DataInputStream streamIn = null;
- private Socket s;
- public static ArrayList osChat =new ArrayList();
- String line;
- private ServerSocket ss;
- public WhiteboardChatServer(Socket s) {
- this.s=s;
- start();
- }
- public void run() {
- try {
- run1();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public void run1() throws IOException {
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "ISO-8859-1"));
- synchronized(osChat) {
- osChat.add(out);
- }
- //streamIn = new DataInputStream(new BufferedInputStream(s.getInputStream()));
- BufferedReader streamIn = new BufferedReader(new InputStreamReader(s.getInputStream(), "ISO-8859-1"));
- while((line=streamIn.readLine()) != null) {
- System.out.println(line);
- synchronized(osChat) {
- for(Iterator it=osChat.iterator(); it.hasNext(); ) {
- BufferedWriter bw = (BufferedWriter) it.next();
- bw.write(line+"\n");
- bw.flush();
- }
- }
- }
- public WhiteboardChatServer (String arg) throws Exception {
- try
- {
- ss = new ServerSocket(Integer.parseInt(arg));
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- while(true) {
- s = ss.accept();
- System.out.print("ChatAccepted\n");
- new WhiteboardChatServer(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment