日期:2014-05-20 浏览次数:20769 次
import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.*; public class ChatClient extends Frame { TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { setLocation(400, 300); this.setSize(300, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); tfTxt.addActionListener(new TFListener()); setVisible(true); connect(); } public void connect(){ try { Socket s = new Socket("localhost",1069); System.out.print("connected!"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private class TFListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String s = tfTxt.getText().trim(); taContent.setText(s); tfTxt.setText(""); } } }
import java.io.*; import java.net.*; public class ChatServer { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(1069); while(true){ Socket s = ss.accept(); System.out.println("a client connected!"); } } catch (IOException e) { e.printStackTrace(); } } }