Java swing 一个简单的Socket通讯程序(服务器端和客户端)

it2023-01-14  49

**

Java swing 一个简单的Socket通讯程序(服务器端和客户端)

**

代码: //*** 服务器端: package com.yufu.svr;

import java.awt.BorderLayout; import java.awt.EventQueue;

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.net.; import java.io.; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;

public class socketSvr extends JFrame {

/** * */ private static final long serialVersionUID = 4450065725328989575L; private JPanel contentPane; private JTextField textField; private JTextArea textArea; private JButton btn_svrSender; private PrintWriter pw; private ServerSocket svr; private Socket socket; //private InputStreamReader inStrread; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { socketSvr frame = new socketSvr(); frame.setVisible(true); frame.setSvr(); } catch (Exception e) { e.printStackTrace(); } } }); } public void setSvr() { new Thread() { @Override public void run() { // TODO Auto-generated method stub super.run(); try { svr = new ServerSocket(11111); textArea.append("服务器启动已成功,等待客户端的连接...\n"); socket=svr.accept(); textArea.append("客户端连接已成功...\n"); pw=new PrintWriter(socket.getOutputStream(),true); InputStreamReader inStrread=new InputStreamReader(socket.getInputStream()); BufferedReader bfRead=new BufferedReader(inStrread); while(true) { String str=bfRead.readLine(); textArea.append("收到客户端发来的信息:"+str+"\n"); if(str.equals("q!")||str.equals("Q!")) { break; } } socket.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }.start(); // TODO 自动生成的方法存根 } /** * Create the frame. */ public socketSvr() { super(); setTitle("Socket通讯服务器端"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.NORTH); JLabel lblNewLabel = new JLabel("服务器端发送消息框:"); panel.add(lblNewLabel); textField = new JTextField(); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String strSvr=textField.getText(); pw.println(strSvr); textArea.append("服务器发送的信息为:"+strSvr+"\n"); textField.setText(""); } }); panel.add(textField); textField.setColumns(10); btn_svrSender = new JButton("发送"); btn_svrSender.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_Svr_It(e); } }); panel.add(btn_svrSender); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); textArea = new JTextArea(); scrollPane.setViewportView(textArea); } protected void do_Svr_It(ActionEvent e) { // TODO 自动生成的方法存根 String strSvr=textField.getText(); pw.println(strSvr); textArea.append("服务器发送的信息为:"+strSvr+"\n"); textField.setText(""); }

}

***** 客户端代码: package com.yufu.client;

import java.awt.BorderLayout; import java.awt.EventQueue; import java.net.; import java.io.;

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;

public class socketClient extends JFrame {

/** * */ private static final long serialVersionUID = 4527626609353696624L; private JPanel contentPane; private JTextField textField; private JTextArea textArea; private JButton btnNewButton; private PrintWriter pw; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { socketClient frame = new socketClient(); frame.setVisible(true); frame.setClient(); } catch (Exception e) { e.printStackTrace(); } } }); } public void setClient() { new Thread() { @Override public void run() { // TODO Auto-generated method stub super.run(); try { Socket socket=new Socket("127.0.0.1",11111); textArea.append("与服务器的连接已成功。\n"); pw=new PrintWriter(socket.getOutputStream(),true); InputStreamReader inStread=new InputStreamReader(socket.getInputStream()); BufferedReader bfReade=new BufferedReader(inStread); while(true) { String svrInfo=bfReade.readLine(); textArea.append("收到服务器端发来的信息:"+svrInfo+"\n"); if(svrInfo.equals("q!")||svrInfo.equals("Q!")) { break; } } socket.close(); } catch (UnknownHostException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }.start(); // TODO 自动生成的方法存根 } /** * Create the frame. */ public socketClient() { setTitle("Socket通讯客户端"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.SOUTH); JLabel lblNewLabel = new JLabel("客户端发送消息框:"); panel.add(lblNewLabel); textField = new JTextField(); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str=textField.getText(); pw.println(str); textArea.append("客户端发送的信息为:"+str+"\n"); textField.setText(""); } }); panel.add(textField); textField.setColumns(10); btnNewButton = new JButton("发送"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_btnSender_It(e); } }); panel.add(btnNewButton); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); textArea = new JTextArea(); scrollPane.setViewportView(textArea); } protected void do_btnSender_It(ActionEvent e) { String str=textField.getText(); pw.println(str); textArea.append("客户端发送的信息为:"+str+"\n"); textField.setText(""); } // TODO 自动生成的方法存根 }

效果图: 注意事项: 由于swing的运行机制问题(Swing是单线程的设计,因此while(true)一下的时候,后果你懂的…),复杂的耗时的内容另起一个线程处理,就不会造成界面启动卡死的问题。 详细的卡死问题,请自行查询里的大神的精心答复。

最新回复(0)