本文实例为大家分享了一款Socket简易聊天工具,希望大家喜欢。
代码运行如图,看起来还不错
服务端
java" id="highlighter_44673">
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
115
116
117
|
package qiu;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Printable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 简单聊天软件的服务器
* */
public class MyServer extends JFrame implements ActionListener{
JTextArea jTextArea = null ; //用来显示纯文本的单行区域
JTextField jTextField= null ; //可以允许用来编辑单行文本
JButton sendButton= null ;
JPanel jPanel= null ;
JScrollPane jScrollPane = null ;
//把信息发给客户端对象
PrintWriter printWriter = null ;
/**
* 服务端的主函数
* */
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyServer();
}
/**
* 服务端的构造函数,用来进行初始化
* */
public MyServer(){
//这里是对GUI的初始化
jTextArea = new JTextArea();
jTextField= new JTextField( 20 );
sendButton= new JButton( "发送" );
sendButton.addActionListener( this );
sendButton.setActionCommand( "send" );
jScrollPane= new JScrollPane(jTextArea);
jPanel = new JPanel();
jPanel.add(jTextField); //添加编辑框
jPanel.add(sendButton); //添加按钮
//将两个面板添加布局
this .add(jScrollPane,BorderLayout.CENTER);
this .add(jPanel,BorderLayout.SOUTH);
this .setSize( 400 , 300 );
this .setTitle( "聊天服务器" );
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置退出按钮
this .setVisible( true );
this .setResizable( true );
//下面是socket服务器的搭建
try {
//服务器监听
ServerSocket ss = new ServerSocket( 9988 );
//等待客户端连接
Socket socket = ss.accept();
//获得客户端发送过来的数据的流
BufferedReader br = new BufferedReader
( new InputStreamReader(socket.getInputStream()));
printWriter = new PrintWriter(socket.getOutputStream(), true );
//读取从客户端发送过来的信息
while ( true ){
String info = br.readLine();
jTextArea.append( "客户端 " +getTime()+ "\r\n" +info+ "\r\n" );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
/**
* 用来获取当前的时间
* @return 当前的时间
*/
public String getTime(){
//可以对每个单独时间域进行修改
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY); //获取小时
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return hour+ ":" +minute+ ":" +second;
}
/**
* 当button被点击的时候调用
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//当按钮按下的时候调用
if (e.getActionCommand().equals( "send" )){
//把服务器在jTextField写的内容发送给客户端
String info= jTextField.getText();
jTextArea.append( "服务器 " +getTime()+ "\r\n" +info+ "\r\n" );
printWriter.println(info);
//清楚发送框内容
jTextField.setText( "" );
}
}
}
|
客户端
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
|
package qiu;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 简单聊天软件的客户端
* */
public class MyClient extends JFrame implements ActionListener{
JTextArea jTextArea= null ;
JTextField jTextField= null ;
JPanel jPanel= null ;
JScrollPane jScrollPane= null ;
JButton sendButton= null ;
PrintWriter printWriter= null ;
/**
* 客户端的主函数
* */
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyClient();
}
/**
* 客户端构造函数用来初始化
* */
public MyClient(){
//GUI初始化
jTextArea= new JTextArea();
jTextField= new JTextField( 20 );
sendButton= new JButton( "发送" );
sendButton.addActionListener( this );
sendButton.setActionCommand( "send" );
jScrollPane= new JScrollPane(jTextArea);
jPanel= new JPanel();
jPanel.add(jTextField);
jPanel.add(sendButton);
this .add(jScrollPane,BorderLayout.CENTER);
this .add(jPanel,BorderLayout.SOUTH);
this .setSize( 400 , 300 );
this .setTitle( "聊天客户端" );
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this .setVisible( true );
this .setResizable( true );
//socket通信代码
try {
Socket s= new Socket( "127.0.0.1" , 9988 );
BufferedReader br = new BufferedReader
( new InputStreamReader(s.getInputStream()));
printWriter= new PrintWriter(s.getOutputStream(), true );
while ( true ){
//不停的读取服务器发过来的信息
String string=br.readLine();
jTextArea.append( "服务器 " +getTime()+ "\r\n" +string+ "\r\n" );
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用来获取当前的时间
* @return 当前的时间
*/
public String getTime(){
//可以对每个单独时间域进行修改
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY); //获取小时
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return hour+ ":" +minute+ ":" +second;
}
/**
* 当button被点击的时候调用
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (sendButton.getActionCommand().equals( "send" )){
String info= jTextField.getText();
//将客户端发送的信息发送给服务端
jTextArea.append( "客户端 " +getTime()+ "\r\n" +info+ "\r\n" );
printWriter.println(info);
jTextField.setText( "" );
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。