实现一个一对多聊天程序

上传人:s9****2 文档编号:506394931 上传时间:2023-06-20 格式:DOC 页数:14 大小:167.50KB
返回 下载 相关 举报
实现一个一对多聊天程序_第1页
第1页 / 共14页
实现一个一对多聊天程序_第2页
第2页 / 共14页
实现一个一对多聊天程序_第3页
第3页 / 共14页
实现一个一对多聊天程序_第4页
第4页 / 共14页
实现一个一对多聊天程序_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《实现一个一对多聊天程序》由会员分享,可在线阅读,更多相关《实现一个一对多聊天程序(14页珍藏版)》请在金锄头文库上搜索。

1、实现一个一对多的聊天程序一、实验题目实现一个一对多的聊天程序。其基本过程如下:服务器首先启动,创建套接 字后等待客户的连接;客户启动以后,创建套接字,然后和服务器建立连接;连 接建立后,客户机和服务器可以通过建立的套接字连接进行通信。 服务器和客户 端可以是一台电脑上的两个进程,也可以分别部署在两台电脑上。二、原理概述TCP是一种可靠的、基于连接的网络传输协议,当两个网络进程准备进行通 信时,都必须首先建立各自的一个套接口, 其中服务器建立套接口后,侦听来自 网络的客户连接请求,客户通过套接口,指定服务器的 IP地址和端口号,便可 与服务器通信。聊天程序采用的是C/S通信模式,即客户/服务模式

2、,它把一个应用划分成 功能不同的两个部分,分别在不同的计算机上运行,其中一个为服务端程序,用 来响应和提供固定的服务,一个为客户端程序,用来向服务端提出请求和要求某 种服务。在数量关系上,通常有一对多的(即一个服务端程序和一个客户端程序 之间通信)。客户机/服务器模式在操作过程中采取的是主动请示方式:首先服务器方要先启动,并根据请示提供相应服务:(1)打开一通信通道并告知本地主机,它愿意在某一个公认地址上接收客户请 求。(2)等待客户请求到达该端口。(3)接收到重复服务请求,处理该请求并发送应答信号。(4)返回第二步,等待另一客户请求(5)关闭服务器。客户方:(1)打开一通信通道,并连接到服务

3、器所在主机的特定端口。(2)向服务器发送服务请求报文,等待并接收应答;继续提出请求 (3)请求结束后关闭通信通道并终止。图1基于连接的服务者、客户服务流程图三、设计方案服务程序运行在服务器主机的某个端口上,一旦启动服务,它将在这个端口 上倾听,等待客户程序发来的请求。服务器的套接口用服务器套接口类(ServerSocket )来建立。1、根据程序设计题目时首先明确程序的目的以及任务2、利用JAVA语言编写程序,使用套接字来完成测试功能。3、程序在Eclipcse环境中编译,根据错误提示查找分析并修改原程序四、程序编写:import java.io.BufferedReader;import j

4、ava.io.BufferedWriter;import java.io .10 Exceptio n;import java.i o.ln putStreamReader;import java.io.OutputStreamWriter;import .ServerSocket;import .Socket;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;Title 服务器author Allenversion 2010-5-3 下午 08:28:36*/ public c

5、lass Server ServerSocket server = null;List clients = new ArrayList();SimpleDateFormat sdf = new SimpleDateFormat(MM-dd HH:mm:ss);public void begin()/ 读取客户端的 userNameBufferedReader bufUserName = null;boolean started = false;String userName = null;try server = new ServerSocket(8888);System.out.printl

6、n( 服务器启动成功!端口 + server.getLocalPort() 监听中 .);started = true;while(started)Socket socket = server.accept();bufUserName = new BufferedReader(new InputStreamReader(socket.getInputStream();userName = bufUserName.readLine();System.out.println( 用户: + userName + IP: +socket.getLocalSocketAddress() + client

7、 connect.);/bufUserName.close();SC sc = new SC(socket);new Thread(sc).start();clients.add(sc);程序安全退 catch (IOException e) System.out.println(端口启动失败 . + e.getMessage() + n出.);System.exit(0);public static void main(String args) new Server().begin();private class SC implements RunnableBufferedReader br

8、 = null;BufferedWriter bw = null;boolean isConnect = false;public SC(Socket socket)this.socket = socket;try br = new BufferedReader(new InputStreamReader(socket.getInputStream();bw = new BufferedWriter(newOutputStreamWriter(socket.getOutputStream();isConnect = true; catch (IOException e) e.printStac

9、kTrace();public void sendMessage(String str)try /bw.write(str + sdf.format(new Date() + n);bw.write(str);bw.newLine();bw.flush(); catch (IOException e) e.printStackTrace();public void run() tryString strRead = null;while(isConnect)while(strRead = br.readLine() != null)System.out.println(Server readL

10、ine. + strRead);for(int i = 0; i clients.size(); i+) clients.get(i).sendMessage(strRead);catch(Exception e)try br.close();socket.close(); catch (IOException e1) e1.printStackTrace();e.getStackTrace();finallytry if(br != null) br.close();if(bw != null) bw.close();socket.close();/s = null; catch (IOEx

11、ception e1) e1.printStackTrace();import java.awt.*;import java.awt.event.*;import java.io.*;import .Socket;import .UnknownHostException;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.*;* Title 客户端* author Allen* version 2010-5-3 下午 08:28:47*/ public class Client extends J

12、Frame JTextField jname = null;JTextField text = new JTextField();JTextArea area = new JTextArea(10, 40);BufferedReader br = null;BufferedWriter bw = null;BufferedWriter name_buf = null;boolean connected = false;SimpleDateFormat sdf = new SimpleDateFormat(MM-dd HH:mm:ss); static String userName = nul

13、l;public Client()initFrame();public static void main(String args) new Client();public String getUserName()return userName;public void initFrame()/setTitle(userName);setSize(400, 300);setLocation(500, 200);this.setResizable(false);JPanel panel = new JPanel();GridBagConstraints gbc = new GridBagConstr

14、aints(); /GridBagLayout layout = new GridBagLayout(); FlowLayout layout = (FlowLayout)panel.getLayout(); layout.setAlignment(FlowLayout.RIGHT); gbc.anchor = GridBagConstraints.LINE_END; JButton btn_send = new JButton(send); JButton btn_clear = new JButton(clear); panel.setLayout(layout); panel.add(btn_clear);panel.add(btn_send, gbc); area.setLineWrap(true); area.setEditable(false);JScrollPane jsp = new JScrollPane(area); this.add(jsp, BorderLayout.NORTH); this.add(panel, BorderLayout.CENTER); this.add(text, BorderLayout.SOUTH); pack();SendListener sendListener = new Send

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号