利用socket編寫聊天程式
import java.awt.Frame; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket;
public class ServerProgram 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 ServerProgram() { super("伺服器"); setSize(250, 250); panel.add(label);panel.add(tf); tf.addActionListener(this); add("North", panel);add("Center", ta); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); try { server=new ServerSocket(4000); client=server.accept(); ta.append("客戶機是:"+client.getInetAddress().getHostName()+"\n\n"); in=client.getInputStream(); out=client.getOutputStream(); } catch (IOException e1) { } while(true) { try { byte[] buf=new byte[256]; in.read(buf); String str=new String(buf); ta.append("客戶機說:"+str+"\n"); } catch (IOException e1) { } } } 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 ServerProgram(); } }
import java.awt.Frame; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket;
public class ClientProgram 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 ClientProgram() { super("客戶機"); setSize(250, 250); panel.add(label);panel.add(tf); tf.addActionListener(this); add("North", panel);add("Center", ta); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); try { client=new Socket(InetAddress.getLocalHost(), 4000); ta.append("伺服器是:"+client.getInetAddress().getHostName()+"\n\n"); in=client.getInputStream(); out=client.getOutputStream(); } catch (IOException e1) { } while(true) { try { byte[] buf=new byte[256]; in.read(buf); String str=new String(buf); ta.append("伺服器說:"+str+"\n"); } catch (IOException e1) { } } } 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 ClientProgram(); } }
執行結果: