标签: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 stub 26 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 success 51 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 exception 65 e.printStackTrace(); 66 } 67 } 68 69 @Override 70 public void actionPerformed(ActionEvent e) { 71 // TODO Auto-generated method stub 72 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(" "); } } }
标签:style blog java color 数据 cti
原文地址:http://www.cnblogs.com/angus67/p/3830317.html