Guest User

Untitled

a guest
Jun 2nd, 2011
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class WhiteboardChatServer extends Thread {
  6.  
  7.  
  8. //Sprivate DataInputStream streamIn = null;
  9.  
  10. private Socket s;
  11. public static ArrayList osChat =new ArrayList();
  12. String line;
  13. private ServerSocket ss;
  14. public WhiteboardChatServer(Socket s) {
  15. this.s=s;
  16. start();
  17. }
  18.  
  19. public void run() {
  20. try {
  21. run1();
  22. } catch (IOException ex) {
  23.  
  24. ex.printStackTrace();
  25. }
  26. }
  27.  
  28. public void run1() throws IOException {
  29. BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "ISO-8859-1"));
  30. synchronized(osChat) {
  31. osChat.add(out);
  32. }
  33. //streamIn = new DataInputStream(new BufferedInputStream(s.getInputStream()));
  34. BufferedReader streamIn = new BufferedReader(new InputStreamReader(s.getInputStream(), "ISO-8859-1"));
  35.  
  36. while((line=streamIn.readLine()) != null) {
  37.  
  38. System.out.println(line);
  39. synchronized(osChat) {
  40. for(Iterator it=osChat.iterator(); it.hasNext(); ) {
  41. BufferedWriter bw = (BufferedWriter) it.next();
  42.  
  43. bw.write(line+"\n");
  44. bw.flush();
  45. }
  46. }
  47.  
  48. }
  49.  
  50. public WhiteboardChatServer (String arg) throws Exception {
  51.  
  52. try
  53. {
  54. ss = new ServerSocket(Integer.parseInt(arg));
  55. }
  56. catch(Exception e)
  57. {
  58. System.out.print(e);
  59. }
  60.  
  61.  
  62. while(true) {
  63. s = ss.accept();
  64. System.out.print("ChatAccepted\n");
  65. new WhiteboardChatServer(s);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment