socket 과 server socket

2967 단어 자바socket
http://blog.sina.com.cn/s/blog_6189ab480100i95e.html
public class Server {
 public static void main(String[] args) {
  Socket socket=null;
  BufferedReader br=null;
  PrintWriter pw=null;
  try {
   //     ,   3081  
   ServerSocket server=new ServerSocket(3081);
   while(true){
    
    //       ,         ,         socket  
    //          ,            ,        
    socket=server.accept();
    System.out.println("     :"+socket.getRemoteSocketAddress());
    // socket      ,              
    InputStream in=socket.getInputStream();
    //InputStreamReader          
    br=new BufferedReader(new InputStreamReader(in));
    //        
    String info=br.readLine();
    System.out.println(info);
    
    
    OutputStream out=socket.getOutputStream();
    pw=new PrintWriter(out);
    pw.println("    :    ");
    pw.flush();
   }
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   
   try {
    pw.close();
    br.close();
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }
}
 
 

 
public class Client {
 public static void main(String[] args) {
  Socket socket=null;
  PrintWriter pw=null;
  BufferedReader br=null;
  try {
   //  socket  ,       IP      
   socket=new Socket("localhost",3081);
   //  socket        
   OutputStream out=socket.getOutputStream();
   //          
   pw=new PrintWriter(out);
   
   //        
   pw.println("    :    ");
   //   ,          
   pw.flush();
   
   InputStream in=socket.getInputStream();
   
   br=new BufferedReader(new InputStreamReader(in));
   String info=br.readLine();
   System.out.println(info);
   
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally{
   try {
    pw.close();
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}
 
 
 


public class Client {
 public static void main(String[] args) {
  Socket socket=null;
  PrintWriter pw=null;
  BufferedReader br=null;
  try {
   //  socket  ,       IP      
   socket=new Socket("localhost",3081);
   //  socket        
   OutputStream out=socket.getOutputStream();
   //          
   pw=new PrintWriter(out);
   
   //        
   pw.println("    :    ");
   //   ,          
   pw.flush();
   
   InputStream in=socket.getInputStream();
   
   br=new BufferedReader(new InputStreamReader(in));
   String info=br.readLine();
   System.out.println(info);
   
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally{
   try {
    pw.close();
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

좋은 웹페이지 즐겨찾기