JAvaSocketとC通信


この間、web端末とCサービス端末の間の通信を書きました.
 
ただし、短い接続で詰まっていない方式で、長い接続を使い、tomcatを起動させるときにCサービス側と通信しようとしたが、方法が見つからなかった.
jeの友达がアイデアをくれることを望んでいます.
 
まず私の現在の具体的な実現を見てみましょう
 
通信のコアクラス
public class newsSockBase 
{
    private SocketChannel sc;  
    private final int MAX_LENGTH = 8192;  
    private ByteBuffer r_buff ;  
    private ByteBuffer w_buff ;  
    private static String host ;  
    private static int port;  

	int		sendBufTotalLen;
	int     sendBufLen;
	int		sendBufStart;
	byte[]  sendBuf;
	
	int		recvBufTotalLen;
	int     recvBufLen;
	int		recvBufStart;
	byte[]  recvBuf;
	
	int		timeout;
	String  msg;
    
    public newsSockBase()
    {
    	r_buff = ByteBuffer.allocate(MAX_LENGTH);  
     	w_buff = ByteBuffer.allocate(MAX_LENGTH); 
     	
		sendBufTotalLen = MAX_LENGTH;
		sendBufLen = sendBufStart = 0;
		sendBuf = new byte[MAX_LENGTH];
		
		recvBufTotalLen = MAX_LENGTH;
		recvBufLen = recvBufStart = 0;
		recvBuf = new byte[MAX_LENGTH];
		
		timeout = 6;
    }
    
    public void setIPandPort(String str,int pt)
    {
    	host = str;
    	port  = pt;
    }
    
    //                            ,              
    public void getBufFrompara(InewsDetail nD)
    {
    	int len = nD.encode(sendBuf, sendBufStart, sendBufTotalLen-sendBufStart-sendBufLen);
    	
    	sendBufLen += len;
    	
    }
    
    public int decodeBufToPara(InewsDetail nD)
    {
    	int len = nD.decode(recvBuf, recvBufStart, recvBufLen);
    	if (len>0)	//         
    	{
        	recvBufLen -= len;
        	recvBufStart += len;		
    	}
     	
    	return len;	
    }
    
    public void start(InewsDetail nD)
    {    
    
    	//             
    	getBufFrompara(nD);
    	
        try {  
            InetSocketAddress addr = new InetSocketAddress(host, port);  
            //     socketchannel  
            sc = SocketChannel.open();  
            sc.configureBlocking(false);// 
            //    server  
            sc.connect(addr);  
            while (!sc.finishConnect())  
                ;  
            System.out.println("connection has been established!…");  

           // while (true) 
            {  
                //         //   ,              
            	w_buff.clear();  
                w_buff.put(sendBuf,sendBufStart,sendBufLen);  
                w_buff.flip();   //       


                //       
                while (w_buff.hasRemaining())  
                    sc.write(w_buff);  
                w_buff.clear();  

                //        
                while (true)
                {
                	int ss=0;
                    int count;  
                    r_buff.clear(); 
                    while(ss<timeout*100)
                    {           	         
            	        count = sc.read(r_buff);
            	        if (count>0)
            	        	break;
            	        ss++;
            	        Thread.currentThread().sleep(10); 
                    }  
                    
                    if (ss==timeout)
                    {
                    	break;
                    }
                    
                    r_buff.flip();  
                    
                    //  recvBuf           
                    if (r_buff.limit()+recvBufStart+recvBufLen>recvBufTotalLen)
                    {
                    	//    
                    	//             
                    	if (recvBufStart>0)
                    	{
                    		for(int i=0;i<recvBufStart;i++)
                    		{
                    			recvBuf[i] = recvBuf[i+recvBufStart];
                    		}
                    		recvBufStart = 0;

                    	}
                    	
                    	if (r_buff.limit()+recvBufStart+recvBufLen>recvBufTotalLen)
                		{
                			//                 ,      
                    		System.err.println("    !        !");
                		}
                    }
                    else
                    {   //          ,            。 
                    	
                    	r_buff.get(recvBuf,recvBufStart+recvBufLen,r_buff.limit());
                    	//                ,      ,      ,          ,       
                    	//           0                                 
                    	recvBufLen += r_buff.limit();
                    	if (decodeBufToPara(nD)!=0)
                    		break;
                    }
                     
        	        System.out.println("reply is " + r_buff.limit() + " long " );  
                }
            }  
            sc.socket().close();
            
        } catch (IOException ioe) {  
            ioe.printStackTrace();  
        } catch (InterruptedException ie) {  
            ie.printStackTrace();  
        }  
        
        System.out.println("Exit App....... " ); 
    }

	public static void main(String[] args) 
	{
		newsDetailNewsSum nDNS = new newsDetailNewsSum();
		newsSockBase nsb = new newsSockBase();
		nsb.setIPandPort("192.168.0.106",8888);
		nsb.start(nDNS);
		
		System.out.println("Exit Allllll....... " );
	}

}

 
 
 
次は、レポート・プロトコルのベース・クラスです.
 
 
//                     !            
public class newsDetail implements InewsDetail
{
	protected int		netErr;	//            ,     。               。
	protected int		type;	//            
	protected byte[]	StreamID=new byte[16];	//        
	protected byte[]	asyn = new byte[2];
	
	//                     
	static private int 	seq=0;	//      2       
	static private Calendar	lastCa;
	
	public newsDetail() 
	{
		getStreamID();
	}
	
	
	
	public int getType() {
		return type;
	}



	public void setType(int type) {
		this.type = type;
	}



	//             ,                      
	//                        
	//      buf           start          len          
	public int encode(byte[] buf,int start,int len)
	{
		return 0;
	}
	//      buf             start          len         
	public int decode(byte[] buf,int start,int len)
	{
		return 0;
	}

	public void getStreamID()
	{
		Calendar ca = Calendar.getInstance();
	      int year = ca.get(Calendar.YEAR);//    
	      int month=ca.get(Calendar.MONTH)+1;//     
	      int day=ca.get(Calendar.DATE);//   
	      int minute=ca.get(Calendar.MINUTE);//  
	      int hour=ca.get(Calendar.HOUR);//   
	      int second=ca.get(Calendar.SECOND);// 
	      int am_pm=ca.get(Calendar.AM_PM);
	      if (am_pm==Calendar.PM)
	    	  hour += 12;
	      if (hour>=24)
	    	  hour -= 24;
	      
	    System.out.println(seq);
	       
	      if (lastCa!=ca)
	      {
	    	  lastCa = ca;
	    	  seq = 12;
	      }
	      else
	      {
	    	  seq++;
	    	  if (seq>=100)
	    		  seq = 0;
	      }
	      
	      //           StreamID   
	      //            ,          
	      StreamID[0] = (byte)(year/1000+'0');
	      StreamID[1] = (byte)((year-(StreamID[0]-'0')*1000)/100+'0');
	      StreamID[2] = (byte)((year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100)/10+'0');
	      StreamID[3] = (byte)(year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100-(StreamID[2]-'0')*10+'0');
	      
	      StreamID[4] = (byte)(month/10+'0');
	      StreamID[5] = (byte)((month-(StreamID[4]-'0')*10)+'0');
	      
	      StreamID[6] = (byte)(day/10+'0');
	      StreamID[7] = (byte)((day-(StreamID[6]-'0')*10)+'0');
	      
	      StreamID[8] = (byte)(hour/10+'0');
	      StreamID[9] = (byte)((hour-(StreamID[8]-'0')*10)+'0');
	      
	      StreamID[10] = (byte)(minute/10+'0');
	      StreamID[11] = (byte)((minute-(StreamID[10]-'0')*10)+'0');
	      
	      StreamID[12] = (byte)(second/10+'0');
	      StreamID[13] = (byte)((second-(StreamID[12]-'0')*10)+'0');
	      
	      StreamID[14] = (byte)(seq/10+'0');
	      StreamID[15] = (byte)((seq-(StreamID[14]-'0')*10)+'0');   
	      
	      System.out.println("    ");
	      System.out.println(" Calendar.getInstance().getTime()      : " + ca.getTime());
	      System.out.println(" Calendar     :" + year +" "+ month +" "+ day + " ");      
	      System.out.println(" Calendar     :" + hour +" "+ minute +" "+ second +" ");
	
	      
	}
	

	public static void main(String[] args) 
	{
		{
			newsDetail nn1 = new newsDetail();
		}
		try {
			Thread.currentThread().sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		{
			newsDetail nn2 = new newsDetail();
		}
	      	


	}

}