新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端。两个工程的读写操作线程类基本上完全相同。
服务端:
1import java.io.BufferedReader;
2import java.io.DataInputStream;
3import java.io.DataOutputStream;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.net.ServerSocket;
7import java.net.Socket;
8
9public class Server {
10
11 public static final int PORT = 8000;//监听的端口号
12
13 public static void main(String[] args) {
14 Server server = new Server();
15 server.init();
16 }
17
18 public void init() {
19 ServerSocket serverSocket = null;
20 try {
21 serverSocket = new ServerSocket(PORT);
22 while (true) {
23 Socket client = serverSocket.accept();
24 //一个客户端连接就开户两个线程处理读写
25 new Thread(new ReadHandlerThread(client)).start();
26 new Thread(new WriteHandlerThread(client)).start();
27 }
28 } catch (Exception e) {
29 e.printStackTrace();
30 } finally{
31 try {
32 if(serverSocket != null){
33 serverSocket.close();
34 }
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 }
39 }
40}
41
42/*
43 *处理读操作的线程
44 */
45class ReadHandlerThread implements Runnable{
46 private Socket client;
47
48 public ReadHandlerThread(Socket client) {
49 this.client = client;
50 }
51
52 @Override
53 public void run() {
54 DataInputStream dis = null;
55 try{
56 while(true){
57 //读取客户端数据
58 dis = new DataInputStream(client.getInputStream());
59 String reciver = dis.readUTF();
60 System.out.println("客户端发过来的内容:" + reciver);
61 }
62 }catch(Exception e){
63 e.printStackTrace();
64 }finally{
65 try {
66 if(dis != null){
67 dis.close();
68 }
69 if(client != null){
70 client = null;
71 }
72 } catch (IOException e) {
73 e.printStackTrace();
74 }
75 }
76 }
77}
78
79/*
80 * 处理写操作的线程
81 */
82class WriteHandlerThread implements Runnable{
83 private Socket client;
84
85 public WriteHandlerThread(Socket client) {
86 this.client = client;
87 }
88
89 @Override
90 public void run() {
91 DataOutputStream dos = null;
92 BufferedReader br = null;
93 try{
94 while(true){
95 //向客户端回复信息
96 dos = new DataOutputStream(client.getOutputStream());
97 System.out.print("请输入:\t");
98 // 键盘录入
99 br = new BufferedReader(new InputStreamReader(System.in));
100 String send = br.readLine();
101 //发送数据
102 dos.writeUTF(send);
103 }
104 }catch(Exception e){
105 e.printStackTrace();
106 }finally{
107 try {
108 if(dos != null){
109 dos.close();
110 }
111 if(br != null){
112 br.close();
113 }
114 if(client != null){
115 client = null;
116 }
117 } catch (IOException e) {
118 e.printStackTrace();
119 }
120 }
121 }
122}
客户端:
1import java.io.BufferedReader;
2import java.io.DataInputStream;
3import java.io.DataOutputStream;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.net.Socket;
7
8public class Client {
9
10 public static final String IP = "localhost";//服务器地址
11 public static final int PORT = 8000;//服务器端口号
12
13 public static void main(String[] args) {
14 handler();
15 }
16
17 private static void handler(){
18 try {
19 //实例化一个Socket,并指定服务器地址和端口
20 Socket client = new Socket(IP, PORT);
21 //开启两个线程,一个负责读,一个负责写
22 new Thread(new ReadHandlerThread(client)).start();
23 new Thread(new WriteHandlerThread(client)).start();
24 } catch (Exception e) {
25 e.printStackTrace();
26 }
27 }
28}
29
30/*
31 *处理读操作的线程
32 */
33class ReadHandlerThread implements Runnable{
34 private Socket client;
35
36 public ReadHandlerThread(Socket client) {
37 this.client = client;
38 }
39
40 @Override
41 public void run() {
42 DataInputStream dis = null;
43 try {
44 while(true){
45 //读取服务器端数据
46 dis = new DataInputStream(client.getInputStream());
47 String receive = dis.readUTF();
48 System.out.println("服务器端返回过来的是: " + receive);
49 }
50 } catch (IOException e) {
51 e.printStackTrace();
52 } finally{
53 try {
54 if(dis != null){
55 dis.close();
56 }
57 if(client != null){
58 client = null;
59 }
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 }
64 }
65}
66
67/*
68 * 处理写操作的线程
69 */
70class WriteHandlerThread implements Runnable{
71 private Socket client;
72
73 public WriteHandlerThread(Socket client) {
74 this.client = client;
75 }
76
77 @Override
78 public void run() {
79 DataOutputStream dos = null;
80 BufferedReader br = null;
81 try {
82 while(true){
83 //取得输出流
84 dos = new DataOutputStream(client.getOutputStream());
85 System.out.print("请输入: \t");
86 //键盘录入
87 br = new BufferedReader(new InputStreamReader(System.in));
88 String send = br.readLine();
89 //发送数据
90 dos.writeUTF(send);
91 }
92 } catch (IOException e) {
93 e.printStackTrace();
94 } finally{
95 try{
96 if(dos != null){
97 dos.close();
98 }
99 if(br != null){
100 br.close();
101 }
102 if(client != null){
103 client = null;
104 }
105 }catch(Exception e){
106 e.printStackTrace();
107 }
108 }
109 }
110}