javasocket客户端连接服务端简单示例

12/12/2015来源:Java教程人气:1974

本例只做简单功能演示,代码并不严谨,只是说明客户端如何实现连接服务端简单代码。

代码在集成Eclipse工具下测试编译运行环境如下图所示:

 

客户端echoClient.java代码:

 1 package com.zhengzz.echo;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.PRintWriter;
 7 import java.net.Socket;
 8 import java.net.UnknownHostException;
 9 
10 class EchoC{
11     
12     private Socket socket;
13     
14     public EchoC() {
15         // TODO Auto-generated constructor stub
16         try {
17             socket = new Socket("localhost", 60000);
18         } catch (UnknownHostException e) {
19             e.printStackTrace();
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24     
25     public void Client() {
26         try {
27             BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
28             PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
29             BufferedReader lbr = new BufferedReader(new InputStreamReader(System.in));
30             
31             String linestr = null;
32             while ((linestr = lbr.readLine()) != null) {
33                 
34                 pw.println(linestr);
35                 
36                 System.out.println(br.readLine());
37                 
38                 if (linestr.equals("bye#")) {
39                     break;
40                 }
41             }
42             
43             
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47     }
48 }
49 
50 public class echoClient {
51 
52     public static void main(String[] args) {
53         new EchoC().Client();
54     }
55 
56 }

服务端echoServer.java代码:

 1 package com.zhengzz.echo;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.PrintWriter;
 7 import java.net.ServerSocket;
 8 import java.net.Socket;
 9 
10 class EchoS{
11     
12     private ServerSocket serverSocket;
13     
14     public EchoS() {
15         
16         try {
17             serverSocket = new ServerSocket(60000);
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21         
22     }
23     
24     public void Server() {
25         while (true) {
26             try {
27                 Socket socket = serverSocket.accept();
28                 
29                 System.out.println("client:" + socket.getInetAddress() + ":" + socket.getLocalPort());
30 
31                 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
32                 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
33 
34                 String linestr;
35 
36                 while ((linestr = br.readLine()) != null) {
37 
38                     System.out.println(linestr);
39 
40                     pw.println("--->" + linestr);
41 
42                 }
43             } catch (IOException e) {
44                 System.out.println("连接断开:(");
45             }
46 
47         }
48 }
49 }
50 
51 public class echoServer {
52 
53     public static void main(String[] args) {
54         new EchoS().Server();
55     }
56 
57 }

我在Eclipse工具下直接调试结果如下: