手动写的小代码,测试tomcat的案例
package com.gyarmy.test1; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyServer { public static void main(String[] args) throws Exception{ //第一次访问 ServerSocket server = new ServerSocket(30001); while(true) { Socket socket = server.accept(); new Thread(new myThread(socket)).start(); } } } class myThread implements Runnable{ private Socket socket; public myThread(Socket sock){ this.socket = sock; } @Override public void run() { // TODO Auto-generated method stub try { FileInputStream is = new FileInputStream(new File("c:\\1.txt")); OutputStream os = socket.getOutputStream(); byte[] b = new byte[20]; int len = 0; while((len = is.read(b))!=-1) { os.write(b,0,len); System.out.println(len); } os.close(); is.close(); socket.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
访问地址:
http://192.168.1.104:30001/
0则评论给“简单服务器代码测试”