本文實例講述了Java編程實現基于TCP協議的Socket聊天室。分享給大家供大家參考,具體如下:
這里使用Socket套接字進行編程,完成的是基于TCP可靠服務實現服務器與客戶端的雙通信。
Server服務器端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.han; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import java.net.*; import javax.swing.JDialog; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 本程序實現了一個TCP程序的服務器編程部分。 * 使用Socket套接字進行編程,完成的是基于TCP可靠服務實現與客戶端的雙通信。 * 客戶端的編程見本包中的類Client * @author HAN * */ @SuppressWarnings ( "serial" ) public class Server extends JDialog{ private BufferedReader reader; private PrintWriter writer; private ServerSocket server; private Socket socket; private JTextArea ta= new JTextArea(); private JScrollPane sp= new JScrollPane(ta); private JTextField tf= new JTextField(); Container cc; public Server(String title) { setTitle(title); addWindowListener( new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //撤銷dialog一切相關資源 System.exit( 0 ); //正常退出程序 } }); cc=getContentPane(); setLayout( null ); ta.setLineWrap( true ); ta.setEditable( false ); sp.setBounds( 0 , 0 , 300 , 342 ); tf.setBounds( 0 , 342 , 300 , 25 ); cc.add(sp); cc.add(tf); tf.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ try { writer= new PrintWriter(socket.getOutputStream(), true ); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } writer.println(tf.getText()); ta.append( "User1:" +tf.getText()+ '\n' ); tf.setText( "" ); } }); } void getserver(){ try { server= new ServerSocket( 8998 ); ta.append( "服務器套接字已經創建成功\n" ); while ( true ){ ta.append( "等待客戶機的連接\n" ); socket=server.accept(); ta.append( "客戶機已連接\n" ); reader= new BufferedReader( new InputStreamReader(socket.getInputStream())); getClientMessage(); } } catch (Exception e){ e.printStackTrace(); } } private void getClientMessage(){ try { while ( true ){ String news=reader.readLine(); if (news!= null ){ ta.append( "User2:" +news+ "\n" ); } else { ta.append( "User2(客戶端) 已斷開鏈接\n" ); break ; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (reader!= null ){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (socket!= null ){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Server user1= new Server( "User1" ); user1.setBounds( 0 , 0 , 300 , 400 ); user1.setResizable( false ); user1.setVisible( true ); user1.getserver(); } } |
Client客戶端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.han; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; /** * 本程序實現了一個TCP程序的客戶端編程部分。 * 使用Socket套接字進行編程,完成的是基于TCP可靠服務實現與服務器的雙通信。 * 服務器的編程見本包中的類Server * 可以在不同的平臺與不同的機器上運行,只是要把代碼中寫的IP地址修改為運行服務器程序Server的機器的IP地址。 * @author HAN * */ @SuppressWarnings ( "serial" ) public class Client extends JDialog{ private BufferedReader reader; private PrintWriter writer; private Socket socket; private JTextArea ta= new JTextArea(); private JScrollPane sp= new JScrollPane(ta); private JTextField tf= new JTextField(); Container cc; public Client(String title) { setTitle(title); addWindowListener( new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //撤銷dialog一切相關資源 System.exit( 0 ); //正常退出程序 } }); cc=getContentPane(); setLayout( null ); ta.setLineWrap( true ); ta.setEditable( false ); sp.setBounds( 0 , 0 , 300 , 342 ); tf.setBounds( 0 , 342 , 300 , 25 ); cc.add(sp); cc.add(tf); tf.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ writer.println(tf.getText()); ta.append( "User2:" +tf.getText()+ '\n' ); tf.setText( "" ); } }); } private void connect(){ ta.append( "嘗試連接\n" ); try { socket= new Socket( "192.168.1.3" , 8998 ); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer= new PrintWriter(socket.getOutputStream(), true ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ta.append( "完成連接\n" ); } private void getClientMessage(){ try { reader= new BufferedReader( new InputStreamReader(socket.getInputStream())); while ( true ){ String news=reader.readLine(); if (news!= null ){ ta.append( "User1:" +news+ "\n" ); } else { ta.append( "User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n" ); break ; } } } catch (IOException e) { // TODO Auto-generated catch block ta.append( "User1(服務器) 已斷開鏈接,等待服務器重連之時,重啟User2(客戶端)進行通信\n" ); e.printStackTrace(); } try { if (reader!= null ){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (socket!= null ){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Client user2= new Client( "User2" ); user2.setBounds( 0 , 0 , 300 , 400 ); user2.setVisible( true ); user2.setResizable( false ); user2.connect(); user2.getClientMessage(); } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/gaowen_han/article/details/7163709