您的位置:首页 > 其它

第二周周三

2015-08-05 20:38 232 查看

聊天工具(初级版)

网络连接 多线程 综合应用

程序分为四部分,包括服务器、客户端

public class MyServer extends JFrame {

private JPanel contentPane;
private Socket socket;
private boolean isRunning = true;
private JTextArea textArea;
public boolean isRunning(){
return isRunning;
}

public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyServer frame = new MyServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MyServer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton button = new JButton("\u542F\u52A8\u670D\u52A1");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动");
socket = server.accept();
Thread t = new Thread(new MyServerRead(MyServer.this));
t.start();
System.out.println("有客户端链接");
}catch (IOException e1){
e1.printStackTrace();
}
}
});
button.setBounds(22, 10, 475, 87);
contentPane.add(button);

textArea = new JTextArea();
textArea.setBounds(100, 476, 518, 138);
contentPane.add(textArea);

JButton button_1 = new JButton("\u53D1\u7ED9\u5BA2\u6237\u7AEF");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
write();
}
});
button_1.setBounds(671, 531, 93, 23);
contentPane.add(button_1);
}

public void read(){
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
System.out.println("服务器收到的信息"+line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String words = textArea.getText();
System.out.println("服务器发送信息");
bw.write(words+"\n");
textArea.setText("");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


public class MyServerRead implements Runnable{
private MyServer server;
public MyServerRead(MyServer server){
this.server = server;
}

@Override
public void run() {
while (server.isRunning()){
server.read();
}
}
}


public class MyClient extends JFrame {

private JPanel contentPane;
private Socket socket;
private JTextArea textArea;
private JList list;
private DefaultListModel<String> modle;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyClient frame = new MyClient();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MyClient() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 850, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textArea = new JTextArea();
textArea.setBounds(49, 436, 500, 101);
contentPane.add(textArea);

JButton btnNewButton = new JButton("发送");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
write();
}
});
btnNewButton.setBounds(589, 436, 129, 101);
contentPane.add(btnNewButton);

list = new JList();
list.setBounds(61,10,484,416);
modle = new DefaultListModel<>();
list.setModel(modle);
contentPane.add(list);

JButton btnNewButton_1 = new JButton("连接服务器");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
System.out.println("连接服务器");
socket=new Socket("192.168.0.41", 8080);
System.out.println("连接服务器成功");
Thread t=new Thread(new MyClientRead(MyClient.this));
t.start();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_1.setBounds(615, 68, 103, 86);
contentPane.add(btnNewButton_1);
}
public void read() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
modle.addElement("服务器说的"+line);
System.out.println(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os=socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String words=   textArea.getText();
bw.write(words+"\n");
modle.addElement(words);
textArea.setText("");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public class MyClientRead implements Runnable {
private MyClient client;
public MyClientRead(MyClient client){
this.client = client;
}

@Override
public void run() {
while(true){
client.read();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: