CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

在线等(急问) : 如何在我的程序中实现 服务器端向客户端返回 数据

楼主christain(张二木)2005-06-01 19:20:32 在 Java / J2SE / 基础类 提问

最近搞一个       java     聊天室程序   遇到麻烦:  
              如何在我现有的   ChatServerRoom.java   中实现数据由服务器端传送到客户端(***比如说   “将在线人数返回到客户端界面上ChatClientRoom.java中的(panel3)上***)  
              由于初学   java     希望大家给个帮助哈!!谢谢  
            下面是我的程序      
                    登陆界面为:christain2.java  
                    客户端界面为:ChatClientRoom.java       (在下页)  
                    服务器端界面为:ChatServerRoom.java   (在另外一个页面)  
   
   
  登陆界面为:christain2.java  
                     
    import   java.awt.*;  
  import   java.awt.event.*;  
  import   java.io.*;  
  import   java.net.*;  
   
   
  import   java.awt.*;  
  import   java.awt.event.*;  
  public   class   Christain2       implements   ActionListener              
  {         Frame   f;  
            Label       t1,t2;  
            TextField   tf1;  
            Label     t3;  
            TextField     tf2;    
            Button   m;  
              public       void   display()          
          {        
                   
                  Frame   f;  
                  f   =   new   Frame("chatRoom");               //创建框架并设置标题  
                  f.setSize(600,350);                                             //框架大小  
                f.setBackground(Color.GREEN);  
                f.setLayout(new   FlowLayout(FlowLayout.LEFT));  
                  t1=new   Label("用户名");  
                  t3=new   Label("嘟嘟   的   乐   园");  
                  tf1=new   TextField("user1",25);  
                  tf1.setEditable(true);  
                  t2=new   Label("密码:");  
                  tf2=new   TextField(15);  
                  tf2.setEditable(true);  
                  f.add(t3);  
                  f.add(t1);  
                  f.add(tf1);  
                  f.add(t2);  
                  f.add(tf2);  
                  m   =new   Button("登录");  
                  f.add(m);  
                  m.addActionListener(this);  
                  tf2.addActionListener(this);  
                  f.addWindowListener(new   WinClose());    
                  f.setVisible(true);                                             //显示框架    
                   
          }      
           
          public   void   actionPerformed(ActionEvent   e)  
            {   if(e.getSource()!=null   &&   e.getSource()==m   )  
                      (new   ChatRoomClient()).show();    
                            f.setVisible(false);  
                           
                  }  
             
          class   WinClose   extends   WindowAdapter{  
           
            public   void   windowClosing(WindowEvent   e)  
          {  
                  System.exit(0);    
          }  
          }  
              public   static   void   main   (String   arg[])  
          {  
                        (new   Christain2()).display();  
                        }  
           
    }  
   
       
   
   
   
     
   
   
   
   
   
   
  问题点数:0、回复次数:2Top

1 楼mituzhishi(慎独)回复于 2005-06-02 12:54:34 得分 0

服务器端:  
   
  /**  
    *   In   order   to   send   a   network   message,   use   write();   In   order   to   broadcast   a  
    *   network   message,   use   broadcast();   You   also   have   to   implement   the   function  
    *   handleServerMessage().  
    */  
   
  import   java.nio.channels.SocketChannel;  
  import   java.nio.channels.ServerSocketChannel;  
  import   java.nio.channels.SelectionKey;  
  import   java.nio.channels.Selector;  
  import   java.nio.ByteBuffer;  
  import   java.net.InetSocketAddress;  
  import   java.util.Iterator;  
  import   java.util.LinkedList;  
  import   java.io.IOException;  
   
  public   class   AsyncServer   implements   Runnable   {  
          public   AsyncServer()   {  
                  new   Thread(this).start();  
          }  
   
          public   void   run()   {  
                  try   {  
                          ServerSocketChannel   ssc   =   ServerSocketChannel.open();  
                          ssc.configureBlocking(false);  
                          Selector   s   =   Selector.open();  
                          ssc.socket().bind(new   InetSocketAddress(port));  
                          ssc.register(s,   SelectionKey.OP_ACCEPT);  
                          System.out.println("echo   server   has   been   set   up   ......");  
                          while   (true)   {  
                                  int   n   =   s.select();  
                                  if   (n   ==   0)   {  
                                          continue;  
                                  }  
                                  Iterator   it   =   s.selectedKeys().iterator();  
                                  while   (it.hasNext())   {  
                                          SelectionKey   key   =   (SelectionKey)   it.next();  
                                          if   (key.isAcceptable())   {  
                                                  ServerSocketChannel   server   =   (ServerSocketChannel)   key  
                                                                  .channel();  
                                                  SocketChannel   sc   =   server.accept();  
                                                  clientList.add(sc);  
                                                  sc.configureBlocking(false);  
                                                  sc.register(s,   SelectionKey.OP_READ);  
                                          }  
                                          if   (key.isReadable())   {  
                                                  handleClientMessage(key);  
                                          }  
                                          it.remove();  
                                  }  
                          }  
                  }   catch   (Exception   e)   {  
                          e.printStackTrace();  
                  }  
          }  
   
          public   String   read(SocketChannel   sc)   {  
                  String   returnMsg   =   "";  
                  int   count;  
                  r_buff.clear();  
                  try   {  
                          count   =   sc.read(r_buff);  
                          r_buff.flip();  
                          byte[]   temp   =   new   byte[r_buff.limit()];  
                          r_buff.get(temp);  
                          r_buff.clear();  
                          returnMsg   =   new   String(temp);  
                  }   catch   (IOException   ioe)   {  
                          System.out.println("read   exception.");  
                          disconnect(sc);  
                  }  
                  return   returnMsg;  
          }  
   
          public   void   write(SocketChannel   sc,   String   message)   {  
                  w_buff.clear();  
                  w_buff.put(message.getBytes());  
                  w_buff.flip();  
                  try   {  
                          while   (w_buff.hasRemaining())  
                                  sc.write(w_buff);  
                  }   catch   (IOException   ioe)   {  
                          System.out.println("write   exception.");  
                          disconnect(sc);  
                  }  
   
          }  
   
          private   void   broadcast(SocketChannel   from,   String   message)   {  
                  Iterator   i   =   clientList.iterator();  
                  while   (i.hasNext())   {  
                          SocketChannel   channel   =   (SocketChannel)   i.next();  
                          write(channel,   message);  
                  }  
          }  
   
          public   void   handleClientMessage(SelectionKey   key)   {  
                  SocketChannel   sc   =   (SocketChannel)   key.channel();  
                  String   message   =   read(sc);   //message   is   here   !  
                  System.out.println(message);                  
                  broadcast(sc,   "I   am   broadcasting   !");  
          }  
   
          public   void   disconnect(SocketChannel   sc)   {  
                  clientList.remove(sc);  
                  try   {  
                          sc.close();  
                  }   catch   (IOException   ioe)   {  
                          System.out.println("Can   not   close   SocketChannel   .");  
                  }  
   
          }  
   
          public   static   void   main(String   args[])   {  
                  new   AsyncServer();  
          }  
   
          private   LinkedList   clientList   =   new   LinkedList();  
   
          private   ByteBuffer   r_buff   =   ByteBuffer.allocate(1024);  
   
          private   ByteBuffer   w_buff   =   ByteBuffer.allocate(1024);  
   
          private   static   int   port   =   8848;  
  }Top

2 楼mituzhishi(慎独)回复于 2005-06-02 12:54:59 得分 0

客户端:  
   
  /**  
    *   You   have   to   create   classes   to   extend   this   class   and   implement   the   function  
    *   handleServerMessage().   In   order   to   send   a   network   message,   use   write();  
    */  
   
  import   java.io.IOException;  
  import   java.net.InetSocketAddress;  
  import   java.nio.ByteBuffer;  
  import   java.nio.channels.SocketChannel;  
   
  public   class   networkHandler   {  
          public   class   IncomingMessagehandler   extends   Thread   {  
                  public   void   run()   {  
                          while   (true)   {  
                                  try   {  
                                          handleServerMessage(read());  
                                  }   catch   (Exception   e)   {  
                                          System.exit(0);  
                                  }  
                          }  
                  }  
          }  
   
          public   networkHandler()   {  
                  Connect();  
          }  
   
          public   void   Connect()   {  
                  try   {  
                          InetSocketAddress   addr   =   new   InetSocketAddress(host,   port);//生成一个socketchannel  
                          sc   =   SocketChannel.open();//连接到server  
                          sc.connect(addr);  
                          System.out.println("connection   has   been   established!...");  
                  }   catch   (IOException   ioe)   {  
                          ioe.printStackTrace();  
                  }  
                  new   IncomingMessagehandler().start();  
          }  
   
          public   String   read()   throws   IOException   {  
                  int   count;  
                  r_buff.clear();  
                  count   =   sc.read(r_buff);  
                  r_buff.flip();  
                  byte[]   temp   =   new   byte[r_buff.limit()];  
                  r_buff.get(temp);  
                  return   new   String(temp);  
          }  
   
          public   void   write(String   string)   throws   IOException   {  
                  w_buff.clear();  
                  w_buff.put(string.getBytes());  
                  w_buff.flip();  
   
                  while   (w_buff.hasRemaining())  
                          sc.write(w_buff);  
          }  
   
          public   void   handleServerMessage(String   message)   {  
                  System.out.println(message);  
          }  
   
          public   static   void   main(String   args[])   {  
                  try   {  
                          new   networkHandler().write("I   love   you   !");  
                  }   catch   (IOException   e)   {  
                          e.printStackTrace();  
                  }  
          }  
   
          private   SocketChannel   sc;  
   
          private   static   String   host   =   "127.0.0.1";  
   
          private   static   int   port   =   8848;  
   
          private   final   int   MAX_LENGTH   =   1024;  
   
          private   ByteBuffer   r_buff   =   ByteBuffer.allocate(MAX_LENGTH);  
   
          private   ByteBuffer   w_buff   =   ByteBuffer.allocate(MAX_LENGTH);  
  }Top

相关问题

  • 请教大虾们一个问题,当客户端向服务器数据库(sqlserver2000)中一个表中插入一个条数据时,要求服务器端应用程序实时显示更新数据,如何操作
  • 请教大虾们一个问题,当客户端向服务器数据库(sqlserver2000)中一个表中插入一个条数据时,要求服务器端应用程序实时显示更新数据,如何操作
  • CSocket客户端向服务器端发数据,一段时间后客户端程序发送就阻塞在m_pArchiveOut->Flush()处。
  • MIDAS怎么从客户端向应用服务器传命令和数据
  • 客户端向服务器传送xml格式数据,如何在服务器读出该数据并且简单回应?
  • 服务器数据库程序的问题?
  • 程序连接数据库服务器的权限问题
  • javascript 在服务器端向数据库添加记录 给段代码 谢谢了
  • ---> 100分 在线等!!如何模拟合法客户端向服务器发数据?
  • 大家邦我看看,有关客户端向服务器端发送数据流的问题!

关键词

  • .net
  • 服务器
  • 界面
  • 客户
  • christain
  • 客户端
  • 程序
  • awt
  • tf
  • frame

得分解答快速导航

  • 帖主:christain

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo