標籤:style blog java color 資料 cti
1 package myclient; 2 3 import java.net.*; 4 import java.util.Date; 5 import java.text.SimpleDateFormat; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.io.*; 9 import java.awt.*;10 11 import javax.swing.*;12 public class Myclient1 extends JFrame implements ActionListener{13 JTextArea jta = null;14 JPanel jpl = null;15 JTextField jtf = null;//輸入文本的地方16 JScrollPane jsp = null;17 JButton jbtn = null;18 //向sock管道流寫資訊19 PrintWriter pw = null;20 //顯示發送時間21 //SimpleDateFormat sendtime = new SimpleDateFormat("HH:mm:ss");22 //time = sendtime.format(new Date());// new Date()為擷取當前系統時間23 24 public static void main(String[] args) {25 // TODO Auto-generated method stub26 Myclient1 mclnt = new Myclient1();27 }28 29 public Myclient1(){30 jta = new JTextArea();31 jsp = new JScrollPane(jta);32 jtf = new JTextField(10);//輸入框的長度33 jbtn = new JButton("發送");34 jbtn.addActionListener(this);35 jpl = new JPanel();36 37 jpl.add(jtf);38 jpl.add(jbtn);39 this.add(jsp,"Center");40 this.add(jpl,"South");41 this.setTitle("qq用戶端");42 this.setSize(400,300);43 this.setVisible(true);44 45 46 47 try {48 49 Socket sock = new Socket("127.0.0.1",9988);50 // if connect success51 InputStreamReader isr = new InputStreamReader(sock.getInputStream());52 BufferedReader br = new BufferedReader(isr);53 54 pw = new PrintWriter(sock.getOutputStream(),true);55 56 //顯示訊息57 while(true){58 //不停讀取伺服器發來的資訊59 String info =br.readLine();60 jta.append("伺服器說 :"+info+"\r\n");61 }62 63 } catch (Exception e) {64 // TODO: handle exception65 e.printStackTrace();66 }67 }68 69 @Override70 public void actionPerformed(ActionEvent e) {71 // TODO Auto-generated method stub72 if(e.getSource() == jbtn){73 //將伺服器在jtf中寫的文本發送給用戶端74 String info = jtf.getText();75 jta.append("用戶端說 :"+info+"\r\n");76 pw.println(info);77 //清空輸入框78 jtf.setText(" ");79 80 }81 }82 83 }
下面是伺服器端代碼
/** * This is first server port 9999 * */package com.myserver;import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class Myserver1 extends JFrame implements ActionListener{ JTextArea jta = null; JPanel jpl = null; JTextField jtf = null;//輸入文本的地方 JScrollPane jsp = null; JButton jbtn = null; PrintWriter pw = null; public static void main(String[] args){ Myserver1 msrv = new Myserver1(); } //constructor public Myserver1(){ jta = new JTextArea(); jsp = new JScrollPane(jta); jtf = new JTextField(10);//輸入框的長度 jbtn = new JButton("發送"); jbtn.addActionListener(this); jpl = new JPanel(); jpl.add(jtf); jpl.add(jbtn); this.add(jsp,"Center"); this.add(jpl,"South"); this.setTitle("qq伺服器"); this.setSize(400,300); this.setVisible(true); try{ ServerSocket ss = new ServerSocket(9988); System.out.println("我是伺服器,我在監聽連接埠9988"); Socket sock = ss.accept();//返回一個socket串連 //從sock中讀取資料 InputStreamReader isr = new InputStreamReader(sock.getInputStream()); BufferedReader br = new BufferedReader(isr); pw = new PrintWriter(sock.getOutputStream(),true); while(true){ String info =br.readLine(); jta.append("用戶端說 :"+info+"\r\n"); } }catch(Exception e){ e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(arg0.getSource() == jbtn){ //將伺服器在jtf中寫的文本發送給用戶端 String info = jtf.getText(); jta.append("伺服器說 :"+info+"\r\n"); pw.println(info); //清空輸入框 jtf.setText(" "); } }}