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

📄 sosserver.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        //IMPL_NOTE: check if socket-id is invalid        //Get the length of data to be read        byte[] dataFromProxy2 = serialPort.serialReceiveInt();        int numbytes = getIntFromByteArray(dataFromProxy2);        //System.out.println("numbytes : " + numbytes);        //Get the data to be written at the socket        buf = serialPort.serialReceive(numbytes);        //Perform the write operation at proxy socket        System.out.println("Data from client : " + new String(buf));        try {            io_writeDataToSocket(sockets[handle], buf, numbytes);        } catch (InterruptedIOException ioe) {            System.out.println("performWriteOperation : InterruptedIOException"                                + " in writing to a proxy socket");            serialPort.serialSend(PCSL_NET_INTERRUPTED);            return;        } catch (IOException ioe) {            System.out.println("performWriteOperation : IOException in"  +                                " writing to a proxy socket");            serialPort.serialSend(PCSL_NET_IOERROR);            return;        }         /*          * io_writeDataToSocket() does not return how many bytes it has          * actually written to the socket. According to the Javadoc for          * write() of an outputstream, it writes all the bytes one by one          */        // Return the no of bytes written        serialPort.serialSend(numbytes);    }    private void performReadOperation() {        try {            performReadOperationImpl();        } catch (IOException ioe) {            System.out.println("performReadOperation : IOException for serial"  +                                " port operations ");        }    }    private void performReadOperationImpl() throws IOException {        String dataString;        int handle = -1;        int numbytes;        int bytesRead;        System.out.println("performReadOperation");        //Get the socketid from client        byte[] dataFromProxy1 = serialPort.serialReceiveInt();        handle = getIntFromByteArray(dataFromProxy1);        //System.out.println("handle : " + handle);        //IMPL_NOTE: check if socket-id is invalid               //Get the number of bytes to be read         byte[] dataFromProxy2 = serialPort.serialReceiveInt();        numbytes = getIntFromByteArray(dataFromProxy2);		byte[] buf = new byte[numbytes];        try {             //Perform the read operation            bytesRead = io_readDataFromSocket(sockets[handle], buf, numbytes);        } catch (InterruptedIOException ioe) {            System.out.println("performReadOperation : InterruptedIOException"                                + " in reading from a proxy socket");            serialPort.serialSend(PCSL_NET_INTERRUPTED);            return;        } catch (IOException ioe) {            System.out.println("performReadOperation : IOException in" +                                 " reading from a proxy socket");            serialPort.serialSend(PCSL_NET_IOERROR);            return;        }                //send the number of bytes read from socket        serialPort.serialSend(bytesRead);        //System.out.println("bytesRead : " + bytesRead);        //write the actual data        serialPort.serialSend(buf, bytesRead);        //System.out.println("End of performReadOperation");    }    private void performAvailableOperation() {        try {            performAvailableOperationImpl();        } catch (IOException ioe) {            System.out.println("performAvailableOperation : IOException for serial"  +                                " port operations ");        }    }    private void performAvailableOperationImpl() throws IOException {        String dataString;        int handle = -1;        int numbytes;        int availableBytes;        System.out.println("performAvailableOperation");        //Get the socketid from client        byte[] dataFromProxy1 = serialPort.serialReceiveInt();        handle = getIntFromByteArray(dataFromProxy1);        System.out.println("handle : " + handle);        //IMPL_NOTE: check if socket-id is invalid               try {             //Perform the read operation            availableBytes = io_availableBytesFromSocket(sockets[handle]);        } catch (IOException ioe) {            System.out.println("performAvailableOperation : IOException in" +                                 " available operation in a proxy socket");            serialPort.serialSend(PCSL_NET_IOERROR);            return;        }        //System.out.println("availableBytes : " + availableBytes);                //send the number of bytes available to read from socket        serialPort.serialSend(availableBytes);    }    private boolean performCloseConnection() {        boolean serialPortClosed= false;        try {            serialPortClosed = performCloseConnectionImpl();        } catch (IOException ioe) {            System.out.println("performCloseConnection : IOException for serial"  +                                " port operations ");        }        return serialPortClosed;    }    private boolean performCloseConnectionImpl() throws IOException {        String dataString;        int handle;        System.out.println("performCloseOperation");        //Get the socketid from client        byte[] dataFromProxy1 = serialPort.serialReceiveInt();        handle = getIntFromByteArray(dataFromProxy1);        //IMPL_NOTE: check if socket-id is invalid        try {            //Perform the close operation            io_closeSocketConnection(sockets[handle]);        } catch (IOException ioe) {            System.out.println("performCloseConnectionImpl : IOException in" +                                 " closing the proxy socket");            serialPort.serialSend(PCSL_NET_IOERROR);            return false;  //serial port is still open        }        // Mark the socketid status in socketid array        socketid[handle] = SOCKET_ID_AVAILABLE;        serialPort.serialSend(SUCCESS);              return false;    }    private void performShutdownConnection() {        try {            performShutdownImpl();        } catch (IOException ioe) {            System.out.println("performShutdownConnection : IOException for serial"  +                                " port operations ");        }    }    private void performShutdownImpl() throws IOException {        String dataString;        int handle;        System.out.println("performShutdownImpl");        //Get the socketid from client        byte[] dataFromProxy1 = serialPort.serialReceiveInt();        handle = getIntFromByteArray(dataFromProxy1);        //IMPL_NOTE: check if socket-id is invalid        try {            //Perform the shutdown operation            io_shutdownSocketConnection(sockets[handle]);        } catch (IOException ioe) {            System.out.println("performShutdownImpl : IOException in" +                                 " closing the proxy socket");            serialPort.serialSend(PCSL_NET_IOERROR);            return;        }        // Mark the socketid status in socketid array        socketid[handle] = SOCKET_ID_AVAILABLE;        serialPort.serialSend(SUCCESS);    }	private Socket io_openSocketConnectionByHost(String host, int port)             throws IOException, UnknownHostException, SecurityException {		Socket socket = null;        //System.out.println("Inside io_openSocketConnection(): host: " + host +         //                   "port : " + port);        socket = new Socket(host, port);        //System.out.println("socket opened successfully");		return socket;	}	private Socket io_openSocketConnectionByIpn(byte[] ipn, int port)             throws IOException, UnknownHostException, SecurityException {		Socket socket = null;        InetAddress ipAddress = InetAddress.getByAddress(ipn);        //System.out.println("Inside io_openSocketConnection(): host: " + host +         //                   "port : " + port);        socket = new Socket(ipAddress, port);        //System.out.println("socket opened successfully");		return socket;	}    private byte[] io_getIpNumber(String host) throws UnknownHostException {        byte[] ip = InetAddress.getByName(host).getAddress();            return ip;    }    /**      * This function returns number of bytes read at proxy socket     * Returns -1 in case of an error in read operation     */	private int io_readDataFromSocket(Socket socket, byte[] buf,                             int numbytes) throws IOException {		int num = 0;        InputStream in = socket.getInputStream();        if (in.available() != 0) {            num = in.read(buf, 0, numbytes);        }        return num;	} 	private int io_availableBytesFromSocket(Socket socket)                 throws IOException {		int num = 0;        InputStream in = socket.getInputStream();        num = in.available();        return num;	}     /**      * This function does NOT return number of bytes written at proxy      * socket. It always writes all the bytes per the request. Write()     * operation for an Outputstream is a blocking operation     */	private void io_writeDataToSocket(Socket socket, byte[] buf,                                    int numbytes) throws IOException {        OutputStream out = socket.getOutputStream();        out.write(buf, 0, numbytes);	}	private void io_closeSocketConnection(Socket socket) throws IOException {        socket.close();	}	private void io_shutdownSocketConnection(Socket socket) throws IOException {        socket.shutdownInput();        socket.shutdownOutput();	}    public int getIntFromByteArray(byte[] buffer) {        int n = 0;        for(int i = 0; i < 4; i++){            n += ((0xFF & buffer[i]) << (8*(3 - i)));        }        return n;    }    public int getFirstAvailableSocketID() {        for (int index = 0; index < MAX_NO_OF_SOCKETS; index++) {            if (socketid[index] == SOCKET_ID_AVAILABLE) {                return index;            }        }        return SOCKET_ID_NOT_AVAILABLE;    }    public static void main(String[] args) {        SoSserver sosserver = new SoSserver(1);    }}

⌨️ 快捷键说明

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