亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? cwtpsocket.java~8~

?? jwap 協議 udp 可以用于手機通訊
?? JAVA~8~
字號:
/**
 * JWAP - A Java Implementation of the WAP Protocols
 * Copyright (C) 2001-2004 Niko Bender
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package net.sourceforge.jwap.wtp;

import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.*;
import java.util.Hashtable;

import net.sourceforge.jwap.util.Logger;
import net.sourceforge.jwap.util.Utils;
import net.sourceforge.jwap.wtp.pdu.CWTPAbort;
import net.sourceforge.jwap.wtp.pdu.CWTPAck;
import net.sourceforge.jwap.wtp.pdu.CWTPInvoke;
import net.sourceforge.jwap.wtp.pdu.CWTPPDU;
import net.sourceforge.jwap.wtp.pdu.EWTPCorruptPDUException;


/**
 * This class interfaces to the lower layer, the UDP Layer.
 * We use a DatagramSocket here.<br>
 * Per WSP session (defined by local port + address and remote port + address)
 * we need one object of type CWTPSocket. We may need more than one transaction
 * (CWTPTransaction) per session. CWTPSocket listens in a endless thread
 * for new datagrams on the DatagramSocket. If it gets one, it uses
 * CWTPFactory to decode the PDU. Then it associates the PDU with a transaction
 * by comparing the Transaction Identifier.<br>
 * The constructor is private. Use #getInstance(CWTPTransaction t) to
 * get an object for a specific transaction. This is because we want to check,
 * if there already exists a CWTPManagement for the session the transaction
 * belongs to.<br>
 */
public class CWTPSocket extends Thread
{
    static Logger logger = Logger.getLogger(CWTPSocket.class);

    /** WAP Socket type tcp/udp  */
    public int TcpUdp=0;
    public  int tid=0;
    /** WAP Default client port (49200)  */
    public static final int DEFAULT_PORT = 49200;

    /**
     * the underliing Layer, a DatagramSocket (UDP)
     */
    private DatagramSocket socket=null;

    /**
     * the underliing Layer, a DatagramSocket (TCP)
     */
     private Socket TcpSocket=null;
     DataOutputStream Tcpoutput=null;
     DataInputStream Tcpinput=null;

    /**
     * the upper Layer, modelling the session, WSP
     */
    private IWTPUpperLayer upperLayer;

    // remote port and address
    private int toPort;
    private InetAddress toAddress;

    // Used to synchronize reader-thread with close() method
    private byte[] lock = new byte[0];
    private boolean isRunning;

    /**
     * Holds all transactions belonging to this management entity and
     * their corresponding Transaction IDs wrapped in an Integer
     */
    private Hashtable transactions = new Hashtable();

    public CWTPSocket(InetAddress toAddress, int toPort,
        IWTPUpperLayer upperLayer) throws SocketException {
        this(toAddress, toPort, null, DEFAULT_PORT, upperLayer);
    }

    public CWTPSocket(InetAddress toAddress, int toPort, InetAddress localAddress,
        int localPort, IWTPUpperLayer upperLayer) throws SocketException {

  if(TcpUdp==1)
  {
    try {
      String str=toAddress.getHostAddress();
      TcpSocket = new Socket(toAddress.getHostAddress(), toPort);
      Tcpoutput=new DataOutputStream(TcpSocket.getOutputStream());
      Tcpinput=new DataInputStream(TcpSocket.getInputStream());
    }
    catch (UnknownHostException ex) {
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    TcpSocket.setSoTimeout(1000);
  }
  else
  {
    if (localAddress == null) {
      socket = new DatagramSocket(localPort);
    }
    else {
      socket = new DatagramSocket(localPort, localAddress);
    }

    socket.setSoTimeout(1000);
  }

        this.upperLayer = upperLayer;
        this.toAddress = toAddress;
        this.toPort = toPort;
        this.setName("CWTPSocket-"+toAddress.getHostAddress()+":"+toPort);
        if (logger.isDebugEnabled()) {
          logger.debug("開始與網關建立連接 CWTPSocket-"+toAddress.getHostAddress()+":"+toPort);
        }
        isRunning=true;
        this.start();
    }

    public CWTPInitiator tr_invoke(IWTPUpperLayer upper_Layer,
        CWTPEvent initPacket, boolean ackType, byte classType) {
        return new CWTPInitiator(this, upper_Layer, initPacket, ackType,
            classType);
    }

    public void send(CWTPPDU pdu) {
        byte[] sendBytes = pdu.toByteArray();

        try {
             if(markflg.sendreflg==1)
             {
               tid++;
               System.out.println(tid+" Send data:" + sendBytes.length + " bytes");
             }
            if (logger.isDebugEnabled())
            {
                logger.debug("sending " + CWTPPDU.types[pdu.getPDUType()] +
                    ", TID: " + pdu.getTID());
                //logger.debug("\n"+Utils.hexDump("data to send: ", sendBytes));
            }
            if(TcpUdp==1)
            {
              Tcpoutput.write(sendBytes);
            }
            else
            {
              socket.send(new DatagramPacket(sendBytes, sendBytes.length,
                                             toAddress, toPort));
            }
        } catch (IOException e) {
            logger.error("IOException while sending.", e);
        }
    }

    /**
     * This method in an (endless) loop receives DatagramPackets (UDP)
     * by the DatagramSocket layer below.
     * <br>
     * Stopping: The loop can be interrupted by calling interrupt().
     * If you would like to stop the whole socket you better
     * should call close(). It also closes the DatagramSocket below.
     */
    public void run() {
         int i=0;
        while (isRunning) {
            logger.debug("WTP-Layer listening...");

            /** @todo MRU capability negotiation
             * The folowing number (1400) should not be hard coded!
             * It is the MRU that is negotiated via WSP.
             * This is part of the WSP CAPABILITY NEGOTIATION Task!
             */
            DatagramPacket in = new DatagramPacket(new byte[1500], 1500);

            try {
                while (isRunning) {
                    try {
                         if(TcpUdp==1)
                         {
                           int flag=0;
                           byte[] ReceiveByte=new byte[2084];
                           while(true)
                           {
                             Tcpinput.read(ReceiveByte);
                             if(ReceiveByte.equals("")||ReceiveByte==null)
                             {
                               break;
                             }
                             else
                             {
                               logger.debug("\n"+Utils.hexDump("data received: ", ReceiveByte));
                             flag=1;
                             }
                           }
                           if(flag==1)
                           {
                             break;
                           }

                         }
                         else
                         {
                           //markflg.ReceiveBag();
                           socket.receive(in);
                           byte ReceiveByte[] = in.getData();
                           if(markflg.sendreflg==1)
                          {
                            i++;
                           System.out.println(i+ " receive data:" + ReceiveByte.length + " bytes");
                           markflg.ReceiveBag();
                          }

                           logger.debug("\n"+Utils.hexDump("data received: ", ReceiveByte));
                           break;
                         }


                    } catch (InterruptedIOException ie) {
                        // timeout from read...
                    }
                }
            } catch (IOException e) {
                if (isRunning) {
                    logger.error("IOException from socket.receive()", e);
                }
            }

            // Have we been stopped?
            if (!isRunning) {
                break;
            }

            // is the remote host allowed to communicate with this session?
            // if not, ignore it!
            //if (toAddress.equals(in.getAddress()) && toPort == in.getPort()){
            // decode the bytes
            CWTPPDU pdu = null;
            IWTPTransaction transact = null;

            try {
                // uses now the actual data length from the DatagramPacket
                // instead of the length of the  byte[] buffer

                pdu = CWTPPDU.decode(in.getData(), in.getLength());


                if (logger.isDebugEnabled()) {
                    logger.debug("received WTP PDU: " +
                        CWTPPDU.types[pdu.getPDUType()] + " TID: " +
                        pdu.getTID() + " | " + (pdu.getTID() + 32768));
                }
            } catch (EWTPCorruptPDUException e) {
                // if the TID is available process in state machine
                // else: ignore
                if (e.isTidAvailable()) {
                    transact = getTransaction(e.getTid() + 32768);

                    if (transact != null) {
                        // process in state machine
                        transact.process(e);
                    }
                }
            }

            // associate the PDU with the corresponding transaction
            transact = getTransaction(pdu.getTID() + 32768);

            if (transact == null) {
                // there is no transaction with this TID
                //logger.debug("new Transaction");
                // if rcvInvoke start new transaction:
                if (pdu.getPDUType() == CWTPPDU.PDU_TYPE_INVOKE) {
                    CWTPInvoke pdu2 = (CWTPInvoke) pdu;

                    if ((pdu2.getTCL() == CWTPInitiator.CLASS_TYPE_1) ||
                            (pdu2.getTCL() == CWTPInitiator.CLASS_TYPE_2)) {
                        CWTPResponder resp = new CWTPResponder(this,
                                upperLayer, pdu2, pdu2.getU_P(), pdu2.getTCL());
                    }
                }

                // if Ack PDU with TIDve flag set, send abort PDU
                if (pdu.getPDUType() == CWTPPDU.PDU_TYPE_ACK) {
                    if (((CWTPAck) pdu).getTve_tok()) {
                        send(new CWTPAbort(CWTPAbort.ABORT_REASON_INVALIDTID));
                    }
                }

                // else: ignore
            } else {
                // pdu is associated with transaction
                try {
                    //logger.debug("PDU associated");
                    // process in state machine of transaction
                    transact.process(pdu);
                } catch (EWTPAbortedException e3) {
                    logger.warn("Transaction aborted", e3);
                    transact = getTransaction(pdu.getTID());

                    if (transact != null) {
                        removeTransaction(transact);
                    }
                }
            }
        }
        // Notify close()...
        synchronized(lock) {
            lock.notifyAll();
        }
    }

    /**
     * Close the socket. Closes the underliing DatagramSocket (UDP)
     */
    public void close() {
        logger.debug("close(): Closing socket");
        isRunning=false;
        socket.close();
        if( Thread.currentThread() != this ) {
            synchronized(lock) {
                try {
                    logger.debug("close(): waiting for thread to finish");
                    lock.wait(5000); // Wait max 5 secs for reader thread to terminate...
                    logger.debug("close(): done");
                } catch (InterruptedException e) {}
            }
        }
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //XXXXXXXXXXXXXXX get/add/remove transactions XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    public boolean addTransaction(IWTPTransaction t) {
        Integer tid = new Integer(t.getTID());

        synchronized (transactions) {
            if (transactions.containsKey(tid)) {
                return false;
            }

            transactions.put(tid, t);
        }

        return true;
    }

    public IWTPTransaction getTransaction(int TID) {
        Integer tid = new Integer(TID);

        return (IWTPTransaction) transactions.get(tid);
    }

    public boolean removeTransaction(IWTPTransaction t) {
        Integer tid = new Integer(t.getTID());

        return transactions.remove(tid) != null;
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //XXXXXXXXXXXXXXX getter/setter XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    public InetAddress getLocalAddress() {
        return socket.getLocalAddress();
    }

    public int getLocalPort() {
        return socket.getLocalPort();
    }

    public InetAddress getRemoteAddress() {
        return toAddress;
    }

    public int getRemotePort() {
        return toPort;
    }
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩午夜电影av| 欧美午夜寂寞影院| a在线欧美一区| 欧美日韩国产中文| 精品久久久久99| 国产精品久久久久久福利一牛影视| 成人免费福利片| 一区二区三区四区av| 久久se精品一区二区| 成人免费观看视频| 欧美自拍丝袜亚洲| 精品国产一区二区精华| 国产精品高潮呻吟| 一区二区三区av电影| 激情小说亚洲一区| 91丨九色porny丨蝌蚪| 日韩欧美综合在线| 亚洲人成电影网站色mp4| 久久爱www久久做| 日韩一区二区三区免费观看| 国产三级三级三级精品8ⅰ区| 一区二区三区精品视频| 成人福利视频在线| 久久嫩草精品久久久久| 视频在线在亚洲| 欧洲av在线精品| 国产精品亲子伦对白| 国产在线不卡视频| 91精品国产入口在线| 日韩av成人高清| 91麻豆精品国产91久久久资源速度| 一区二区三区色| 91福利国产精品| 亚洲天堂免费在线观看视频| 99麻豆久久久国产精品免费优播| 久久久久久亚洲综合影院红桃 | 亚洲大片精品永久免费| 色综合久久天天| 亚洲第一av色| 91精品国产综合久久蜜臀| 日韩福利电影在线观看| 欧美精品一区二区蜜臀亚洲| 国产精品亚洲专一区二区三区| 久久日韩精品一区二区五区| 三级欧美韩日大片在线看| 日韩欧美区一区二| 成人免费看片app下载| 一区二区三区影院| 欧美成人乱码一区二区三区| 成人在线一区二区三区| 午夜亚洲福利老司机| 欧美r级电影在线观看| 懂色av一区二区三区免费观看| 综合分类小说区另类春色亚洲小说欧美| 在线精品亚洲一区二区不卡| 日本成人中文字幕在线视频| 久久精品日产第一区二区三区高清版| av亚洲精华国产精华| 美国一区二区三区在线播放| 亚洲人成网站在线| 精品国产三级a在线观看| 成人aaaa免费全部观看| 老司机午夜精品| 亚洲综合免费观看高清完整版 | 精品亚洲国产成人av制服丝袜| 亚洲人精品午夜| 久久久一区二区三区捆绑**| 精品视频在线免费观看| 成人黄色777网| 国产久卡久卡久卡久卡视频精品| 婷婷中文字幕一区三区| 亚洲欧美自拍偷拍色图| 国产精品久久久久一区| 777午夜精品免费视频| 欧美午夜电影网| 成人国产一区二区三区精品| 激情图片小说一区| 久久爱www久久做| 久久国产精品一区二区| 蜜臀久久久久久久| 一区二区三区精品在线观看| 亚洲精品第一国产综合野| 国产精品每日更新| 亚洲欧美一区二区久久| 一区二区三区精品在线| 日韩av午夜在线观看| 亚洲成人精品在线观看| 免费在线观看成人| 99久久精品国产一区| 国产精品一线二线三线精华| 国产99久久精品| 91福利国产成人精品照片| 91麻豆精品国产91久久久| 这里只有精品免费| 国产精品美女久久久久久久| 亚洲欧美日韩一区| 人人狠狠综合久久亚洲| 99久久99久久综合| 欧美成人乱码一区二区三区| 亚洲视频你懂的| 麻豆91在线播放免费| 欧洲在线/亚洲| 久久蜜桃av一区二区天堂| 亚洲一区二区三区四区在线| 国产精品中文有码| 欧美日韩精品三区| 国产精品白丝在线| 国产大陆亚洲精品国产| 欧美精品在线观看播放| 亚洲桃色在线一区| 国产一区二区三区四| 日韩欧美成人一区二区| 亚洲综合免费观看高清完整版 | 成人欧美一区二区三区| 久久国产剧场电影| 欧美一区二区三区免费大片| 亚洲国产成人av| 欧美日韩免费高清一区色橹橹| 亚洲九九爱视频| 99久久久国产精品| 国产精品灌醉下药二区| 9人人澡人人爽人人精品| 国产精品国产三级国产三级人妇| 国产乱人伦偷精品视频不卡| 日韩精品一区二区三区三区免费| 日本一不卡视频| 日韩一区二区影院| 国产一区二区三区免费| 一区二区三区色| 国产成人精品影视| 性久久久久久久久久久久| 一本大道综合伊人精品热热| 亚洲美女视频在线| 欧美高清视频在线高清观看mv色露露十八| 一区二区成人在线| 欧美一区中文字幕| 9i看片成人免费高清| 日韩二区三区四区| 亚洲丝袜另类动漫二区| 在线播放中文一区| 91亚洲精品一区二区乱码| 极品少妇一区二区| 午夜不卡av在线| 欧美群妇大交群中文字幕| 国产福利电影一区二区三区| 夜夜嗨av一区二区三区网页| 国产精品免费av| 欧美肥妇bbw| 丝袜脚交一区二区| 国产午夜三级一区二区三| 色偷偷88欧美精品久久久| 五月天网站亚洲| 国产欧美日韩精品一区| 欧美性猛交xxxx乱大交退制版| 久久成人免费网站| 一区二区免费在线| 久久亚洲综合av| 欧美日韩和欧美的一区二区| 成人午夜电影小说| 国精产品一区一区三区mba桃花| 亚洲乱码国产乱码精品精的特点 | 日韩理论片中文av| 精品国产一二三| 4438x成人网最大色成网站| 91影视在线播放| 国产精品1024| 美女视频免费一区| 热久久免费视频| 日本欧美一区二区在线观看| 亚洲国产精品激情在线观看 | 精品欧美一区二区三区精品久久| 91麻豆高清视频| 成人性生交大片免费看中文| 九九精品视频在线看| 蜜桃视频免费观看一区| 日本网站在线观看一区二区三区| 悠悠色在线精品| 亚洲bdsm女犯bdsm网站| 五月天欧美精品| 蜜臀久久99精品久久久画质超高清 | 亚洲一区二区偷拍精品| 一区二区高清视频在线观看| 亚洲一区精品在线| 日日夜夜免费精品| 国内精品久久久久影院一蜜桃| 国模一区二区三区白浆| 国产一区二区调教| 成人影视亚洲图片在线| 色香蕉成人二区免费| 91麻豆精品一区二区三区| 成人高清视频免费观看| 久久99精品国产.久久久久久 | 欧美另类久久久品| 精品久久久久久久久久久院品网| 国产婷婷色一区二区三区| 亚洲视频一区二区免费在线观看| 天堂资源在线中文精品| 国产成人自拍高清视频在线免费播放| 国产精品18久久久久久久网站| 99re8在线精品视频免费播放|