Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.net.*;
  4.  
  5. public class PhoneServer
  6. { // The port number on which the server will be listening
  7.  
  8. private static int port = 2014; // The server socket.
  9. private static ServerSocket listener = null; // The client socket.
  10. private static Socket clientSocket = null;
  11. public static void main(String[] args)throws Exception
  12. {
  13. boolean listening = true;
  14. try
  15. {
  16. listener = new ServerSocket(port);
  17. while (listening)
  18. {
  19. new ClientThread(listener.accept()).run();
  20. }
  21. }
  22. catch (Exception e)
  23. {
  24. System.out.println("I/O failure: " + e.getMessage());
  25. }
  26. listener.close();
  27. /**
  28. * Open a server socket on the specified port number(2014)
  29. * and monitor the port for connection requests . When a
  30. * connection request is received, create a client request
  31. * thread, passing to its constructor a reference to the
  32. * Socket object that represents the established connection
  33. * with the client.
  34. */
  35. }
  36. }
  37. class ClientThread extends Thread
  38. {
  39. Socket socket; //constructor
  40.  
  41. public ClientThread(Socket socket)
  42. {
  43. this.socket = socket; //Establishes connection
  44. }
  45. //implement the run method
  46. public void run()
  47. {
  48. handleConnection(socket);
  49. }
  50. //implement the handleConnection method here.
  51. public void handleConnection(Socket socket) throws IOException
  52. {
  53. List<ClientThread> contact = new ArrayList<ClientThread>();
  54.  
  55. BufferedReader in = new BufferedReader(new
  56. InputStreamReader(socket.getInputStream()));
  57. System.out.print("Received string: '");
  58.  
  59. while (!in.ready()) {}
  60. System.out.println(in.readLine()); // Read one line and output it
  61.  
  62. System.out.print("'\n");
  63. in.close();
  64.  
  65. //contact.add(new Contact(23, "Peter"));
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement