Java 2实用教程第六版上机实践13实验3读取服务器端的窗口

!!!注意客户端和服务器端并不能在一个项目中!!!

代码如下:

客户端代码部分:
import java.io.*;
import java.net.*;
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 Runnable {
       ObjectInputStream 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 java.net.*;
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)

   {
       try {
           server = new ServerSocket(4331);
       }
       catch(IOException e1)
           {
               System.out.println("正在监听");
           }
           try {
               you = server.accept();
               System.out.println("客户的地址:" + you.getInetAddress());
           } catch (IOException e) {
               System.out.println("正在等待客户");
           }
           if (you != null) {
               new ServerThread(you).start();
           }
       }
   }
}
class ServerThread extends Thread {
   Socket socket;
   ObjectInputStream in=null;
   ObjectOutputStream out=null;
   JFrame window;
   JTextArea text;
   ServerThread(Socket t){
       socket=t;
       try {
           out = 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() {
       try {
           out.writeObject(window);
       } catch (IOException e) {
           System.out.println("客户离开");
       }
   }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值