⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.java

📁 J2me唆哈的代码
💻 JAVA
字号:
/*
 * Created on 2005-8-19 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.booqio.emulator.io.serversocket;

import java.io.IOException;

import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.StreamConnection;

import com.booqio.emulator.io.socket.*;


import java.net.ServerSocket;
/**
 * @author pcy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Socket implements ServerSocketConnection {

    private ServerSocket instance;


    /** Flag to indicate connection is currently open. */
    boolean connectionOpen = false;

    /**
     * Opens a port to listen on.
     *
     * @param port       TCP to listen on
     *
     * @exception IOException  if some other kind of I/O error occurs
     */
    public void open(int port) throws IOException {
        
        instance=new ServerSocket(port);
        connectionOpen = true;
    }

    /**
     * Checks if the connection is open.
     *
     * @exception  IOException  is thrown, if the stream is not open
     */
    void ensureOpen() throws IOException {
        if (!connectionOpen) {
            throw new IOException("Connection closed");
        }
    }


    synchronized public StreamConnection acceptAndOpen()
        throws IOException {

        Protocol con=null;

        ensureOpen();

        while (connectionOpen) {
            con =new Protocol(instance.accept());
            if(con!=null){
                break;
            }
        }
        return con;
    }


    public String getLocalAddress() throws IOException {
        ensureOpen();
        return getLocalAddress0();
    }

    public int getLocalPort() throws IOException {
        ensureOpen();
        return getLocalPort0();
    }


    public void close() throws IOException {
        if (connectionOpen) {
            close0();
            connectionOpen = false;
        }
    }

    public void close0() throws IOException{
        instance.close();
    }

    public boolean isClosed(){
        return instance.isClosed();
    }

    private String getLocalAddress0(){
        return instance.getInetAddress().getHostName();
    }


    private int getLocalPort0(){
        return instance.getLocalPort();
    }
}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -