熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java C/S mode --Client

2022-06-13   來源: JSP教程 

  作者ludejun
  
  import javaapplet*;
  import javaawt*;
  import javaawtevent*;
  import javaio*;
  import *;
  //public String str;
  
  
  class javaclient extends Panel implements ActionListener
  {
  
  
  TextField tf;
  LudCanvas cv;
  Button b;
  
  LudPanel(LudCanvas cv)
  {
  thiscv = cv;
  
  tf = new TextField(ludejun);
  add(tf);
  
  b = new Button(退出);
  baddActionListener(this);
  add(b);
  b = new Button(發送);
  baddActionListener(this);
  add(b);
  }
  
  
  public str actionPerformed(ActionEvent ev)
  {
  String str = evgetActionCommand();
  if( strequals(退出) )
  {
  //tfsetText(This is my first button);
  Systemexit();
  }
  else
  {
  str = tfgetText();
  tfsetText(開始檢測發送);
  return str;
  
  }
  }
  
  
  
  
  public static void main(String args[])
  {
  String data;
  Socket Client;
  DataInputStream InputS;
  DataInputStream KeyS;
  PrintStream OutputS;
  
  Basic h = new Basic();
  
  int i = ;
  
  
  try
  {
  Client = new Socket(args[]);
  InputS = new
  DataInputStream(ClientgetInputStream());
  OutputS = new PrintStream(ClientgetOutputStream());
  KeyS = new DataInputStream(Systemin);
  }
  catch(IOException e)
  {
  Systemoutprintln(Cannot Connect with Server);
  return;
  }
  
  try
  {
  while (i<5)
  {
  data = KeyS.readLine();
  OutputS.println(data);
  System.out.println("ECHO From Server:"+ InputS.readLine());
  i++;
  }
  }
  catch(IOException e)
  {
  System.out.println("IOException Happened");
  }
  
  try
  {
  System.out.println("Now will end this program");
  Client.close();
  }
  catch(IOException e)
  {
  System.out.println("system cannot close socket");
  }
  }
  }
  
  
  
  //@@@@@@@@@@@@@@@@@@@@@@@222
  
  
  
  class LudCanvas extends Canvas
  {
  public void paint(Graphics g)
  {
  Rectangle r = getBounds();
  g.setColor(Color.red);
  int panelHeight = 15;
  int b = 3;
  g.drawRect(b , b , r.width-b-b , r.height-b-b-panelHeight);
  int t = 10;
  g.drawRect(t , t , r.width-t-t , r.height-t-t-panelHeight);
  
  g.drawString("The first applet of mine", 120, 30);
  
  
  Image img = Toolkit.getDefaultToolkit().getImage("ie.gif");
  
  g.drawImage(img, 50, 50, this);
  
  
  }
  
  }
  
  
  class LudPanel extends Panel implements ActionListener
  {
  TextField tf;
  LudCanvas cv;
  Button b;
  
  LudPanel(LudCanvas cv)
  {
  this.cv = cv;
  
  tf = new TextField("ludejun",30);
  add(tf);
  
  b = new Button("退出");
  b.addActionListener(this);
  add(b);
  b = new Button("發送");
  b.addActionListener(this);
  add(b);
  }
  
  
  public void actionPerformed(ActionEvent ev)
  {
  String str = ev.getActionCommand();
  if( str.equals("退出") )
  {
  //tf.setText("This is my first button");
  System.exit(0);
  }
  else
  {
  tf.setText("開始檢測發送......");
  }
  }
  }
  :em51: :em51: :em51:

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19690.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.