Java Socket编程服务器端和客户端的一个简单实例

注:本程序在MyEclipse8.5环境下运行通过,运行时请先启动服务器端,在启动客户端,否则会出错。

服务器端:

 1 import java.io.*;
 2 import java.net.*;
 3 
 4 public class EchoServer {
 5     private int port=8000;
 6     private ServerSocket serverSocket;
 7     
 8     public EchoServer() throws IOException{
 9         serverSocket=new ServerSocket(port);
10         System.out.println("服务器启动");
11         }
12     public String echo(String msg){
13         return "echo:"+msg;
14     }
15     public PrintWriter getWriter(Socket socket)throws IOException{
16         OutputStream socketOut=socket.getOutputStream();
17         return new PrintWriter(socketOut,true);
18     }
19     public BufferedReader getReader(Socket socket)throws IOException{
20         InputStream socketIn=socket.getInputStream();
21         return new BufferedReader(new InputStreamReader(socketIn));
22     }
23     public void service(){
24         while(true){
25             Socket socket=null;
26             try{
27                 socket=serverSocket.accept();//等待客户端连接
28                 System.out.println("New aonnection accepted"
29                                         +socket.getInetAddress()+":"
30                                         +socket.getPort());
31                 BufferedReader br=getReader(socket);
32                 PrintWriter pw=getWriter(socket);
33                 
34                 String msg=null;
35                 while((msg=br.readLine())!=null){
36                     System.out.println(msg);
37                     pw.println(echo(msg));
38                     if(msg.equals("bye"));
39                         break;
40                 }
41             }catch(IOException e){e.printStackTrace();
42             }finally{
43                 try{
44                     if(socket!=null)
45                         socket.close();
46                 }catch(IOException e){e.printStackTrace();
47                 }
48             }
49         }
50     }
51 
52     public static void main(String[] args) throws IOException {
53         // TODO Auto-generated method stub
54         new EchoServer().service();
55     }
56 
57 }

 

客户端:

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class EchoClient {
 5     
 6     private String host="localhost";
 7     private int port=8000;
 8     private Socket socket;
 9 
10     public EchoClient()throws IOException{
11         socket = new Socket(host,port);
12     }
13     public static void main(String []args) throws IOException {
14         // TODO Auto-generated method stub
15         new EchoClient().talk();
16     }
17     private PrintWriter getWriter(Socket socket)throws IOException{
18         OutputStream socketOut=socket.getOutputStream();
19         return new PrintWriter(socketOut,true);
20     }
21     private BufferedReader getReader(Socket socket)throws IOException{
22         InputStream socketIn=socket.getInputStream();
23         return new BufferedReader(new InputStreamReader(socketIn));
24     }
25     public void talk()throws IOException{
26         try{
27             BufferedReader br=getReader(socket);
28             PrintWriter pw=getWriter(socket);
29             BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
30             String msg=null;
31             while((msg=localReader.readLine())!=null){
32                 pw.println(msg);
33                 System.out.println(br.readLine());            
34                 if(msg.equals("bye"))
35                     break;
36             }
37         }catch(IOException e){
38             e.printStackTrace();
39         }finally{
40             try{socket.close();}catch(IOException e){e.printStackTrace();}
41         }
42     }    
43 }

 

posted @ 2012-08-08 14:00  斗榖於菟  阅读(10257)  评论(1编辑  收藏  举报