实例70CS结构聊天室_第1页
实例70CS结构聊天室_第2页
实例70CS结构聊天室_第3页
实例70CS结构聊天室_第4页
实例70CS结构聊天室_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实例70C/S结构聊天室 通过前面的简短学习,下面用 TCP/IP协议里的套接字(Socket)编程接口来实现一个聊天室.socket是一种流式通信机制,是一种基于连接的通信,即,在通信之前通信双方确认身份并建立一条专用的虚拟连接通道,然后他们通过这条通道传送数据信息进行通信,当通信结束时再将原来所建的连接拆除.本例有一个客户端程序MyClient.java和一个服务器端程序Server.java. 服务器端创建对象ServerSocket使他在某端口提供监听服务, 一旦客户端创建Socket对象向监听窗口请求,服务器接收这个请求就会建立socket连接.运行MyClient和Server程序

2、:监听器窗口:客户端窗体,输入”Hello”:服务器端窗口信息:-MyClient.java源代码-import java.awt.*;import java.awt.event.*;import .*;import java.io.*;/client端的程序:public class MyClient extends Frame implements ActionListener,ItemListener MenuBar m_Menu_Bar; Menu menuFile,menuEdit,menuHelp; MenuItem mi_File_Open,mi_File_Clo

3、se,mi_File_Exit,mi_Edit_Copy,mi_Edit_Paste; MenuItem pi_New,pi_Del,pi_Pro,mi_Help_Sub; CheckboxMenuItem mi_Edit_Cut; PopupMenu popM; Socket ClientSocket; PrintStream os; DataInputStream is; String s; Label MyLabel=new Label( 欢迎使用本系统为您提供服务); TextArea textarea; / 发送消息按钮 Button MyButton=new Button(发 送

4、消 息); public MyClient() setTitle(Client Window(客户端窗口); setLayout(new BorderLayout(); / 给窗体添加监听器 this.addWindowListener(new WinAdptClient(this);/ 给发送按钮设置添加事件监听器 MyButton.addActionListener(this); / 界面设计,添加下拉菜单和弹出菜单等 textarea=new TextArea(13,55); popM=new PopupMenu(); pi_New=new MenuItem( 新建 ); pi_New.

5、addActionListener(this); popM.add(pi_New); pi_Del=new MenuItem( 删除 ); pi_Del.addActionListener(this); popM.add(pi_Del); pi_Pro=new MenuItem( 属性 ); pi_Pro.addActionListener(this);popM.add(pi_Pro); m_Menu_Bar=new MenuBar(); menuFile=new Menu(文件); mi_File_Open=new MenuItem(打开); mi_File_Open.setShortcut

6、(new MenuShortcut(f);mi_File_Close=new MenuItem(关闭,new MenuShortcut(s); mi_File_Exit=new MenuItem(退出,new MenuShortcut(x); mi_File_Open.setActionCommand(打开); mi_File_Close.setActionCommand(关闭); mi_File_Exit.setActionCommand(退出); mi_File_Open.addActionListener(this); mi_File_Close.addActionListener(th

7、is); mi_File_Exit.addActionListener(this); menuFile.add(mi_File_Open); menuFile.add(mi_File_Close); menuFile.add(mi_File_Exit); m_Menu_Bar.add(menuFile); menuEdit=new Menu(编辑); mi_Edit_Copy=new MenuItem(复制); mi_Edit_Paste=new MenuItem(粘贴); mi_Edit_Cut=new CheckboxMenuItem(CUT); mi_Edit_Copy.setActio

8、nCommand(复制); mi_Edit_Paste.setActionCommand(粘贴); mi_Edit_Copy.addActionListener(this); mi_Edit_Paste.addActionListener(this); mi_Edit_Cut.addItemListener(this); menuEdit.add(mi_Edit_Copy); menuEdit.add(mi_Edit_Paste); menuEdit.addSeparator(); menuEdit.add(mi_Edit_Cut); m_Menu_Bar.add(menuEdit); men

9、uHelp=new Menu(帮助); mi_Help_Sub=new MenuItem(主题); menuHelp.add(mi_Help_Sub); m_Menu_Bar.add(menuHelp); this.setMenuBar(m_Menu_Bar); add(North,MyLabel); add(South,MyButton); add(Center,textarea); / 设置窗体大小不可变 setResizable(false); pack(); / 显示窗体 show(); /连接服务器端 connect(); / 和服务器端连接程序 public void connec

10、t() try / 服务器应用程序端口设成了6544 ClientSocket=new Socket(localhost,6544); / 建立输入输出流对象 os=new PrintStream( new BufferedOutputStream(ClientSocket.getOutputStream(); is=new DataInputStream( new BufferedInputStream(ClientSocket.getInputStream(); s=is.readLine(); textarea.appendText(s+n); catch(Exception e) /响

11、应CHECKBOXMENUITEM被点击事件 public void itemStateChanged(ItemEvent e) if(e.getSource()=mi_Edit_Cut) /查看是否被选中 if(CheckboxMenuItem)e.getSource().getState() textarea.setText(nnnnnnttt+you have chosen + (CheckboxMenuItem)e.getSource().getLabel(); else textarea.setText(nnnnnnttt+you have not chosen + (Checkbo

12、xMenuItem)e.getSource().getLabel(); / 事件相应 public void actionPerformed(ActionEvent e) if(e.getActionCommand()=退出)dispose();System.exit(0);/ 点击”发送”按钮,向服务器发送消息 if(e.getSource()=MyButton) try os.print(textarea.getText(); os.flush(); catch(Exception e1) public static void main(String args) / 建立一个客户端对象 n

13、ew MyClient(); class WinAdptClient extends WindowAdapter MyClient m_Parent; WinAdptClient(MyClient p) m_Parent=p; public void windowClosing(WindowEvent e) /关闭窗口前先向SERVER端发送结束信息,并关闭各输入输出流与连接 try m_Parent.os.println(Bye); m_Parent.os.flush(); m_Parent.is.close(); m_Parent.os.close(); m_Parent.ClientSo

14、cket.close(); m_Parent.dispose(); System.exit(0); catch(IOException e2) -Server.java源代码-import java.io.*;import .*;import java.awt.*;import java.awt.event.*;/ 服务器端程序,并实现了多个用户请求的功能public class Server public static void main(String args) / 建立服务器对象 ServerService MyServer=new ServerService(6544,

15、10); class ServiceThread extends Frame implements Runnable/当CLIENT有请求时 SERVER端创建一个FRAME用于与之交互数据 ServerService FatherListener; Socket ConnectedClient; Thread ConnectThread; Panel ListenerPanel; TextArea ServerMeg; public ServiceThread(ServerService sv,Socket s)/构造函数 FatherListener=sv; ConnectedClient

16、=s; ConnectThread=new Thread(this); setTitle(Server(服务器端); setLayout(new BorderLayout(); ServerMeg=new TextArea(13,50); add(Center,ServerMeg); setResizable(false); pack(); show(); InetAddress ClientAddress=ConnectedClient.getInetAddress(); /获得请求服务的CLIENT端计算机的IP地址 ServerMeg.appendText(Server connect+

17、 to: nn+ClientAddress.toString()+.n); public void run() try/获得从CILENT读入的数据流 DataInputStream in=new DataInputStream( new BufferedInputStream(ConnectedClient.getInputStream();/获得向CILENT输出的数据流 PrintStream out=new PrintStream( new BufferedOutputStream(ConnectedClient.getOutputStream(); out.println(Hello

18、! Welcome connect to me(Server)!r); /向CLIENT端输出信息out.flush();/从CLIENT端读入信息 String s=in.readLine(); / 当客户端发出 “Bye”消息,服务器中断和他的socket连接while(!s.equals(Bye) ServerMeg.appendText(client端输入的信息为: n+s); /读入CLIENT端写入的下一行信息 s=in.readLine(); ConnectedClient.close(); catch(Exception e) FatherListener.addMeg(Cli

19、ent+closed.+n); dispose(); /服务器端的监听窗口class ServerService extends Frame /监听器 ServerSocket m_sListener; /显示信息的监听器窗口 TextArea ListenerMeg; public ServerService(int Port,int Count) try /建立监听服务 m_sListener=new ServerSocket(6544,10); /建立监听服务的窗口并显示 setTitle(Server Listener(监听器窗口); this.addWindowListener(new WinAdpt(); setLayout(new BorderLayout(); ListenerMeg=new TextArea( 监听服务已启动啦nnn,10,50); add(Center,ListenerMeg); setRe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论