Advertisement
Guest User

server

a guest
Aug 24th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8.  
  9. public class Server_X_Client {
  10. public static void main(String args[]){
  11.  
  12.  
  13. Socket s=null;
  14. ServerSocket ss2=null;
  15. System.out.println("Server Listening......");
  16. try{
  17. ss2 = new ServerSocket(4445); // can also use static final PORT_NUM , when defined
  18.  
  19. }
  20. catch(IOException e){
  21. e.printStackTrace();
  22. System.out.println("Server error");
  23.  
  24. }
  25.  
  26. while(true){
  27. try{
  28. s= ss2.accept();
  29. System.out.println("connection Established");
  30. ServerThread st=new ServerThread(s);
  31. st.start();
  32.  
  33. }
  34.  
  35. catch(Exception e){
  36. e.printStackTrace();
  37. System.out.println("Connection Error");
  38.  
  39. }
  40. }
  41.  
  42. }
  43.  
  44. }
  45.  
  46. class ServerThread extends Thread{
  47.  
  48. String line=null;
  49. BufferedReader is = null;
  50. PrintWriter os=null;
  51. Socket s=null;
  52.  
  53. public ServerThread(Socket s){
  54. this.s=s;
  55. }
  56.  
  57. public void run() {
  58. try{
  59. is= new BufferedReader(new InputStreamReader(s.getInputStream()));
  60. os=new PrintWriter(s.getOutputStream());
  61.  
  62. }catch(IOException e){
  63. System.out.println("IO error in server thread");
  64. }
  65.  
  66. String Name;
  67. try {
  68. os.println("Enter Your Name:");
  69. Name=is.readLine();
  70. os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or type QUIT to Exit");
  71. line=is.readLine();
  72. while(line.compareTo("QUIT")!=0){
  73.  
  74. os.println(line);
  75. os.flush();
  76. System.out.println("Response to Client : "+line);
  77. line=is.readLine();
  78. }
  79. } catch (IOException e) {
  80.  
  81. line=this.getName(); //reused String line for getting thread name
  82. System.out.println("IO Error/ Client "+line+" terminated abruptly");
  83. }
  84. catch(NullPointerException e){
  85. line=this.getName(); //reused String line for getting thread name
  86. System.out.println("Client "+line+" Closed");
  87. }
  88.  
  89. finally{
  90. try{
  91. System.out.println("Connection Closing..");
  92. if (is!=null){
  93. is.close();
  94. System.out.println(" Socket Input Stream Closed");
  95. }
  96.  
  97. if(os!=null){
  98. os.close();
  99. System.out.println("Socket Out Closed");
  100. }
  101. if (s!=null){
  102. s.close();
  103. System.out.println("Socket Closed");
  104. }
  105.  
  106. }
  107. catch(IOException ie){
  108. System.out.println("Socket Close Error");
  109. }
  110. }//end finally
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement