`

j2se-10.14-log

阅读更多

2006.10.14          晴
      HTTP服务器,利用了线程可以实现多人的访问,代码是COPY过来了,可还是一知半解,另外还有UDP编程,和多播编程.其中主要用到了DatagramSocket,DatagramPacket, MulticastSocket ,InetAddress等类.

下面是今天的代码:
1.http服务器

public class HttpServer {

 public void startServer() {
  ServerSocket ss;
  try {
   ss = new ServerSocket(81);// 创建一个Server套接字并绑定端口
   while (true) {
    Socket socket = ss.accept();//侦听并接受到此套接字的连接,并返回新套接字
    // 启动谈话线程
    HttpSession session = new HttpSession(socket);
    Thread thread = new Thread(session);
    thread.start();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  HttpServer server = new HttpServer();
  server.startServer();
 }

}

public class HttpSession implements Runnable {
 public static final String PATH_NAME = "F:\\网站资料\\" ;

 private Socket socket;
 public HttpSession(Socket socket){
  this.socket = socket ;
 }
 
 public void run() {
  try {
   BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   OutputStream out = socket.getOutputStream();

   String line = "";
   while( (line = reader.readLine()) != null){
    if(line.length() <= 4) continue;
    
    int start = line.indexOf(" ");
    int end = line.lastIndexOf(" ");
    
    if(start <= 0 || end <= 0 || start >= end)
     continue ;
    
    String cmd = line.substring(0, start).toUpperCase();
    if(cmd != null && cmd.equals("GET")){
     String fileName = line.substring(start + 2, end);
     String version = line.substring(end + 1);
     doGet(out, fileName) ;
    }    
    System.out.println(line);
   }   
   out.close();
   reader.close();
   socket.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private void printOkResponse(OutputStream out, File file){
  PrintWriter pw = new PrintWriter(out) ;
  pw.println("HTTP/1.1 200 OK");
  pw.println("Server: Apache-Coyote/1.1");
  pw.println("ETag: W/\"6381-1144994988000\"");
  pw.println("Last-Modified: Fri, 14 Apr 2006 06:09:48 GMT");
  pw.println("Content-Type: text/plain");
  pw.println("Content-Length: " + file.length());

  DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
    DateFormat.FULL);
  pw.println(df.format(new Date()));

  pw.println("Connection: keep-alive");

  pw.println("");
  pw.flush();
 }
 
 private void doGet(OutputStream out, String fileName){
  File file = new File(PATH_NAME + fileName);
  printOkResponse(out, file) ;
  
  InputStream fis;
  try {
   fis = new FileInputStream(file);
   byte[] buff = new byte[1024];
   int len = 0;
   while((len = fis.read(buff) ) != -1){
    out.write(buff, 0, len);
   }
   fis.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
 
}


2.UDP编程:
public class UDPClient {
 public void uDPReceive() {
  try {
   DatagramSocket socket = new DatagramSocket(8989);//声明本机的一个指定端口的UDP数据接收包
   while (true) {
    byte buffer[] = new byte[256];
    DatagramPacket packet = new DatagramPacket(buffer,
      buffer.length);//
    socket.receive(packet);
    System.out.write(packet.getData(), 0, packet.getData().length);
    System.out.println();
    Thread.sleep(500);
   }
  } catch (SocketException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Throwable e) {
   e.printStackTrace();
  }

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  UDPClient udpclient = new UDPClient();
  udpclient.uDPReceive();
 }

}


public class UDPServer {
 public void uDPSend() {
  try {
   DatagramSocket socket = new DatagramSocket();// 声明一个UDP用途的套接字
   while (true) {
    byte buffer[] = new byte[256];
    buffer = new Date().toString().getBytes();// 得到本地时间
    InetAddress address = InetAddress.getLocalHost();// 得到本机的address
    DatagramPacket packet = new DatagramPacket(buffer,
      buffer.length, address, 8989);// 得到带有目的IP的UDP数据包
    socket.send(packet);// 发送UDP数据包

   }
  } catch (SocketException e) {
   e.printStackTrace();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  UDPServer udpserver = new UDPServer();
  udpserver.uDPSend();
 }

}



3.多播编程:
public class UDPClient {
 public void uDPReceive() {
  try {
   MulticastSocket  socket = new MulticastSocket(6789);// 创建多播套接字并将其绑定到特定端口
   InetAddress group=InetAddress.getByName("228.5.6.7");//创建一个多播组
   socket.joinGroup(group);//加入多播组
   while (true) {
    byte buffer[] = new byte[256];
    DatagramPacket packet = new DatagramPacket(buffer,
      buffer.length);// DatagramPacket主要是负责DatagramSocket的发送和接收
    socket.receive(packet);
    System.out.write(packet.getData(), 0, packet.getData().length);
    System.out.println();
    Thread.sleep(500);
   }
  } catch (SocketException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Throwable e) {
   e.printStackTrace();
  }

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  UDPClient udpclient = new UDPClient();
  udpclient.uDPReceive();
 }

}


public class UDPServer {
 public void uDPSend() {
  try {
   MulticastSocket  socket = new MulticastSocket(6789);// 创建多播套接字并将其绑定到特定端口
   InetAddress group=InetAddress.getByName("228.5.6.7");//创建一个多播组
   socket.joinGroup(group);//加入多播组
   while (true) {
    byte buffer[] = new byte[256];
    buffer = new Date().toString().getBytes();// 得到本地时间
    DatagramPacket packet = new DatagramPacket(buffer,
      buffer.length,group,6789);// 创建带有多播组的UDP数据包
    socket.send(packet);// 发送UDP多播数据包
    Thread.sleep(500);

   }
  } catch (SocketException e) {
   e.printStackTrace();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Throwable e) {
   e.printStackTrace();
  }

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  UDPServer udpserver = new UDPServer();
  udpserver.uDPSend();
 }

}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics