技术开发 频道

J2EE连接器开发实践之: J2EE连接器的开发

        开发简单资源层

  资源层是一个Socket服务程序,当它接收到客户端发送来的字符串时,就在这个字符串前增加"Hello:"字符串,然后返回这个新的字符串。代码如例程1所示。

  例程1 EIS资源层

1 package com.hellking.jca.eis;
2 import java.net.*;
3 import java.io.*;
4 public class EISServer
5 {
6     public static void main(String[] args)
7     {        
8       try
9       {
10         System.out.println ("启动服务....");    
11         ServerSocket s = new ServerSocket (2008);
12         // 处理客户端请求
13         while (true)
14         {
15           System.out.println ("监听客户端连接...");
16           new ServerThread(s.accept()).start();    
17            System.out.println ("接收到一个连接");            
18         }      
19     }
20     catch(Exception e)
21     {
22        e.printStackTrace(System.err);
23     }
24   }
25 }
26   
27   class ServerThread extends Thread
28   {
29       private Socket socket=null;
30       public ServerThread(Socket socket)
31       {
32           super("a new thread");
33           this.socket=socket;
34       }
35       public void run()
36       {
37            try
38            {
39                BufferedReader in = new BufferedReader
40             (new InputStreamReader (socket.getInputStream()));
41               PrintStream out = new PrintStream(socket.getOutputStream());
42               String line;
43               do
44                 {
45                      line = in.readLine();
46                     System.out.println ("接收到以下输入: " + line);
47                     if (line != null)
48                       {
49                       out.println ("Hello: "+line);
50                       }
51                 } while (line != null);
52               System.out.println ("关闭连接");
53               socket.close();
54           }
55           catch(Exception e)
56           {
57               e.printStackTrace();
58           }
59      }
60   }
61
0
相关文章