1. 程式人生 > >利用java語言寫一個伺服器

利用java語言寫一個伺服器

import java.net.*;
import java.io.*;

public class Server{
    public static void main(String []args) throws Exception{
        ServerSocket server = new ServerSocket(9999);            //設定埠
        Socket sock = server.accept();
        
        FileInputStream in = new FileInputStream("D:\\server\\1.html");
        OutputStream out = sock.getOutputStream();
        
        int len = 0;
        byte buffer[] = new byte[1024];
        while((len = in.read(buffer)) > 0){
            out.write(buffer,0,len);
        }
        
        in.close();
        out.close();
        sock.close();
        server.close();
    }
}