단일 연결 Tcp 서버 구현

10539 단어 tcp서버단일 연결
단일 클라이언트만 연결할 수 있습니다.start 시작, stop 정지.

  
  
  
  
  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. public class TcpServer { 
  9.      
  10.     private int port; 
  11.     private ServerSocket serverSocket = null
  12.     private Socket connected; 
  13.     private PrintWriter outToClient; 
  14.      
  15.     private boolean bRunning = true
  16.      
  17.     public TcpServer(String strPort) { 
  18.         this.port = Integer.parseInt(strPort); 
  19.     } 
  20.      
  21.     public void start() { 
  22.         System.out.println("TCPServer waiting for client on port: " + port); 
  23.  
  24.         try { 
  25.             serverSocket = new ServerSocket(port); 
  26.         } catch (IOException e) { 
  27.             e.printStackTrace(); 
  28.         } 
  29.          
  30.         ServerThread thread = new ServerThread(); 
  31.         thread.start(); 
  32.     } 
  33.  
  34.     public void stop() { 
  35.         bRunning = false
  36.          
  37.         try { 
  38.             connected.close(); 
  39.         } catch (IOException e) { 
  40.             e.printStackTrace(); 
  41.         } 
  42.     } 
  43.      
  44.     class ServerThread extends Thread { 
  45.          
  46.         public void run() { 
  47.             try { 
  48.                 while (bRunning) { 
  49.                     //TODO:  ( BindException), serverSocket  
  50.                     connected = serverSocket.accept(); 
  51.                     System.out.println(" THE CLIENT" + " " 
  52.                             + connected.getInetAddress() + ":" 
  53.                             + connected.getPort() + " IS CONNECTED "); 
  54.      
  55.                     BufferedReader inFromClient = new BufferedReader( 
  56.                             new InputStreamReader(connected.getInputStream(), "gb2312")); 
  57.                      
  58.                     //BufferedReader inFromClient = new BufferedReader( 
  59.                     //      new InputStreamReader(connected.getInputStream())); 
  60.                      
  61.                     //InputStream is = connected.getInputStream(); 
  62.                      
  63.                     outToClient = new PrintWriter( 
  64.                             connected.getOutputStream(), true); 
  65.      
  66.                     while (bRunning) { 
  67.                         System.out.println("SEND(Type Q or q to Quit):"); 
  68.                          
  69.                         String fromclient = inFromClient.readLine(); 
  70.                         System.out.println("received: " + fromclient); 
  71.                                              
  72.                         if (fromclient != null) { 
  73.                             if (fromclient.equals("quit")) { 
  74.                                 TcpServer.this.stop(); 
  75.                                 break
  76.                             } else { 
  77.                                 //  fromclient 
  78.                             } 
  79.                              
  80.                         } 
  81.                     } 
  82.                 } 
  83.             } catch (IOException ex) { 
  84.                 ex.printStackTrace(); 
  85.             } 
  86.      
  87.         } 
  88.     } 
  89.      
  90.     public void send(String req) { 
  91.         sendReply(req); 
  92.     } 
  93.  
  94.     public void sendReply(String cmd) { 
  95.         if (outToClient != null) { 
  96.             outToClient.println(cmd); 
  97.             outToClient.flush(); 
  98.         } 
  99.     } 

좋은 웹페이지 즐겨찾기