Server端

 

01.ServerSocket server=new ServerSocket(1234);
02.Socket client=server.accept();
03.BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
04.PrintWriter pw=new PrintWriter(client.getOutputStream());
05.BufferedReader bw=new BufferedReader(new InputStreamReader(System.in));
06.while(true){
07.String str=br.readLine();
08.System.out.println("accept client info:"+str);
09.String send=bw.readLine();
10.//          System.out.println("answer message...");
11.pw.println("send:"+send);
12.pw.println("server已接收info....");
13.pw.flush();
14.if(send.equals("交易结束"))
15.break;
16.}
17.client.close();


client端代码:

 

 

 

 

01.Socket socket=new Socket(InetAddress.getLocalHost(),1234);
02.BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));//接收server消息
03.PrintWriter pw=new PrintWriter(socket.getOutputStream());
04.BufferedReader bw=new BufferedReader(new InputStreamReader(System.in));//send info
05.while(true){
06.System.out.println("client begin send message...");
07.String str=bw.readLine();
08.pw.println("str:"+str);
09.pw.flush();
10.if(str.equals("交易结束")){
11.break;
12.}
13.System.out.println("client:"+br.readLine());
14.}
15.socket.close();
16.catch (UnknownHostException e) {
17.// TODO Auto-generated catch block
18.e.printStackTrace();
19.catch (IOException e) {
20.// TODO Auto-generated catch block
21.e.printStackTrace();
22.}

 

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()