




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验3 读取服务端的窗口1. 相关知识点网络套接字是基于TCP协议的有连接通信,套接字连接就是客户端的套接字和服务器端的套接字对象通过输入输出流连接在一起。服务器建立ServerSocket对象,ServerSocket对象负责等待客户端请求建立套接字连接,而客户端建立Socket对象向服务器端发出套接字请求。可以使用Socket类不带参数的构造方法public Socket()创建一个套接字对象,该对象不请求任何连接。该对象再调用public void connect(SocketAddress endpoint) throws IOException请求和参数SocketAddress指定地址的套接字建立连接。为了使用connect方法,可以使用SocketAddress的子类:InetSocketAddress创建一个对象,InetSocketAddress的构造方法是:public InetSocketAddress(InetAddress addr,int port)2. 实验目的学会使用套接字读取服务器端的对象。3. 实验要求客户端利用套接字连接将服务器端的JFrame对象读取到客户端。首先将服务器端的程序编译通过,并运行起来,等待请求套接字连接。4. 程序模板/客户端模板client.javaimport java.io.*;import .*;import java.util.*;public class Client public static void main(String args) Scanner scanner =new Scanner (System. in); Socket mysocket=null; ObjectInputStream inObject=null; ObjectOutputStream outObject=null; Thread thread; ReadWindow readWindow=null; try mysocket=new Socket(); readWindow=new ReadWindow(); thread=new Thread(readWindow); System.out.print(输入服务器的IP:); String IP=scanner.nextLine(); System.out.print(输入端口号:); int port=scanner.nextInt(); if(mysocket.isConnected() else InetAddress address=InetAddress.getByName(IP); InetSocketAddress socketAddress=new InetSocketAddress(address,port); mysocket.connect(socketAddress); InputStream in=mysocket.getInputStream(); OutputStream out=mysocket.getOutputStream(); inObject=new ObjectInputStream(in); outObject=new ObjectOutputStream(out); readWindow.setObjectInputStream(inObject); thread.start(); catch(Exception e) System.out.println(服务器已断开+e); class ReadWindow implements RunnableObjectInputStream in;public void setObjectInputStream(ObjectInputStream in)this.in=in;public void run()double result=0;while(true) try javax.swing.JFrame window=(javax.swing.JFrame)in.readObject(); window.setTitle(这是从服务器上读取的窗口); window.setVisible(true); window.requestFocusInWindow(); window.setSize(600,800); catch(Exception e) System.out.println(与服务器已断开+e); break; import java.io.*;import .*;import java.util.*;import java.awt.*;import javax.swing.*;public class Server public static void main(String args) ServerSocket server=null;ServerThread thread;Socket you=null;while(true)tryserver=new ServerSocket(4331);catch(IOException e1)System.out.println(正在监听);tryyou=server.accept();System.out.println(客户的地址:+you.getInetAddress();catch(IOException e)System.out.println(正在等待客户);if(you!=null)new ServerThread(you).start();class ServerThread extends ThreadSocket socket;ObjectInputStream in=null;ObjectOutputStream out=null;JFrame window;JTextArea text;ServerThread(Socket t)socket=t;tryout=new ObjectOutputStream(socket.getOutputStream(); in=new ObjectInputStream(socket.getInputStream(); catch(IOException e)window=new JFrame();text=new JTextArea();for(int i=1;i+20;i+)text.append(你好,我是服务器上的文本区组件n);text.setBackground(Color.yellow);window.add(text);window.setDefaultClos
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 考研中的自我管理与规划
- 策划展览活动方案
- 端午节小学活动方案
- 线下公益交友活动方案
- 石油公司品牌策划方案
- 礼仪协会开展活动方案
- 碘缺乏宣传活动方案
- 美工汽车活动方案
- 童装年前活动方案
- 甲亢国庆活动方案
- 2025年第一届安康杯安全生产知识竞赛试题题库及答案(完整版)
- 电力工程冬季施工安全技术措施
- 贵州省贵阳市2026届高三上学期摸底考试数学试卷含答案
- 公司年度员工安全教育培训计划
- 潍坊市2026届高三开学调研监测考试化学试题及答案
- 供电所安全教育培训课件
- 2025年杭州市上城区望江街道办事处 编外人员招聘8人考试参考试题及答案解析
- 百果园水果知识培训资料课件
- 商业地产策划流程
- 2025年灌注桩考试题及答案
- 公司安全生产责任书范本
评论
0/150
提交评论