首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

一个简单Java服务器端程序

2009-01-17 
一个简单Java服务器端程序

    代码如下:
  /**
  * This is a test program for Echo Server. @author Hu Dennis created at 2008-11-12
  */
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintStream;
  public class HttpServer {
  public static void main(String[] args) {
  int listenPort = 8080;
  ServerSocket serverSocket = null;
  Socket serviceSocket = null;
  DataInputStream inBound = null;
  PrintStream outBound = null;
  try {
  serverSocket = new ServerSocket(listenPort);
  serviceSocket = serverSocket.accept();
  inBound = new DataInputStream(serviceSocket.getInputStream());
  outBound = new PrintStream(serviceSocket.getOutputStream());
  BufferedReader bfReader = new BufferedReader(new InputStreamReader(inBound));
  String line = null;
  while (true){
  /* The request must contains a line separator.
  * eg.
  * StringBuilder request = new StringBuilder();
  * request.append("Echo\n");
  */
  line = bfReader.readLine();
  outBound.println(line);
  }
  }catch (IOException e){
  System.out.println(e);
  return ;
  }
  }
  }  
 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行