基于GUI的网络通信程序的设计说明

上传人:xmg****18 文档编号:118807393 上传时间:2019-12-25 格式:DOC 页数:7 大小:154KB
返回 下载 相关 举报
基于GUI的网络通信程序的设计说明_第1页
第1页 / 共7页
基于GUI的网络通信程序的设计说明_第2页
第2页 / 共7页
基于GUI的网络通信程序的设计说明_第3页
第3页 / 共7页
基于GUI的网络通信程序的设计说明_第4页
第4页 / 共7页
基于GUI的网络通信程序的设计说明_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《基于GUI的网络通信程序的设计说明》由会员分享,可在线阅读,更多相关《基于GUI的网络通信程序的设计说明(7页珍藏版)》请在金锄头文库上搜索。

1、. . . .Java程序设计实验报告-实验3实验室: 2014 年 12 月 10 日学院计算机与信息学院专业班级姓名成绩课程名称Java程序设计实验项目名 称 实验三 基于GUI的网络通信程序设计指导教师教师评语 教师签名: 年 月 日1、 实验目的1掌握Java中GUI程序的编写,包括事件监听机制。2掌握Java的网络通信编程,ServerSocket,Socket类的使用。3掌握Java中多线程的编程,Thread类,Runnable接口的使用。4掌握用面向对象的方法分析和解决复杂问题。2、 实验原理1. 利用java.awt和javax.swing包提供的各种组件实现服务器和客户端的

2、界面设计。2使用套接字实现基于TCP协议的服务器和客户端。3.为服务器和客户端界面中的有关组件添加消息相应,实现交互。三、使用硬件、软件环境 PC 计算机一台,配置为CPU为2.6G,内存为4G,硬盘为1T,安装Windows8操作系统。另外,使用JCreator,JDK1.8.0等软件四、实验过程、步骤及原始记录(算法、原程序、测试结果,分析等) 1.实验过程: 首先实现界面的编写,之后使用套接字实现基于TCP协议的通信,再设置监视 器,为相应的组件添加消息相应。 2.源程序:1.客户端程序:KeHuDuan.javaimport java.awt.*;import java.awt.eve

3、nt.*;import java.util.*;import java.io.*;import .*;import javax.swing.*;public class KeHuDuanpublic static void main(String args)MyFrame client = new MyFrame();client.setVisible(true);client.setResizable(false);client.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);class MyFrame extends JFrameJTex

4、tField ip;JTextField port;JTextField cin;JTextArea content;JButton connect;JButton say;Socket socket;MyFrame()init();ConnectListen cListener = new ConnectListen();SayListen sListener = new SayListen();connect.addActionListener(cListener);say.addActionListener(sListener);void init() setLayout(new Flo

5、wLayout(); setSize(400,400); setLocation(800,100); add(new JLabel(Serve ip); ip = new JTextField(127.0.0.1,8); add(ip); add(new JLabel(Serve port); port = new JTextField(8888,8); add(port); connect = new JButton(connect); add(connect); content = new JTextArea(16,35); JScrollPane scroll = new JScroll

6、Pane(content); add(scroll); add(new Label(Say:); cin = new JTextField(25); add(cin); say = new JButton(say); add(say);class ConnectListen implements ActionListenerint portNum;public void actionPerformed(ActionEvent e)connect.setEnabled(false);try portNum =Integer.parseInt(port.getText();socket = new

7、 Socket(ip.getText(),portNum);ClientThread ct = new ClientThread();ct.start(); catch (Exception ex) class SayListen implements ActionListenerString str;public void actionPerformed(ActionEvent e)try PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),t

8、rue);str=cin.getText();if(!str.isEmpty()out.println(str);content.append(me:+str+n);out.flush();cin.setText(); catch (Exception ex) class ClientThread extends Threadpublic void run()try BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream();String str;while(true)str = in

9、.readLine();/System.out.println (a);content.append(str+n); catch (Exception ex) 2.服务器端程序:import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import .*;import javax.swing.*;public class FuWuQipublic static void main(String args)MyFrame serve = new MyFrame();serve.setVisible(

10、true);serve.setResizable(false);serve.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);class MyFrame extends JFrameJTextField port;JButton start;JTextArea content;JTextField cin;JButton say;Socket socket;MyFrame()init();StartListen sListen = new StartListen();SayListen stListen = new SayListen();st

11、art.addActionListener(sListen);say.addActionListener(stListen);void init() setLayout(new FlowLayout();setSize(400,400);setLocation(400,100);add(new JLabel(Port:);port = new JTextField(8888,25);add(port);start = new JButton(Start);add(start);content = new JTextArea(15,35);JScrollPane scroll = new JSc

12、rollPane(content);add(scroll);add(new JLabel(Say:);cin = new JTextField(26);add(cin);say = new JButton(Say);add(say);class StartListen implements ActionListenerpublic void actionPerformed(ActionEvent e)start.setEnabled(false);try ServerSocket s = new ServerSocket(Integer.parseInt(port.getText();socket = s.accept();PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),true);out.println(连接成功);content.append(连接成功+n);ServerThread st = new ServerThread();st.start(); catch (Exception

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

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