| | 首页 | 文章中心 | 下载中心 | 本站特供 | 软硬件结合论坛 | 软硬件结合博客 | | |
![]() | |
| 您现在的位置: 中国软硬件结合技术网 >> 文章中心 >> 软件技术 >> VB、JAVA等 >> 正文 |
|
|||||
| 一个java(C/S)程序 | |||||
| 作者:大连交通大学_耿祥义 文章来源:大连交通大学 点击数: 更新时间:2006-12-31 | |||||
| 1) 客户端 import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Computer_client extends Applet implements Runnable,ActionListener { Button 计算;TextField 输入三边长度文本框,计算结果文本框; Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread thread; public void init() { setLayout(new GridLayout(2,2)); Panel p1=new Panel(),p2=new Panel(); 计算=new Button(" 计算"); 输入三边长度文本框=new TextField(12); 计算结果文本框=new TextField(12); p1.add(new Label("输入三角形三边的长度,用逗号或空格分隔:")); p1.add( 输入三边长度文本框); p2.add(new Label("计算结果:")); p2.add(计算结果文本框); p2.add(计算); 计算.addActionListener(this); add(p1); add(p2); } public void start() { try { //和小程序所驻留的服务器建立套接字连接: socket = new Socket(this.getCodeBase().getHost(), 4331); in =new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); } catch (IOException e){} if(thread == null) { thread = new Thread(this); thread.start(); } } public void run() { String s=null; while(true) { try{ s=in.readUTF();//堵塞状态,除非读取到信息。 计算结果文本框.setText(s); } catch(IOException e) { 计算结果文本框.setText("与服务器已断开"); break; } } } public void actionPerformed(ActionEvent e) { if(e.getSource()==计算) { String s=输入三边长度文本框.getText(); if(s!=null) { try { out.writeUTF(s); } catch(IOException e1){} } } } } (2) 服务器端. import java.io.*; import java.net.*; import java.util.*; public class Computer_server { public static void main(String args[]) { ServerSocket server=null; Server_thread thread; Socket you=null; while(true) { try{ server=new ServerSocket(4331); } catch(IOException e1) { System.out.println("正在监听"); //ServerSocket对象不能重复创建。 } try{ you=server.accept(); System.out.println("客户的地址:"+you.getInetAddress()); } catch (IOException e) { System.out.println("正在等待客户"); } if(you!=null) { new Server_thread(you).start(); //为每个客户启动一个专门的线程。 } else { continue; } } } } class Server_thread extends Thread { Socket socket; DataOutputStream out=null; DataInputStream in=null; String s=null; Server_thread(Socket t) { socket=t; try { in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); } catch (IOException e) {} } public void run() { while(true) { double a[]=new double[3] ;int i=0; try{ s=in.readUTF();堵塞状态,除非读取到信息。 StringTokenizer fenxi=new StringTokenizer(s," ,"); while(fenxi.hasMoreTokens()) { String temp=fenxi.nextToken(); try{ a[i]=Double.valueOf(temp).doubleValue();i++; } catch(NumberFormatException e) { out.writeUTF("请输入数字字符"); } } double p=(a[0]+a[1]+a[2])/2.0; out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2]))); sleep(2); } catch(InterruptedException e){} catch (IOException e) { System.out.println("客户离开"); break; } } } } |
|||||
| 文章录入:pangyusheng 责任编辑:Polylove | |||||
| 【发表评论】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 最新热点 | 最新推荐 | 相关文章 | ||
| 没有相关文章 |
| |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 | | |
![]() |
Copyright ©2004 - 2006 中国软硬件结合技术网 91tech.net 91tech.cn 91tech.com 站长:Polylove |