JAVA写服务器程序代码

web服务器获取一个用户的连接时,会初始化一个线程和用户通信,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
 
 
//每有一个连接建立时,服务器分出一个通信的线程
public class CommunicateThread extends Thread{
     //与客户端通信的套接字
     Socket client;
     
     public CommunicateThread(Socket s) {
         client = s;
     }
     
     //获取浏览器请求资源的路径
     public String getResourcePath(String s){
         // 一般的HTTP请求报文的第一行是“GET /index.html HTTP/1.1”
         // 我们要获取的就是中间的"/indext.apsx"
         
         //获取资源的位置
         String s1 = s.substring(s.indexOf( ' ' )+ 1 );
         s1 = s1.substring( 1 ,s1.indexOf( ' ' ));
         
         //默认资源为index.html
         if (s1.equals( "" ))
             s1 = "index.html" ;
         
         return s1;
     }
 
     public void sendFile(PrintStream out,File file){
         try {
             DataInputStream in  = new DataInputStream( new FileInputStream(file));
             int len = ( int )file.length();
             byte buf[] = new byte [len];
             in.readFully(buf); //读取文内容到buf数组中
             out.write(buf, 0 ,len);
             out.flush();
             in.close();
         }
         catch (Exception e){
             System.out.println(e.getMessage());
             System.exit( 1 );
             }
     }
     
     public void run(){
         try {
             //获取用户的IP地址和端口号
             String clientIP = client.getInetAddress().toString();
             int clientPort = client.getPort();
             //创建输出流对象
             PrintStream out = new PrintStream(client.getOutputStream());
             //创建输入流对象
             DataInputStream in = new DataInputStream(client.getInputStream());
             //读取浏览器提交的请求
             String msg = in.readLine();
             
             
             //获取文件路径
             String fileName = getResourcePath(msg);
             System.out.println( "The user asked for resource: " +fileName);
             File file = new File(fileName);
             
             if (file.exists()){
                 //根据响应报文格式设置
                 System.out.println(fileName+ " start send" );
                 
                 out.println( "HTTP/1.0 200 OK" );
                 out.println( "MIME_version:1.0" );
                 out.println( "Content_Type:text/html" );
                 int len = ( int ) file.length();
                 out.println( "Content_Length:" +len);
                 out.println( "" ); //报文头和信息之间要空一行
                 
                 //发送文件
                 sendFile(out,file);
                 
                 out.flush();
             }  
             client.close();    
         }
         catch (Exception e){
             System.out.println(e.getMessage());
         }      
     }
     
 
 
}

服务器主要负责初始化套接字和线程,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.net.ServerSocket;
import java.net.Socket;
 
 
public class WebServer {
 
 
     public static void main(String[] args) {
         int Port = 12345 ; //端口号,由于这里是测试,所以不要使用常用端口
         //创建两个套接字
         ServerSocket server = null ;
         Socket client = null ;
         try {
             server = new ServerSocket(Port);
             //服务器开始监听
             System.out.println( "The WebServer is listening on port " +server.getLocalPort());
             while ( true ){
                 client = server.accept();
                 //多线程运行
                 new CommunicateThread(client).start();
             }
         } catch (Exception e){
             System.out.println(e.getMessage());
         }
     }
 
}


运行测试:

编写一个index.html文件

?
1
<h1>This is the index of my WebServer</h1><hr>
放到项目文件的根目录,然后在浏览器地址栏输入:“localhost:12345/index.html”,就可以看到位于服务器端的html文件了。注意由于服务器是死循环,重启服务器会发现指定的端口已被绑定,只需要进入任务管理器,关闭"Java(TM) Platfrom SE binary"进程即可。最后结果如下所示:
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值