看一下这个我写的:/*this applet is used to request and receive the updated price information *from the server side and display it in the applet window. The applet display *the information using an animation. */import javax.swing.JApplet;import java.applet.*; import java.awt.*; import java.awt.image.BufferedImage;import java.net.*; import java.io.*; import javax.swing.JOptionPane;import java.text.DecimalFormat; public class Client extends JApplet implements Runnable { protected Socket socket; //socket connecting the server side protected DataInputStream input; protected DataOutputStream output; //horizontal position on the applet canvas where the string is displayed protected int horiPos; private Image bufImage; //buffered image for offscreen drawing private Graphics graphic; protected Thread thread; //thread object //protected String[] stockNames = {"MSFT", "HSBC", "DELL", "IBM", "INTC", "SUN"}; //protected String[] stockPrices = {"27.5", "82.3", "25.2", "85.5", "18.8", "5.7"}; protected String[] stockNames = new String[6]; protected String[] stockPrices = new String[6]; private int width; //width of the canvas private int height; //height of the canvas private int currentStringLength; protected FontMetrics metrics; protected Font nameFont, priceFont; //fonts for different strings //constructor public Client() { //get the values of canvas of this applet width = 350; height = 150; horiPos = width;
nameFont = new Font("Helvetica", Font.BOLD, 20); priceFont = new Font("Helvetica", Font.BOLD, 12); } //this method wait until the display is finished and then get the //image resource of the display based on the finished screen show public void addNotify() { super.addNotify(); bufImage = this.createImage(width, height); graphic = bufImage.getGraphics(); metrics = graphic.getFontMetrics(); } //init method public void init() { //get the server host from the paramters in html file String host = getParameter("Host"); if (host == null) { host = "127.0.0.1"; } try { //set the socket connection to the server socket = new Socket(host, 9999); //get the dataInputStream and outputstream from the connection input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (UnknownHostException uhe) { System.err.println("Unknown Host Error!"); } catch (SocketException se) { System.err.println("Socket Error!"); } catch (IOException ioe) { System.err.println("IO Error!"); }
//create a thread using this object thread = new Thread(this); thread.start(); } //run method public void run() { //first time for updating stock information try{ updateStockInformation(); }catch(IOException ioe){ System.err.println("IOException"); ioe.printStackTrace(); }
while(true) { repaint(); try{ Thread.currentThread().sleep(100L); }catch(InterruptedException ie){ System.err.println("Interrupted Exception During the Sleep Method"); }
//the displayed string moves leftward by 4 pixles for each loop round horiPos -= 4;
//if the displayed string has reached the left side edge //then the program should wait for a while and at the same //update the stock price information downloaded from the server if(horiPos + currentStringLength < 0){ try{ Thread.currentThread().sleep(400L); horiPos = width; updateStockInformation(); }catch(InterruptedException ie){ System.err.println("Interrupted Exception during waiting"); }catch(IOException e){ System.err.println("IO Exception during the updating process"); } } } } //this method gets the stock names and prices from the server from the inputstream public void updateStockInformation() throws IOException { //send the request for updated price info output.writeBoolean(true); output.flush(); for(int i=0; i<stockNames.length; i++) { stockNames[i] = input.readUTF(); //format mask the price info stockPrices[i] = input.readUTF(); } }
//update method overridden public void update(Graphics g) { //call for resizing function when the size of the canvas is changed resizing(); clearCanvas(graphic); paint(graphic); g.drawImage(bufImage, 0, 0, this); } //paint method overridden public void paint(Graphics g) { drawString(g); } //clear the display of the canvas protected void clearCanvas(Graphics g) { g.clearRect(0, 0, width, height); }
//if the applet window is resized then we need to update the size information public void resizing() { Dimension size = this.getSize(); if(size.getWidth() > 0 && size.getHeight() > 0) { width = (int)size.getWidth(); height = (int)size.getHeight(); }
//create the new iamge resources based on the new screen resources graphic.dispose(); bufImage = this.createImage(width, height); graphic = bufImage.getGraphics(); } //draw method that is used to draw the graphics on the offscreen image protected void drawString(Graphics g) { //the length of the string int stringLength = 0; int temp; for(int i=0; i<stockNames.length; i++) { //draw stock names g.setColor(Color.blue); g.setFont(nameFont); g.drawString(stockNames[i], (horiPos + stringLength), height/2-5);
服务器端/*This class is server-side listener and deal with the incoming connections *sending back the reply to the client containing updated stock prices. */import java.net.ServerSocket; import java.net.Socket;import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.IOException;import java.text.DecimalFormat;public class Server { protected Socket socket; protected DecimalFormat format; //constructor public Server(Socket socket) { this.socket = socket; this.format = new DecimalFormat("###0.0#"); } //override the run method of the thread public void start() { DataOutputStream output = null; DataInputStream input = null; try{ //get the io streams from the socket output = new DataOutputStream(socket.getOutputStream()); input = new DataInputStream(socket.getInputStream()); boolean request = false; try { do { System.out.println(); //wait for the request for updated price info from client side request = input.readBoolean();
//send the updated stock price info if (request == true) { String[] names = Stock.getNames(); double[] prices = Stock.getPrices(); String temp; //send the updated info via the output stream for (int i = 0; i < names.length; i++) { output.writeUTF(names[i]); System.out.print(names[i] + " "); temp = format.format(prices[i]); output.writeUTF(temp); System.out.println(temp); } output.flush(); } } while (true); } catch (IOException ioe) { System.err.println("Could not send price info"); } //finally block to ensure that the iostreams and socket are closed finally{ input.close(); output.close(); socket.close(); System.err.println(); System.err.println("Client side disconnected. Server stopped."); } }catch(IOException ioe){ System.err.println("Failure in intializing IO stream"); } } //main method public static void main(String[] args) { ServerSocket server = null; Socket socket; try{ //create the server socket listening at port 9999 server = new ServerSocket(9999); System.err.println("\nServer started..."); }catch(IOException ioe){ System.err.println("Failure in server socket initialization"); } try{ if(server != null) { socket = server.accept(); //block and wait for the incoming connection System.err.println("Incoming connection from " + socket.getInetAddress() + " at " + 9999 + " accepted!"); //deal with the connection incoming new Server(socket).start(); } }catch(IOException ioe){ System.err.println("Failure in accepting incoming connection"); } } }
服务器Sockets服务器并不是主动地建立连接.相反地,他们是被动地监听一个客户端的连接请示然后给他们服务.服务器是由类ServerSocket来建立的.下面的程序建立了一个服务器端socket并把它绑定到80端口:ServerSocket serverSocket = new ServerSocket(80, 5); 第一个参数是服务器要监听的端口.第二个参数是可选的.API文档中说明了这是一个监听时间,但是在传统的socket程序中第二个参数是监听深度.一个服务器可以同时接收多个连接请求,但是每次只能处理一个.监听堆是一个无回答的连接请求队列.上面的请求建立一个连接来处理最后五个请求.如果省略了后面的一个参数,则默认值是50.ServerSocket serverSocket = new ServerSocket(80, 5); 一旦socket建立了并开始监听连接,进来的连接将会建立并放在监听堆.accetp()方法把在堆中的连接取出来.Socket clientSocket = serverSocket.accept(); 这个方法返回一个用来与来访者对话的客户端连接.服务器本身不可能建立对话,相反地,服务器socket会使用accept()方法来产生一个新的socket.服务器socket依旧打开并排列新的连接请求.与客户端socket一样,下面的一步建立输入和输出流:DataInputStream inbound = new DataInputStream( clientSocket.getInputStream() ); DataOutputStream outbound = new DataOutputStream( clientSocket.getOutputStream() ); 一般的I/O操作可以在新建的流中运用.在服务器回应前它等待客户端发送一个空白的行.当会话结束时,服务器关闭流和客户端socket.如果在队列中没有请示将会出现什么情况呢?那个方法将会等待一个的到来.这个行为叫阻塞.accept()方法将会阻塞服务器线程直到一个呼叫到来.当5个连接处理完闭之后,服务器退出.任何的在队列中的呼叫将会被取消. 所有的服务器都要有以下的基本的步骤:1.建立一个服务器socket并开始监听.2.使用accept()方法取得新的连接. 3.建立输入和输出流.4.在已有的协议上产生会话. 5.关闭客户端流和socket.6.回到第二步或者到第七步.7.关闭服务器socket.
重复和并发服务器 所有的这些调用都可以掷出一个UnknownHostException违例.如果一台计算机没有连接上DNS服务器,或者主机的确没有找到,这个违例就会被掷出.如果一台计算机没有一个激活的TCP/IP配置,getLocalHost()也为失败并掷出一个违例. 一旦一个地址被确定了,数据报就可以被送出了.下面的程序传输了一个字符串给目的socket: String toSend = "This is the data to send!"); byte[] sendbuf = new byte[ toSend.length() ]; toSend.getBytes( 0, toSend.length(), sendbuf, 0 ); DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length, addr, port); clientSocket.send( sendPacket ); 首先,字符串必须被转换成一个字节数组.然后,一个新的DatagramPacket实例必须被建立.注意构建器的最后两个参数.因为要发送一个包,所以地址和端口必须被给定.一个applet可能可以知道它的服务器的地址,但是服务器如何知道它的客户机的地址呢.当任何一个包被收到后,返回的地址和端口会被解压出来,并通过getAddress()和getPort()方法得到.这就是一个服务器如何回应一个客户端的包: DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length, recvPacket.getAddress(), recvPacket.getPort() ); serverSocket.send( sendPacket ); 不像面向连接的操作,数据报服务器服务器其实比数据报客户端更简单: 数据报服务器一个数据报服务器的基本步骤: 1.在一个指定的端口上建立一个数据报socket. 2.用receive方法等待进来的包.3.用特定的协议来回应收到的包.4.回到第二步或继续第二步.5.关闭数据报socket.列表9.3演示了一人简单的数据报回应服务器.它将回应它收到的包. 列表9.3.一个简单的数据报回应服务器 import java.io.*; import java.net.*; public class SimpleDatagramServer { public static void main(String[] args) { DatagramSocket socket = null; DatagramPacket recvPacket, sendPacket; try { socket = new DatagramSocket(4545); while (socket != null) { recvPacket= new DatagramPacket(new byte[512], 512); socket.receive(recvPacket); sendPacket = new DatagramPacket( recvPacket.getData(), recvPacket.getLength(), recvPacket.getAddress(), recvPacket.getPort() ); socket.send( sendPacket ); } } catch (SocketException se) { System.out.println("Error in SimpleDatagramServer: " + se); } catch (IOException ioe) { System.out.println("Error in SimpleDatagramServer: " + ioe); 这个应用程序被当作一个重复的服务器.因为它只有在处理完一个进程以后才会接受另一个连接.更多的复杂服务器是并发的.它为每一个请求分配一个线程,而不是来一个处理一个.所以看起来它在同时处理多人请求.所有的商业的服务器都是并发的服务器. Java数据报类不像面向连接的类,数据报的客户端和服务器端的类在表面上是一样的.下面的程序建立了一个客户和服务器商的数据报sockets:DatagramSocket serverSocket = new DatagramSocket( 4545 ); DatagramSocket clientSocket = new DatagramSocket(); 服务器用参数4545来指定端口号,由于客户端将要呼叫服务器,客户端可以利用可利用的端口.如果省略第二个参数,程序会让操作系统分配一个可用的端口.客户端可以请求一个指定的端口,但是如果其它的应用程序已经绑定到这个端口之上,请求将会失败.如果你的意图不是作为一个服务器,最好不要指定端口.由于流不能由交谈得到,那么我么如何与一个数据报Socket进行对话.答案在于数据报类. 接收数据报DatagramPacket类是用来通过DatagramSocket类接收和发送数据的类.packet类包括了连接信息和数据.就如前面所说的一样,数据报是自身独立的传输单元.DatagramPacket类压缩了这些单元.下面的程序表示了用一个数据报socket来接收数据:DatagramPacket packet = new DatagramPacket(new byte[512], 512); clientSocket.receive(packet); clientSocket.receive(packet); packet的构建器需要知道将得到的数据放在哪儿.一个512字节的缓存被建立并且作为构建器的第二个参数.每二个构建器参数是缓存的大小.就像ServerSocket类的accept()方法一样,receive()方法在数据可用之前将会阻塞.发送数据报发送数据报是非常地简单地,所有需要的只是一个地址.地址是由InetAddress类来建立的.这个类没有公共的构建器,但是它有几个static的方法,可以用来建立这个类的实例.下面的列表列出了建立InetAddress类的实例的方法: Public InetAddress Creation Methods InetAddress getByName(String host); InetAddress[] getAllByName(String host); InetAddress getLocalHost(); 得到本地主机的地址是非常地有用的,只有前面两个方法是用来发送数据包的.getByName()和getAllByName()需要目的主机的地址.第一个方法仅仅只是返回第一个符合条件的东西.第二个方法是必须的,因为一台计算机可能有多个地址.在这种情况下,这台计算机被称为multi-homed.所有的建立的方法都被标记为static.它们必须像下面这样得到调用: InetAddress addr1 = InetAddress.getByName("merlin"); InetAddress addr2[] = InetAddress.getAllByName("merlin"); InetAddress addr3 = InetAddress.getLocalHost();
需要的stock类但是没必要了解/*this class is used to calculate the stock price according to the model given *by the problem. The price info will be updated each round. */ public class Stock { //the possibilities of changes for different stocks private static double [][] possibility = {{0, 0.60, 0.85, 0.95, 1.00}, {0, 0.50, 0.75, 0.90, 1.00}, {0, 0.40, 0.65, 0.85, 1.00}, {0, 0.30, 0.60, 0.85, 1.00}}; //percentage change compared with the original price private static double [] changes = {0, 0.01, 0.03, 0.05}; //betas of these stocks private static double [] betas = {1.0, 1.3, 1.3, 1.6, 2.0, 2.0}; private static double [] prices = {27.50, 82.30, 25.20, 85.50, 18.80, 5.70}; //prices static String [] stockNames = {"MSFT", "HSBC", "DELL", "IBM", "INTC", "SUN"}; //stock names
//update the prices of all the given stocks protected static void updatePrices() { for(int i=0; i<prices.length; i++) { prices[i] = calculatePrice(prices[i], betas[i]); } }
//getter method for stock prices public static double [] getPrices() { double [] temp = prices; updatePrices(); return temp; }
//getter method for stock names public static String [] getNames() { return stockNames; }
//this static method is used to calculate the forecast stock price //using the input arguments of original price and beta of the stock protected static double calculatePrice(double originalPrice, double beta) { double percentage = (Math.abs(Math.random()-0.5))*2; double direction = Math.random()-0.5; double changePer = 0;
int i; if(beta == 1.0) { i = 0; } else if(beta == 1.3) { i = 1; } else if(beta == 1.6) { i = 2; } else if(beta == 2.0) { i = 3; } else { i = -1; }
//find the interval in which the possibility falls if(i != -1){ for(int p=0; p < (possibility[i].length)-1; p++){ if(percentage >= possibility[i][p] && percentage < possibility[i][p+1]){ changePer = changes[p]; } } }
//calculate and return the new price if(direction >= 0){ return originalPrice*(1+changePer); }else { return originalPrice*(1-changePer); } }//end of calculatePrice method }
简单的WEB服务器一个简单的WEB服务器将由列表9.2这样构建.当然,还必须要对方法和回应事件进行改进.简单的服务器不会分析和存储请求头.新的WEB服务器将分析和存储请求,为以后的处理作准备.为了达到这个目的,你必须有一个包含HTTP请求的类.HTTPrequest类列表9.5列出了一个完整的HTTPrequest类.这个类必须包括一个请求头所需的所有信息. 列表9.5.HTTPrequest类. import java.io.*; import java.util.*; import java.net.*; import NameValue; /** * 这个类有一个HTTP请求的所有信息 */ public class HTTPrequest { public String version; public String method; public String file; public Socket clientSocket; public DataInputStream inbound; public NameValue headerpairs[]; /** * 建立一个这个类的实例 */ public HTTPrequest() { version = null; method = null; file = null; clientSocket = null; inbound = null; inbound = null; headerpairs = new NameValue[0]; } /** * 加入一个名称/值对到核心数组 */ public void addNameValue(String name, String value) { try { NameValue temp[] = new NameValue[ headerpairs.length + 1 ]; System.arraycopy(headerpairs, 0, temp, 0, headerpairs.length); temp[ headerpairs.length ] = new NameValue(name, value); headerpairs = temp; } catch (NullPointerException npe) { System.out.println("NullPointerException while adding name-value: " + npe); } } /** * 以字符串的形式归还这个类 */ public String toString() { String s = method + " " + file + " " + version + "\n"; for (int x = 0; x < headerpairs.length; x++ ) s += headerpairs[x] + "\n"; return s; } } NameValue类简单地存储了两个字符串:name 和 value.当一个新的对要被加入时,一个新的数组将被分配.新的数组接受了旧的数组和新的成员.旧的数组然后被一个新建的对象覆盖了.
try{
clientSentence = new String(inFromClient.readLine());
outToClient.writeBytes("OK\r\n");
}
catch(IOException ex){
}其中clientSentence 是String,
inFromClient =new BufferedReader(new InputStreamReader(connectedSocket.getInputStream()));
outToClient = new DataOutputStream(connectedSocket.getOutputStream());connectedSocket是当前的连接
*from the server side and display it in the applet window. The applet display
*the information using an animation.
*/import javax.swing.JApplet;import java.applet.*;
import java.awt.*;
import java.awt.image.BufferedImage;import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;import java.text.DecimalFormat;
public class Client extends JApplet implements Runnable
{
protected Socket socket; //socket connecting the server side
protected DataInputStream input;
protected DataOutputStream output;
//horizontal position on the applet canvas where the string is displayed
protected int horiPos; private Image bufImage; //buffered image for offscreen drawing
private Graphics graphic;
protected Thread thread; //thread object
//protected String[] stockNames = {"MSFT", "HSBC", "DELL", "IBM", "INTC", "SUN"};
//protected String[] stockPrices = {"27.5", "82.3", "25.2", "85.5", "18.8", "5.7"};
protected String[] stockNames = new String[6];
protected String[] stockPrices = new String[6];
private int width; //width of the canvas
private int height; //height of the canvas
private int currentStringLength; protected FontMetrics metrics;
protected Font nameFont, priceFont; //fonts for different strings
//constructor
public Client()
{
//get the values of canvas of this applet
width = 350;
height = 150;
horiPos = width;
nameFont = new Font("Helvetica", Font.BOLD, 20);
priceFont = new Font("Helvetica", Font.BOLD, 12);
} //this method wait until the display is finished and then get the
//image resource of the display based on the finished screen show
public void addNotify()
{
super.addNotify();
bufImage = this.createImage(width, height);
graphic = bufImage.getGraphics();
metrics = graphic.getFontMetrics();
}
//init method
public void init()
{
//get the server host from the paramters in html file
String host = getParameter("Host");
if (host == null) {
host = "127.0.0.1";
} try {
//set the socket connection to the server
socket = new Socket(host, 9999);
//get the dataInputStream and outputstream from the connection
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
}
catch (UnknownHostException uhe) {
System.err.println("Unknown Host Error!");
}
catch (SocketException se) {
System.err.println("Socket Error!");
}
catch (IOException ioe) {
System.err.println("IO Error!");
}
//create a thread using this object
thread = new Thread(this);
thread.start();
} //run method
public void run()
{
//first time for updating stock information
try{
updateStockInformation();
}catch(IOException ioe){
System.err.println("IOException");
ioe.printStackTrace();
}
while(true)
{
repaint(); try{
Thread.currentThread().sleep(100L);
}catch(InterruptedException ie){
System.err.println("Interrupted Exception During the Sleep Method");
}
//the displayed string moves leftward by 4 pixles for each loop round
horiPos -= 4;
//if the displayed string has reached the left side edge
//then the program should wait for a while and at the same
//update the stock price information downloaded from the server
if(horiPos + currentStringLength < 0){
try{
Thread.currentThread().sleep(400L);
horiPos = width;
updateStockInformation();
}catch(InterruptedException ie){
System.err.println("Interrupted Exception during waiting");
}catch(IOException e){
System.err.println("IO Exception during the updating process");
}
}
}
}
//this method gets the stock names and prices from the server from the inputstream
public void updateStockInformation() throws IOException
{
//send the request for updated price info
output.writeBoolean(true);
output.flush(); for(int i=0; i<stockNames.length; i++)
{
stockNames[i] = input.readUTF();
//format mask the price info
stockPrices[i] = input.readUTF();
}
}
//update method overridden
public void update(Graphics g)
{
//call for resizing function when the size of the canvas is changed
resizing();
clearCanvas(graphic);
paint(graphic);
g.drawImage(bufImage, 0, 0, this);
} //paint method overridden
public void paint(Graphics g)
{
drawString(g);
} //clear the display of the canvas
protected void clearCanvas(Graphics g)
{
g.clearRect(0, 0, width, height);
}
//if the applet window is resized then we need to update the size information
public void resizing()
{
Dimension size = this.getSize();
if(size.getWidth() > 0 && size.getHeight() > 0)
{
width = (int)size.getWidth();
height = (int)size.getHeight();
}
//create the new iamge resources based on the new screen resources
graphic.dispose();
bufImage = this.createImage(width, height);
graphic = bufImage.getGraphics();
}
//draw method that is used to draw the graphics on the offscreen image
protected void drawString(Graphics g)
{
//the length of the string
int stringLength = 0;
int temp; for(int i=0; i<stockNames.length; i++)
{
//draw stock names
g.setColor(Color.blue);
g.setFont(nameFont);
g.drawString(stockNames[i], (horiPos + stringLength), height/2-5);
metrics = g.getFontMetrics();
temp = metrics.stringWidth(stockNames[i]);
stringLength = stringLength + temp + 1; //draw stock prices
g.setColor(Color.red);
g.setFont(priceFont);
g.drawString(stockPrices[i], (horiPos + stringLength), (height/2-28)+metrics.getHeight()); metrics = g.getFontMetrics();
temp = metrics.stringWidth(stockPrices[i]);
stringLength = stringLength + temp + 25;
} currentStringLength = stringLength - 32;
}}
*sending back the reply to the client containing updated stock prices.
*/import java.net.ServerSocket;
import java.net.Socket;import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.IOException;import java.text.DecimalFormat;public class Server
{
protected Socket socket;
protected DecimalFormat format;
//constructor
public Server(Socket socket) {
this.socket = socket;
this.format = new DecimalFormat("###0.0#");
}
//override the run method of the thread
public void start() {
DataOutputStream output = null;
DataInputStream input = null; try{
//get the io streams from the socket
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(socket.getInputStream()); boolean request = false; try {
do {
System.out.println(); //wait for the request for updated price info from client side
request = input.readBoolean();
//send the updated stock price info
if (request == true) {
String[] names = Stock.getNames();
double[] prices = Stock.getPrices();
String temp;
//send the updated info via the output stream
for (int i = 0; i < names.length; i++) {
output.writeUTF(names[i]);
System.out.print(names[i] + " ");
temp = format.format(prices[i]);
output.writeUTF(temp);
System.out.println(temp);
}
output.flush();
}
} while (true);
}
catch (IOException ioe) {
System.err.println("Could not send price info");
}
//finally block to ensure that the iostreams and socket are closed
finally{
input.close();
output.close();
socket.close();
System.err.println();
System.err.println("Client side disconnected. Server stopped.");
}
}catch(IOException ioe){
System.err.println("Failure in intializing IO stream");
}
}
//main method
public static void main(String[] args)
{
ServerSocket server = null;
Socket socket; try{
//create the server socket listening at port 9999
server = new ServerSocket(9999);
System.err.println("\nServer started...");
}catch(IOException ioe){
System.err.println("Failure in server socket initialization");
} try{
if(server != null)
{
socket = server.accept(); //block and wait for the incoming connection
System.err.println("Incoming connection from "
+ socket.getInetAddress() + " at "
+ 9999 + " accepted!");
//deal with the connection incoming
new Server(socket).start();
}
}catch(IOException ioe){
System.err.println("Failure in accepting incoming connection");
}
}
}
* 一个监听端口并提供HTML文档的程序.
*/
class SimpleWebServer {
public static void main(String args[])
{
ServerSocket serverSocket = null;
Socket clientSocket = null;
int connects = 0;
try
{
{
// 建立一个服务器socket
serverSocket = new ServerSocket(80, 5);
while (connects < 5)
{
// 等待连接
clientSocket = serverSocket.accept();
//服务连接
ServiceClient(clientSocket);
connects++;
}
serverSocket.close();
}
catch (IOException ioe)
{
System.out.println("Error in SimpleWebServer: " + ioe);
}
}
public static void ServiceClient(Socket client)
throws IOException
{
DataInputStream inbound = null;
DataOutputStream outbound = null;
try
{
// 得到IO流
inbound = new DataInputStream( client.getInputStream());
outbound = new DataOutputStream( client.getOutputStream());
//格式化输出(回应头和很少的HTML文档)
StringBuffer buffer = PrepareOutput();
String inputLine;
while ((inputLine = inbound.readLine()) != null)
{
//如果到了HTTP请求的尾部,就发送回应
if ( inputLine.equals("") )
{
outbound.writeBytes(buffer.toString());
break;
}
}
}
finally
{
// 清除
System.out.println("Cleaning up connection: " + client);
tln("Cleaning up connection: " + client);
outbound.close();
inbound.close();
client.close();
client.close();
}
}
byte[] sendbuf = new byte[ toSend.length() ];
toSend.getBytes( 0, toSend.length(), sendbuf, 0 );
DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length,
addr, port);
clientSocket.send( sendPacket ); 首先,字符串必须被转换成一个字节数组.然后,一个新的DatagramPacket实例必须被建立.注意构建器的最后两个参数.因为要发送一个包,所以地址和端口必须被给定.一个applet可能可以知道它的服务器的地址,但是服务器如何知道它的客户机的地址呢.当任何一个包被收到后,返回的地址和端口会被解压出来,并通过getAddress()和getPort()方法得到.这就是一个服务器如何回应一个客户端的包: DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length,
recvPacket.getAddress(), recvPacket.getPort() );
serverSocket.send( sendPacket ); 不像面向连接的操作,数据报服务器服务器其实比数据报客户端更简单: 数据报服务器一个数据报服务器的基本步骤: 1.在一个指定的端口上建立一个数据报socket. 2.用receive方法等待进来的包.3.用特定的协议来回应收到的包.4.回到第二步或继续第二步.5.关闭数据报socket.列表9.3演示了一人简单的数据报回应服务器.它将回应它收到的包. 列表9.3.一个简单的数据报回应服务器 import java.io.*;
import java.net.*;
public class SimpleDatagramServer
{
public static void main(String[] args)
{
DatagramSocket socket = null;
DatagramPacket recvPacket, sendPacket;
try
{
socket = new DatagramSocket(4545);
while (socket != null)
{
recvPacket= new DatagramPacket(new byte[512], 512);
socket.receive(recvPacket);
sendPacket = new DatagramPacket(
recvPacket.getData(), recvPacket.getLength(),
recvPacket.getAddress(), recvPacket.getPort() );
socket.send( sendPacket );
}
}
catch (SocketException se)
{
System.out.println("Error in SimpleDatagramServer: " + se);
}
catch (IOException ioe)
{
System.out.println("Error in SimpleDatagramServer: " + ioe); 这个应用程序被当作一个重复的服务器.因为它只有在处理完一个进程以后才会接受另一个连接.更多的复杂服务器是并发的.它为每一个请求分配一个线程,而不是来一个处理一个.所以看起来它在同时处理多人请求.所有的商业的服务器都是并发的服务器. Java数据报类不像面向连接的类,数据报的客户端和服务器端的类在表面上是一样的.下面的程序建立了一个客户和服务器商的数据报sockets:DatagramSocket serverSocket = new DatagramSocket( 4545 );
DatagramSocket clientSocket = new DatagramSocket(); 服务器用参数4545来指定端口号,由于客户端将要呼叫服务器,客户端可以利用可利用的端口.如果省略第二个参数,程序会让操作系统分配一个可用的端口.客户端可以请求一个指定的端口,但是如果其它的应用程序已经绑定到这个端口之上,请求将会失败.如果你的意图不是作为一个服务器,最好不要指定端口.由于流不能由交谈得到,那么我么如何与一个数据报Socket进行对话.答案在于数据报类. 接收数据报DatagramPacket类是用来通过DatagramSocket类接收和发送数据的类.packet类包括了连接信息和数据.就如前面所说的一样,数据报是自身独立的传输单元.DatagramPacket类压缩了这些单元.下面的程序表示了用一个数据报socket来接收数据:DatagramPacket packet = new DatagramPacket(new byte[512], 512); clientSocket.receive(packet);
clientSocket.receive(packet); packet的构建器需要知道将得到的数据放在哪儿.一个512字节的缓存被建立并且作为构建器的第二个参数.每二个构建器参数是缓存的大小.就像ServerSocket类的accept()方法一样,receive()方法在数据可用之前将会阻塞.发送数据报发送数据报是非常地简单地,所有需要的只是一个地址.地址是由InetAddress类来建立的.这个类没有公共的构建器,但是它有几个static的方法,可以用来建立这个类的实例.下面的列表列出了建立InetAddress类的实例的方法: Public InetAddress Creation Methods InetAddress getByName(String host);
InetAddress[] getAllByName(String host);
InetAddress getLocalHost(); 得到本地主机的地址是非常地有用的,只有前面两个方法是用来发送数据包的.getByName()和getAllByName()需要目的主机的地址.第一个方法仅仅只是返回第一个符合条件的东西.第二个方法是必须的,因为一台计算机可能有多个地址.在这种情况下,这台计算机被称为multi-homed.所有的建立的方法都被标记为static.它们必须像下面这样得到调用: InetAddress addr1 = InetAddress.getByName("merlin");
InetAddress addr2[] = InetAddress.getAllByName("merlin");
InetAddress addr3 = InetAddress.getLocalHost();
*by the problem. The price info will be updated each round.
*/ public class Stock
{
//the possibilities of changes for different stocks
private static double [][] possibility = {{0, 0.60, 0.85, 0.95, 1.00},
{0, 0.50, 0.75, 0.90, 1.00},
{0, 0.40, 0.65, 0.85, 1.00},
{0, 0.30, 0.60, 0.85, 1.00}};
//percentage change compared with the original price
private static double [] changes = {0, 0.01, 0.03, 0.05};
//betas of these stocks
private static double [] betas = {1.0, 1.3, 1.3, 1.6, 2.0, 2.0};
private static double [] prices = {27.50, 82.30, 25.20,
85.50, 18.80, 5.70}; //prices
static String [] stockNames = {"MSFT", "HSBC", "DELL",
"IBM", "INTC", "SUN"}; //stock names
//update the prices of all the given stocks
protected static void updatePrices()
{
for(int i=0; i<prices.length; i++)
{
prices[i] = calculatePrice(prices[i], betas[i]);
}
}
//getter method for stock prices
public static double [] getPrices()
{
double [] temp = prices;
updatePrices();
return temp;
}
//getter method for stock names
public static String [] getNames()
{
return stockNames;
}
//this static method is used to calculate the forecast stock price
//using the input arguments of original price and beta of the stock
protected static double calculatePrice(double originalPrice, double beta)
{
double percentage = (Math.abs(Math.random()-0.5))*2;
double direction = Math.random()-0.5;
double changePer = 0;
int i;
if(beta == 1.0) { i = 0; }
else if(beta == 1.3) { i = 1; }
else if(beta == 1.6) { i = 2; }
else if(beta == 2.0) { i = 3; }
else { i = -1; }
//find the interval in which the possibility falls
if(i != -1){
for(int p=0; p < (possibility[i].length)-1; p++){
if(percentage >= possibility[i][p] && percentage < possibility[i][p+1]){
changePer = changes[p];
}
}
}
//calculate and return the new price
if(direction >= 0){
return originalPrice*(1+changePer);
}else
{
return originalPrice*(1-changePer);
}
}//end of calculatePrice method
}
import java.util.*;
import java.net.*;
import NameValue;
/**
* 这个类有一个HTTP请求的所有信息
*/
public class HTTPrequest
{
public String version;
public String method;
public String file;
public Socket clientSocket;
public DataInputStream inbound;
public NameValue headerpairs[];
/**
* 建立一个这个类的实例
*/
public HTTPrequest()
{
version = null;
method = null;
file = null;
clientSocket = null;
inbound = null;
inbound = null;
headerpairs = new NameValue[0];
}
/**
* 加入一个名称/值对到核心数组
*/
public void addNameValue(String name, String value)
{
try
{
NameValue temp[] = new NameValue[ headerpairs.length + 1 ];
System.arraycopy(headerpairs, 0, temp, 0, headerpairs.length);
temp[ headerpairs.length ] = new NameValue(name, value);
headerpairs = temp;
}
catch (NullPointerException npe)
{
System.out.println("NullPointerException while adding name-value:
" + npe);
}
}
/**
* 以字符串的形式归还这个类
*/
public String toString()
{
String s = method + " " + file + " " + version + "\n";
for (int x = 0; x < headerpairs.length; x++ )
s += headerpairs[x] + "\n";
return s;
}
} NameValue类简单地存储了两个字符串:name 和 value.当一个新的对要被加入时,一个新的数组将被分配.新的数组接受了旧的数组和新的成员.旧的数组然后被一个新建的对象覆盖了.
收到请求后,读客户端输入信息,
向客户端发送反馈信息.