{ 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=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(\ } }
catch (IOException e)
{ System.out.println(\客户离开\ return; } } } }
2. 客户端Client.java
import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Client
{ public static void main(String args[]) { new ChatClient(); } }
class ChatClient extends Frame implements Runnable,ActionListener { Button connection,send;
TextField inputName,inputContent; TextArea chatResult; Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread thread; String name=\ public ChatClient () { socket=new Socket();
Box box1=Box.createHorizontalBox(); connection=new Button(\连接服务器\ send=new Button(\发送\ send.setEnabled(false); inputName=new TextField(6); inputContent=new TextField(22); chatResult=new TextArea(); box1.add(new Label(\输入妮称:\ box1.add(inputName); box1.add(connection);
Box box2=Box.createHorizontalBox(); box2.add(new Label(\输入聊天内容:\ box2.add(inputContent); box2.add(send);
connection.addActionListener(this); send.addActionListener(this); thread=new Thread(this); add(box1,BorderLayout.NORTH);
add(box2,BorderLayout.SOUTH); add(chatResult,BorderLayout.CENTER); setBounds(10,30,400,280); 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(\
InetSocketAddress socketAddress=new InetSocketAddress(address,666); socket.connect(socketAddress);
in =new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); name=inputName.getText(); out.writeUTF(\姓名:\ send.setEnabled(true); if(!(thread.isAlive())) thread=new Thread(this); thread.start(); } }
catch (IOException ee){} }
if(e.getSource()==send)
{ String s=inputContent.getText(); if(s!=null)
{ try { out.writeUTF(\聊天内容:\ }
catch(IOException e1){} }
} }
public void run() { String s=null; while(true)
{ try{ s=in.readUTF();
chatResult.append(\ }
catch(IOException e)
{ chatResult.setText(\与服务器已断开\ try { socket.close(); }
catch(Exception exp) {} break; } } } }
服务器端ChatServer.java
import java.io.*; import java.net.*; import java.util.*; public class ChatServer
{ public static void main(String args[]) { ServerSocket server=null; Socket you=null;
Hashtable peopleList; peopleList=new Hashtable(); while(true)
{ try { server=new ServerSocket(666); }
catch(IOException e1)
{ System.out.println(\正在监听\ }
try { you=server.accept(); InetAddress address=you.getInetAddress(); System.out.println(\客户的IP:\ }
catch (IOException e) {} if(you!=null)
{ Server_thread peopleThread=new Server_thread(you,peopleList); peopleThread.start(); }
else { continue; } } } }
class Server_thread extends Thread { String name=null; Socket socket=null; File file=null;
DataOutputStream out=null; DataInputStream in=null; Hashtable peopleList=null;
Server_thread(Socket t,Hashtable list) { peopleList=list; socket=t;
try { in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); }
catch (IOException e) {} }
public void run() { while(true) { String s=null; try{
s=in.readUTF(); if(s.startsWith(\姓名:\ { name=s;
boolean boo=peopleList.containsKey(name); if(boo==false)
{ peopleList.put(name,this); } else
{ out.writeUTF(\请换妮称:\ socket.close();