布置了作业给10的,然后自己写了一下,socket的

Server.java

package cn.com.alfred; import java.awt.BorderLayout; public class Server extends JFrame { private JButton btn_connect; private JButton btn_disconnect; private JLabel label; private ServerSocket serverSocket; private Socket socket; private String s; private DataOutputStream out = null; public Server() { super(); this.setTitle("服务器端"); this.setLayout(new BorderLayout()); this.setLocation(300, 200); JPanel panel = new JPanel(); btn_connect = new JButton("开启连接"); btn_connect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new Thread(new Runnable() { @Override public void run() { try { serverSocket = new ServerSocket(4444); label.setText("等待连接"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("成功断开"); label.setText("断开连接"); try { serverSocket.close(); socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } try { socket = serverSocket.accept(); System.out.println("server成功连接"); label.setText("连接成功"); out = new DataOutputStream(socket.getOutputStream()); while (true) { s = new Date().toString(); if (s != null && socket != null) { out.writeUTF(s); System.out.println(s); } Thread.sleep(2000); } } catch (IOException e) { e.printStackTrace(); label.setText("断开连接"); try { serverSocket.close(); socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); label.setText("断开连接"); try { serverSocket.close(); socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }).start(); } }); btn_disconnect = new JButton("取消连接"); btn_disconnect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("断开连接"); try { if (serverSocket != null) serverSocket.close(); if (socket != null) socket.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); label.setText("断开连接"); } } }); panel.add(btn_connect); panel.add(btn_disconnect); this.add(panel, BorderLayout.NORTH); label = new JLabel("断开连接", SwingConstants.CENTER); label.setHorizontalAlignment(SwingConstants.CENTER); this.add(label, BorderLayout.SOUTH); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new Server(); } }  

 

Client.java

package cn.com.alfred; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class Client extends JFrame { private JButton btn_connect; private JButton btn_disconnect; private JLabel label; private Socket socket; private String s; private DataInputStream in = null; public Client() { super(); this.setTitle("客户端"); this.setLayout(new BorderLayout()); this.setLocation(600, 200); JPanel panel1 = new JPanel(); btn_connect = new JButton("取得连接"); btn_connect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new Thread(new Runnable() { @Override public void run() { try { socket = new Socket("localhost", 4444); label.setText("连接成功"); System.out.println("client成功连接"); in = new DataInputStream(socket.getInputStream()); while (true) { if(socket == null) return; s = in.readUTF(); label.setText(s); System.out.println(s); Thread.sleep(2000); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); label.setText("连接断开"); } catch (InterruptedException e) { e.printStackTrace(); label.setText("连接断开"); } } }).start(); } }); btn_disconnect = new JButton("取消连接"); btn_disconnect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } }); panel1.add(btn_connect); panel1.add(btn_disconnect); this.add(panel1, BorderLayout.NORTH); label = new JLabel("连接断开", SwingConstants.CENTER); label.setHorizontalAlignment(SwingConstants.CENTER); this.add(label, BorderLayout.SOUTH); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new Client(); } }  

你可能感兴趣的:(布置了作业给10的,然后自己写了一下,socket的)