Desgin of Instant Messaging Client

  1 public class Client extends JFrame implements Runnable{
  2
  3     private JPanel jPanel= new JPanel();
  4     private JLabel jLabel1=new JLabel("姓名: ");
  5     private JLabel jLabel2=new JLabel("端口: ");
  6     private JTextField jTextField1=new JTextField();
  7     private JTextField jTextField2=new JTextField();
  8     private JTextArea jTextArea=new JTextArea();
  9     private JTextField jTextField3=new JTextField();
 10     private JButton jButton=new JButton("发送(S)");
 11
 12     private JScrollPane jScrollPane=new JScrollPane();
 13     private BufferedReader bufferedReader;
 14     private PrintWriter printWriter;
 15     private Socket socket;
 16
 17     private static String title="劲爆聊天室";
 18
 19     public Client(String port,String name){
 20         super(title+":"+name);
 21         this.setSize(377,387);
 22         this.add(jPanel);
 23         jPanel.setLayout(null);
 24         jTextArea.setEditable(false);
 25         //
 26         jPanel.add(jLabel1);
 27         jLabel1.setBounds(10, 10, 60, 20);
 28         jTextField1.setText(name);
 29         jTextField1.setEditable(false);
 30         jPanel.add(jTextField1);
 31         jTextField1.setBounds(60, 10, 60, 21);
 32
 33         jPanel.add(jLabel2);
 34         jLabel2.setBounds(125, 10, 60, 20);
 35         jTextField2.setText(port);
 36         jTextField2.setEditable(false);
 37         jPanel.add(jTextField2);
 38         jTextField2.setBounds(190, 10, 60, 21);;
 39
 40         jPanel.add(jTextField3);
 41         jTextField3.setBounds(10, 270, 320, 21);
 42
 43         jTextArea.setColumns(20);
 44         jTextArea.setRows(5);
 45
 46         jTextArea.setEditable(false);
 47         jScrollPane.setViewportView(jTextArea);
 48         jPanel.add(jScrollPane);
 49         jScrollPane.setBounds(10, 42, 320, 220);
 50
 51         jPanel.add(jButton);
 52         jButton.setBounds(250, 300, 80, 22);
 53         jButton.addActionListener(new ActionListener() {
 54
 55             @Override
 56             public void actionPerformed(ActionEvent e) {
 57                 // TODO Auto-generated method stub
 58                 if(e.getSource()==jButton){
 59                     printWriter.println(jTextField1.getText()+": "+jTextField3.getText());
 60                     jTextField3.setText("");
 61                 }
 62             }
 63         });
 64         //Jtextfield不好控制啊。。。。
 65 /*        jTextField3.addActionListener(new ActionListener() {
 66
 67             @Override
 68             public void actionPerformed(ActionEvent e) {
 69                 // TODO Auto-generated method stub
 70                 printWriter.println(jTextField1.getText()+":"+jTextField3.getText());
 71                 jTextField3.setText("");
 72             }
 73         });*/
 74         getSocket();
 75     }
 76     @Override
 77     public void run(){
 78         while(true)
 79         {
 80             try {
 81                 jTextArea.append(bufferedReader.readLine()+"\n");
 82
 83             } catch (Exception e) {
 84                 // TODO: handle exception
 85                 e.printStackTrace();
 86             }
 87         }
 88     }
 89
 90     public void getSocket(){
 91         jTextArea.append("正在与劲爆聊天室进行连接..."+"\n");
 92         try {
 93             socket=new Socket("127.0.0.1",PortNumber.PORT.getNumber());
 94             jTextArea.append("劲爆聊天开始..."+"\n");
 95             bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
 96             printWriter=new PrintWriter(socket.getOutputStream(),true);
 97             new Thread(this).start();
 98         } catch (Exception e) {
 99             // TODO: handle exception
100             e.printStackTrace();
101         }
102     }
103
104 }

考虑到多客户端,此处用到了多线程。Socket确实有意思....

非常感叹T公司如果把一个IM软件,做到亿级用户量,如何解决并发,如何解决Server的及时回应,如何完成数据存储...

想不出来了...

时间: 2024-08-29 07:53:29

Desgin of Instant Messaging Client的相关文章

Instant Messaging for OpenERP v7

This module is based on the socket.io real time implementation. It inherit Of the modules web_longpolling and web_socketio on bitbucket. How Install the module web_im Make your Nginx/Apache conf example conf for nginx: worker_processes 1; events { wo

ubuntu server激活即时通讯IM服务 Instant Messaging is not activated on this server

转自:http://shine-it.net/index.php/topic,16469.msg28364.html ubuntu server 下 odoo激活及时通讯功能im OpenERP IM Odoo IM activated "Instant Messaging is not activated on this server." 参考: https://accounts.openerp.com/forum/Help-1/question/How-to-activate-ch

A tutorial that will show you how to build an instant messaging app with Sinch.

http://stackoverflow.com/questions/26247986/unsatisfiedlinkerror-couldnt-load-sinch-android-rtc-from-loader-dalvik-system https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/ https://github.com/sinch/android-messaging-tut

容易忘记的linux命令之rpm

1 rpm软件包管理方式 也许现实中yum简单方便,但是有时候我们也还会用rpm包,yum用的也是rpm包,从安装,查询,卸载,升级多方面记住,当然还是多多实践才能记住. NAME rpm - RPM Package Manager 2 常用的命令,man文档好长 ①安装 rpm -ivh PACKAGENAME -i 安装install的意思 -h, --hash  Print 50 hash marks as the package archive is unpacked.  Use wit

The Top 50 Proprietary Programs that Drive You Crazy — and Their Open Source Alternatives

The Top 50 Proprietary Programs that Drive You Crazy - and Their Open Source Alternatives 01 / 22 / 2013 By Josh Weikel No Comments Posted in Software Tags Update January 22, 2013: This article was originally posted February 7, 2008. Since then, it h

top-100-of-the-best-useful-opensource-applications/

top-100-of-the-best-useful-opensource-applications/ http://www.ubuntulinuxhelp.com/top-100-of-the-best-useful-opensource-applications/ The following is a list of about 100 of the best OpenSource Applications, that actually help make Linux more usable

Qt简介 及与MFC、GDK+的比较

Qt C++图形用户界面应用程序开发框架. Qt的由来和发展 1.QT由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies,然后更名为Troll Tech,然后在改为Trolltech,中文名是"奇趣科技".2008年6月17日被NOKIA公司收购,以增强该公司在跨平台软件研发方面的实力,更名Qt Software.该工具包名为Qt是因为字母 Q在Ha

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

Go语言(golang)开源项目大全

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu