




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1)第一个程序:ChatServerJFrame_TCP.java/* * 完成一个图形界面的流式套接字聊天程序 单线程,一对一聊天 这是服务器端程序 */public class ChatServerJFrame_TCP extends JFrame implements ActionListener ServerSocket serSkt;Socket scSkt;DataInputStream din;DataOutputStream dout;JTextArea msg;JTextField txt_str_send;JButton sendBtn, exitBtn;ChatServerJFrame_TCP() setTitle(聊天室-服务器);msg = new JTextArea(100, 250);msg.setBackground(Color.white);msg.setEditable(false);add(msg);/ 存放双方聊天记录的文本区JLabel send_lbl = new JLabel(请输入要发送的消息);txt_str_send = new JTextField(20);txt_str_send.addActionListener(this);/ 发送内容文本框sendBtn = new JButton(发送);sendBtn.addActionListener(this);/ 发送按钮exitBtn = new JButton(退出);exitBtn.addActionListener(this);/ 退出按钮JPanel jp = new JPanel();jp.add(send_lbl);jp.add(txt_str_send);jp.add(sendBtn);jp.add(exitBtn);add(jp, BorderLayout.SOUTH);setSize(500, 400);/ setLocation(400, 400);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try serSkt = new ServerSocket(6600);/ 在6600端口监听scSkt = serSkt.accept();din = new DataInputStream(scSkt.getInputStream();dout = new DataOutputStream(scSkt.getOutputStream();/ 返回socket的数据输入流和输出流 catch (IOException e) e.printStackTrace();public void sendMessage() / 发送方法try String strToClient = txt_str_send.getText().trim();/ 提取文本框输入内容if (strToClient.equals() / 发送内容不能为空JOptionPane.showMessageDialog(null, 发送内容不能为空);txt_str_send.requestFocus();return; else / 将内容发送给客户机,同时显示在聊天记录文本区中dout.writeUTF(strToClient);msg.setForeground(Color.red);msg.append(服务器说: + strToClient + n);txt_str_send.setText(); catch (IOException ie1) ie1.printStackTrace();public void receiveMessage() / 接受信息方法try while (true) / 可以不断地接收对方发送过来的消息String strFromClient = din.readUTF();msg.setForeground(Color.red);msg.append(客户端说: + strFromClient + n); catch (IOException ie2) msg.append(您的聊天客户已经离开了! + n); finally try din.close();dout.close();scSkt.close(); catch (IOException ex) ex.printStackTrace();System.exit(0);public void actionPerformed(ActionEvent e) if (e.getSource() = txt_str_send) sendMessage(); else if (e.getSource() = sendBtn) sendMessage(); else if (e.getSource() = exitBtn) System.exit(0);public static void main(String args) ChatServerJFrame_TCP chatServer = new ChatServerJFrame_TCP();chatServer.receiveMessage();/ 调用接收消息方法 2)第二个程序:ChattingRoom_UDP.java/做一个基于UDP的一对多的字符界面的聊天程序。/这是服务器端程序public class ChattingRoom_UDP public static void main(String args) throws IOException byte bufFromClient = new byte1024;/ 从客户端接收数据存储的字节数组byte bufToClient = new byte1024;/ 发送给客户数据的字节数组DatagramSocket serverDSocket;/ 数据报套接字,在3333端口监听DatagramPacket packetFromClient;/ 从接收客户端数据的数据报包DatagramPacket packetToClient;/ 向客户端传送数据的数据报包BufferedReader br;/ 键盘输入serverDSocket = new DatagramSocket(3333);/ 建立数据报式套接字System.out.println(正在等待接收数据);while (true) / 进入聊天流程packetFromClient = new DatagramPacket(bufFromClient, 1024);/ 构造接收客户端数据的数据报包serverDSocket.receive(packetFromClient);/ 接收数据报包String dataFromClient = new String(bufFromClient, 0,packetFromClient.getLength();/ packetFromClient.getLength():返回数据报中数据的长度/ 将从客户端那里接收到的数据转换为字符串/ if (dataFromClient.equals()/ continue;System.out.println(dataFromClient);/ 把客户说的话显示出来int i=dataFromClient.indexOf(bye);/System.out.println(i=+i);if (i=6) / 如果客户输入的是bye,客户结束聊天,离开了System.out.println(dataFromClient.substring(0, 5)+退出了聊天,继续其他客户聊天!);/ break;br = new BufferedReader(new InputStreamReader(System.in);System.out.print(服务器说:);String dataToClient = br.readLine().trim();/ 提取服务器从键盘输入的内容if (dataToClient.equals()/ 内容不能为空continue;bufToClient = dataToClient.getBytes();/ 把服务器输入的内容存入字节数组packetToClient = new DatagramPacket(bufToClient,bufToClient.length, packetFromClient.getAddress(),packetFromClient.getPort();/ 构造发送给客户端的数据报包serverDSocket.send(packetToClient);/ 发送数据报包if (dataToClient.equalsIgnoreCase(bye) / 如果服务器输入的是bye,结束聊天。退出程序System.out.println(聊天结束,退出);break; 3)第三个程序:ChattingRoom_UDP_Client.java/做一个基于UDP的一对多的字符界面的聊天程序。/这是客户端程序public class ChattingRoom_UDP_Client public static void main(String args) throws IOException String strFromServer, strToServer;/发送给服务器的字符串,接收自服务器的字符串byte bufFromServer;/接收自服务器数据的字节数组byte bufToServer;/发送给服务器数据的字节数组String host = localhost;/服务器主机名int port = 3333;/服务端口boolean exitFlag=false;/结束聊天标志DatagramPacket packetToServer, packetFromServer;/接收和发送数据报包DatagramSocket clientDSocket = new DatagramSocket();/数据报式套接字,端口随机BufferedReader br = new BufferedReader(new InputStreamReader(System.in);while (true) System.out.print(客户端1说:);strToServer = br.readLine().trim();/提取客户的键盘输入if (strToServer.equals()/不能为空continue;if(strToServer.equalsIgnoreCase(bye)exitFlag=true;strToServer=客户端1说:+strToServer;bufToServer = strToServer.getBytes();/把客户键盘输入的字符串存入字节数组packetToServer = new DatagramPacket(bufToServer,bufToServer.length, InetAddress.getByName(host), port);/构造发送数据包博爱clientDSocket.send(packetToServer);/发送数据报包给服务器if (exitFlag) / 如果客户输入的是bye,客户结束聊天,离开了System.out.println(strToServer.substring(0, 5)+聊天结束,退出!);break;bufFromServer=new byte1024;/初始化接收数据报包缓冲区数组packetFromServer=new DatagramPacket(bufFromServer,bufFromServer.length);/构造接收数据报包clientDSocket.receive(packetFromServer);/接收来自服务器的数据bufFromServer=packetFromServer.getData();/返回数据报中的数据strFromServer=new String(bufFromServer,0,packetFromServer.getLength();/将字节数据转换为字符串System.out.println(服务器说:+strFromServer);/显示if(strFromServer.equalsIgnoreCase(bye)/如果服务器输入了bye,则结束此次聊天System.out.println(聊天结束,退出);break; 4)第四个程序:ChattingRoomJFrameOne_UDP.java/* * 基于UDP的字符界面的聊天程序 * ChattingRoomJFrameTwo_UDP.java和ChattingRoomJFrameOne_UDP.java */public class ChattingRoomJFrameOne_UDP extends WindowAdapter implementsActionListener JTextArea msg;/ 显示双方聊天记录的文本区JScrollPane jscrPane;/ 滚动面板JTextField txt_str_send;/ 发送消息文本框JButton sendBtn, exitBtn;JFrame f;DatagramSocket oneDgSocket;/ 建立收发数据报包的数据报式套接字DatagramPacket packetFromTwo, packetToTwo;/ 收发数据报包的两个套接字byte messageFromTwo = new byte256;/ 接收数据报包数据部分内容的字节数组缓冲区byte messageToTwo = new byte256;/ 发送给对方的数据boolean exitFlag = false;/ 退出标志String host = localhost;/ 对方主机名int local_port = 7777;/ 本地数据报式套接字端口int two_port = 8888;/ 对方数据报式套接字端口ChattingRoomJFrameOne_UDP() f = new JFrame(聊天室-甲方);msg = new JTextArea(100, 250);msg.setBackground(Color.white);msg.setEditable(false);jscrPane = new JScrollPane(msg);f.add(jscrPane);/ 存放双方聊天记录的文本区JLabel send_lbl = new JLabel(请输入要发送的消息);txt_str_send = new JTextField(20);txt_str_send.addActionListener(this);/ 发送内容文本框sendBtn = new JButton(发送);sendBtn.addActionListener(this);/ 发送按钮exitBtn = new JButton(退出);exitBtn.addActionListener(this);/ 退出按钮JPanel jp = new JPanel();jp.add(send_lbl);jp.add(txt_str_send);jp.add(sendBtn);jp.add(exitBtn);try oneDgSocket = new DatagramSocket(local_port); catch (IOException e) e.printStackTrace();f.add(jp, BorderLayout.SOUTH);f.setSize(500, 400);f.setVisible(true);public void sendMessage() throws IOException / 发送消息方法String str = new String();if (!exitFlag) / 如果exitFlag为false,发送给对方的内容为本用户在文本框txt_str_send中输入的内容str = txt_str_send.getText().trim();if (str.equals() / 发送内容不能为空JOptionPane.showMessageDialog(null, 发送信息不能为空!);txt_str_send.requestFocus();return; else if (exitFlag) / 如果exitFlag为false,发送给对方的内容如下设置str = 服务器已经退出聊天室!;messageToTwo = str.getBytes();/ 将发送内容存储进字节数组packetToTwo = new DatagramPacket(messageToTwo, messageToTwo.length,InetAddress.getByName(host), two_port);/ 构造发送数据报包的packetoneDgSocket.send(packetToTwo);/ 发送msg.append(服务器说: + str + n);/ 同时将发送给对方的内容显示在自己界面的聊天记录区txt_str_send.setText();public void receiveMessage() throws IOException / 接收数据过程packetFromTwo = new DatagramPacket(messageFromTwo,messageFromTwo.length);/ 构造接收数据报包while (true) /if(exitFlag)/break;/oneDgSocket.receive(packetFromTwo);/ 接收对方发送过来的数据报包String strTmp = new String(messageFromTwo, 0, packetFromTwo.getLength();/ 解析数据报包,提取其中的数据内容并转换为字符串msg.append(客户端说: + strTmp + n);/ 显示在聊天记录区public void windowClosing(WindowEvent e) / 处理JFrame右上角的关闭按钮exitFlag = true;/ 设置exitFlag值为truetry sendMessage();/ 发送一方已经退出聊天的讯息给对方 catch (IOException ee) ee.printStackTrace();System.exit(0);/ 关闭JFrame窗口public void actionPerformed(ActionEvent e) / 动作事件接口方法处理if (e.getSource() = txt_str_send | e.getSource() = sendBtn) / 如果触发动作事件的事件源是文本框或者发送按钮try sendMessage(); catch (IOException ex) ex.printStackTrace(); else if (e.getSource() = exitBtn) / 如果触发动作事件的事件源是退出按钮int i = JOptionPane.showConfirmDialog(null, 确定要退出聊天吗?);/ 弹出式对话框询问是否确定退出if (i = 0) / 确定退出exitFlag = true;/ 设置exitFlag值为truetry sendMessage();/ 发送一方已经退出聊天的讯息给对方 catch (IOException ee) ee.printStackTrace();System.exit(0);public static void main(String args) throws IOException ChattingRoomJFrameOne_UDP chatServer = new ChattingRoomJFrameOne_UDP();/ 实例化类对象chatServer.receiveMessage();/ 调用类对象的接收消息方法/* * 基于UDP的字符界面的聊天程序 * ChattingRoomJFrameTwo_UDP.java和ChattingRoomJFrameOne_UDP.java */public class ChattingRoomJFrameTwo_UDP extends WindowAdapter implementsActionListener JTextArea msg;JScrollPane jscrPane;JTextField txt_str_send;JButton sendBtn, exitBtn;JFrame f;DatagramSocket twoDgSocket;/收发数据报包的套接字DatagramPacket packetFromOne, packetToOne;/收发数据报包byte messageFromOne = new byte256;byte messageToOne = new byte256;boolean exitFlag = false;String host = localhost;int local_port = 8888;/本地套接字端口int client_port = 7777;/对方套接字端口ChattingRoomJFrameTwo_UDP() f = new JFrame(聊天室-客户机);msg = new JTextArea(100, 250);msg.setBackground(Color.white);msg.setEditable(false);jscrPane = new JScrollPane(msg);f.add(jscrPane);/ 存放双方聊天记录的文本区JLabel send_lbl = new JLabel(请输入要发送的消息);txt_str_send = new JTextField(20);txt_str_send.addActionListener(this);/ 发送内容文本框sendBtn = new JButton(发送);sendBtn.addActionListener(this);/ 发送按钮exitBtn = new JButton(退出);exitBtn.addActionListener(this);/ 退出按钮JPanel jp = new JPanel();jp.add(send_lbl);jp.add(txt_str_send);jp.add(sendBtn);jp.add(exitBtn);try twoDgSocket = new DatagramSocket(local_port); catch (IOException e) e.printStackTrace();f.add(jp, BorderLayout.SOUTH);f.setSize(500, 400);f.setVisible(true);public void sendMessage() throws IOException /发送消息方法String str = new String();if (!exitFlag) str = txt_str_send.getText().trim();if (str.equals() JOptionPane.showMessageDialog(null, 发送信息不能为空!);txt_str_send.requestFocus();return;else if(exitFlag)str=客户已经退出了聊天室;messageToOne = str.getBytes();packetToOne = new DatagramPacket(messageToOne,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 矿石破碎与磨矿智能控制系统创新创业项目商业计划书
- 社交媒体KOL营销创新创业项目商业计划书
- 2025年卡持设备项目申请报告模范
- 漏洞检测修复软件创新创业项目商业计划书
- 自动化物流分拣创新创业项目商业计划书
- DB15T 2595-2022 中草引1号百脉根种植技术规程
- 虚拟现实辅助下的2025年医学生技能培训效果评估报告
- 学业困难学生支持策略研究报告
- 包装行业绿色生产与环保技术应用报告
- 应急避难场所汇报
- 中国急性缺血性卒中诊治指南(2023)解读
- MOOC 中国电影经典影片鉴赏-北京师范大学 中国大学慕课答案
- 15D501建筑物防雷设施安装图集
- 陕旅版英语字帖三年级英语下册单词表衡水体描红字贴三年级起点
- 1-溴化锂空调机组回收拆除施工方案
- 2020年06月内蒙古巴林左旗基层医疗卫生机构公开招聘护理人员10人笔试参考题库含答案解析
- XXX电力系统基础知识培训考试题
- 上海文化发展基金会调研报告
- GB/T 17478-2004低压直流电源设备的性能特性
- GB/T 13477.10-2017建筑密封材料试验方法第10部分:定伸粘结性的测定
- 国网十八项重大反措试题库完整
评论
0/150
提交评论