JAVA 客户端服务器实验

客户端

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1
{   public static void main(String args[])
{   new ComputerClient();
}
} 
class ComputerClient extends Frame implements Runnable,ActionListener
{   Button connection,send;
    TextField inputText,showResult; 
    Socket socket=null;  
     DataInputStream in=null;
     DataOutputStream out=null;
    Thread thread;
 ComputerClient ()
      { socket=new Socket();
      setLayout(new FlowLayout()) ;
      Box box=Box.createVerticalBox();
      connection=new Button("connect the server");
      send=new Button ("send");
      send.setEnabled(false) ;
      inputText=new TextField(20);
      showResult=new TextField(20);
      box.add(connection);
      box.add(new Label ("请输入圆的半径"));
      box.add(inputText);
      box.add(send);
      box.add(new Label("圆的面积为"));
      box.add(showResult);
      connection.addActionListener(this);
      send.addActionListener(this);
      thread=new Thread(this);
      add(box);
      setBounds(30,50,300,400);
      setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{  public void windowClosing(WindowEvent e)
{  System.exit(0);
}
});
}
  public void actionPerformed(ActionEvent e)
{ if(e.getSource()==connection)
{  try 
{if(socket.isConnected())
{}
  else
{  InetAddress  address=InetAddress.getByName("172.16.8.20");
  InetSocketAddress socketAddress=new InetSocketAddress(address, 4331);
  socket.connect(socketAddress) ;
  in =new DataInputStream(socket.getInputStream()) ;
  out = new DataOutputStream(socket.getOutputStream());
  send.setEnabled(true);
  thread.start();
}
}
catch (IOException ee){}
}
if(e.getSource()==send)
{ String s=inputText.getText();
  if(s!=null)
{  try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
public void run()
{  String s=null;
while(true)
{  try{ s=in.readUTF();
  showResult.setText(s);
}
catch(IOException e)
{   showResult.setText("与服务器已断开");
   break;
}
}
}
}

服务器端

import java.io.*;
import java.net.*;
import java.util.*;
public class 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 el)
{   System.out.println("正在监听");
}
try{ System.out.println("等待客戸呼叫");
    you=server.accept();
    System.out.println("客戸的地址:"+you.getInetAddress());
}
    catch(IOException e)
{    System.out.println("正在等待客戸");
}
   if(you!=null)
      { new Server_thread(you).start(); 
}
}
}
}
 
     class Server_thread extends Thread 
{    Socket socket;
      DataOutputStream out=null;
      DataInputStream in=null;
      String s=null;
      boolean quesion=false;
      Server_thread(Socket t)
{     socket=t;
      try { out=new DataOutputStream (socket.getOutputStream());
      in=new DataInputStream (socket.getInputStream());
}
     catch (IOException e)
{}
}
public void run()
 { while (true)
{ double a[]=new double[3];
   int i=0;
  try{ s=in.readUTF();
    quesion=false;
     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("请输入数字字符");
   quesion=true;
}
}
if (quesion==false)
{ double p=3.14*(a[0])*(a[0]);
  out.writeUTF(" "+(p));
}
}
catch (IOException e)
{ System.out.println("客户离开");
return;
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值