1、服务器程序代码如下:import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class aaa extends Frame implements ActionListener { Label label = new Label("交谈内容"); Panel panel = new Panel(); TextField tf = new TextField(10); TextArea ta = new TextArea(); ServerSocket server; Socket client; InputStream in; OutputStream out; public aaa() { super("服务器"); setSize(80, 80); panel.add(label); panel.add(tf); tf.addActionListener(this); add("West", panel); add("Center", ta); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); show () ; try { server = new ServerSocket(4000); client = server.accept(); ta.append("客户机端:" + client.getInetAddress().getHostName() + "\n\n"); in =client.getInputStream(); out= client.getOutputStream(); } catch (IOException ioe) { } while (true) { try { byte[] buf = new byte[256]; in.read(buf); String str = new String(buf); ta.append("服务器端:" + str + "\n\n"); } catch (IOException e) { } } } public void actionPerformed(ActionEvent e) { try { String str = tf.getText(); byte[] buf = str.getBytes(); tf.setText(null); out.write(buf); ta.append(":" + str + "\n"); } catch (IOException ioe) { } } public static void main(String[] args) { new aaa(); }}运行结果:2、客户端程序代码如下:import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class client extends Frame implements ActionListener {Label label = new Label("交谈内容"); Panel panel = new Panel(); TextField tf = new TextField(10); TextArea ta = new TextArea(); Socket client; InputStream in; OutputStream out; public client() { super("客户端"); setSize(250, 250); panel.add(label); panel.add(tf); tf.addActionListener(this); add("West", panel); add("Center", ta); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); show(); try { client = new Socket(InetAddress.getLocalHost(), 4000); ta.append("您服务器是:" + client.getInetAddress().getHostName() + "\n\n"); in = client.getInputStream(); out = client.getOutputStream(); } catch (IOException ioe) { } while (true) { try { byte[] buf = new byte[256]; in.read(buf); String str = new String(buf); ta.append("服务器端:" + str + "\n"); } catch (IOException e) { } } } public void actionPerformed(ActionEvent e) { try { String str = tf.getText(); byte[] buf = str.getBytes(); tf.setText(null); out.write(buf); ta.append("客户端:" + str + "\n"); } catch (IOException iOE) { } } public static void main(String args[]) { new client(); }}运行结果:最后输入信息,如:你好吗?