Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. public void run(){
  2. System.out.println("Welcome :" + userName);
  3.  
  4. System.out.println("Local Port :" + socket.getLocalPort());
  5. System.out.println("Server = " + socket.getRemoteSocketAddress() + ":" + socket.getPort());
  6.  
  7. try{
  8. PrintWriter serverOut = new PrintWriter(socket.getOutputStream(), false);
  9. InputStream serverInStream = socket.getInputStream();
  10. Scanner serverIn = new Scanner(serverInStream);
  11. // BufferedReader userBr = new BufferedReader(new InputStreamReader(userInStream));
  12. // Scanner userIn = new Scanner(userInStream);
  13.  
  14. while(!socket.isClosed()){
  15. if(serverInStream.available() > 0){
  16. if(serverIn.hasNextLine()){
  17. System.out.println(serverIn.nextLine());
  18. }
  19. }
  20. if(hasMessages){
  21. String nextSend = "";
  22. synchronized(messagesToSend){
  23. nextSend = messagesToSend.pop();
  24. hasMessages = !messagesToSend.isEmpty();
  25. }
  26. serverOut.println(userName + " > " + nextSend);
  27. serverOut.flush();
  28. }
  29. }
  30. }
  31. catch(IOException ex){
  32. ex.printStackTrace();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement