java实验报告 读取服务器端的窗口

上传人:第*** 文档编号:34006157 上传时间:2018-02-19 格式:DOC 页数:5 大小:53KB
返回 下载 相关 举报
java实验报告 读取服务器端的窗口_第1页
第1页 / 共5页
java实验报告 读取服务器端的窗口_第2页
第2页 / 共5页
java实验报告 读取服务器端的窗口_第3页
第3页 / 共5页
java实验报告 读取服务器端的窗口_第4页
第4页 / 共5页
java实验报告 读取服务器端的窗口_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《java实验报告 读取服务器端的窗口》由会员分享,可在线阅读,更多相关《java实验报告 读取服务器端的窗口(5页珍藏版)》请在金锄头文库上搜索。

1、实验 3 读取服务端的窗口1.相关知识点网络套接字是基于 TCP 协议的有连接通信,套接字连接就是客户端的套接字和服务器端的套接字对象通过输入输出流连接在一起。服务器建立ServerSocket 对象,ServerSocket 对象负责等待客户端请求建立套接字连接,而客户端建立 Socket 对象向服务器端发出套接字请求。可以使用 Socket 类不带参数的构造方法public Socket()创建一个套接字对象,该对象不请求任何连接。该对象再调用public void connect(SocketAddress endpoint) throws IOException请求和参数 Socket

2、Address 指定地址的套接字建立连接。为了使用 connect 方法,可以使用 SocketAddress 的子类:InetSocketAddress 创建一个对象,InetSocketAddress 的构造方法是:public InetSocketAddress(InetAddress addr,int port)2.实验目的学会使用套接字读取服务器端的对象。3.实验要求客户端利用套接字连接将服务器端的 JFrame 对象读取到客户端。首先将服务器端的程序编译通过,并运行起来,等待请求套接字连接。4.程序模板/客户端模板 client.javaimport java.io.*;impor

3、t .*;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 ReadWin

4、dow();thread=new Thread(readWindow);System.out.print(输入服务器的IP:);String IP=scanner.nextLine();System.out.print(输入端口号:);int port=scanner.nextInt();if(mysocket.isConnected()elseInetAddress address=InetAddress.getByName(IP);InetSocketAddress socketAddress=new InetSocketAddress(address,port);mysocket.con

5、nect(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

6、implements RunnableObjectInputStream in;public void setObjectInputStream(ObjectInputStream in)this.in=in;public void run()double result=0;while(true)tryjavax.swing.JFrame window=(javax.swing.JFrame)in.readObject();window.setTitle(这是从服务器上读取的窗口);window.setVisible(true);window.requestFocusInWindow();wi

7、ndow.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

8、=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=n

9、ull;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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void run()tryout.writeObject(window); catch(IOException e)System.out.println(客户离开);运行结果如下:(1)客户端:(2)服务器端:

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

当前位置:首页 > 办公文档 > 解决方案

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