📄 socket.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.jni;
/* Import needed classes */
import java.nio.ByteBuffer;
/** Socket
*
* @author Mladen Turk
* @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public class Socket {
/* Standard socket defines */
public static final int SOCK_STREAM = 0;
public static final int SOCK_DGRAM = 1;
/*
* apr_sockopt Socket option definitions
*/
public static final int APR_SO_LINGER = 1; /** Linger */
public static final int APR_SO_KEEPALIVE = 2; /** Keepalive */
public static final int APR_SO_DEBUG = 4; /** Debug */
public static final int APR_SO_NONBLOCK = 8; /** Non-blocking IO */
public static final int APR_SO_REUSEADDR = 16; /** Reuse addresses */
public static final int APR_SO_SNDBUF = 64; /** Send buffer */
public static final int APR_SO_RCVBUF = 128; /** Receive buffer */
public static final int APR_SO_DISCONNECTED = 256; /** Disconnected */
/** For SCTP sockets, this is mapped to STCP_NODELAY internally. */
public static final int APR_TCP_NODELAY = 512;
public static final int APR_TCP_NOPUSH = 1024; /** No push */
/** This flag is ONLY set internally when we set APR_TCP_NOPUSH with
* APR_TCP_NODELAY set to tell us that APR_TCP_NODELAY should be turned on
* again when NOPUSH is turned off
*/
public static final int APR_RESET_NODELAY = 2048;
/** Set on non-blocking sockets (timeout != 0) on which the
* previous read() did not fill a buffer completely. the next
* apr_socket_recv() will first call select()/poll() rather than
* going straight into read(). (Can also be set by an application to
* force a select()/poll() call before the next read, in cases where
* the app expects that an immediate read would fail.)
*/
public static final int APR_INCOMPLETE_READ = 4096;
/** like APR_INCOMPLETE_READ, but for write
*/
public static final int APR_INCOMPLETE_WRITE = 8192;
/** Don't accept IPv4 connections on an IPv6 listening socket.
*/
public static final int APR_IPV6_V6ONLY = 16384;
/** Delay accepting of new connections until data is available.
*/
public static final int APR_TCP_DEFER_ACCEPT = 32768;
/** Define what type of socket shutdown should occur.
* apr_shutdown_how_e enum
*/
public static final int APR_SHUTDOWN_READ = 0; /** no longer allow read request */
public static final int APR_SHUTDOWN_WRITE = 1; /** no longer allow write requests */
public static final int APR_SHUTDOWN_READWRITE = 2; /** no longer allow read or write requests */
public static final int APR_IPV4_ADDR_OK = 0x01;
public static final int APR_IPV6_ADDR_OK = 0x02;
/* TODO: Missing:
* APR_INET
* APR_UNSPEC
* APR_INET6
*/
public static final int APR_UNSPEC = 0;
public static final int APR_INET = 1;
public static final int APR_INET6 = 2;
public static final int APR_PROTO_TCP = 6; /** TCP */
public static final int APR_PROTO_UDP = 17; /** UDP */
public static final int APR_PROTO_SCTP = 132; /** SCTP */
/**
* Enum to tell us if we're interested in remote or local socket
* apr_interface_e
*/
public static final int APR_LOCAL = 0;
public static final int APR_REMOTE = 1;
/* Socket.get types */
public static final int SOCKET_GET_POOL = 0;
public static final int SOCKET_GET_IMPL = 1;
public static final int SOCKET_GET_APRS = 2;
public static final int SOCKET_GET_TYPE = 3;
/**
* Create a socket.
* @param family The address family of the socket (e.g., APR_INET).
* @param type The type of the socket (e.g., SOCK_STREAM).
* @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
* @param cont The parent pool to use
* @return The new socket that has been set up.
*/
public static native long create(int family, int type,
int protocol, long cont)
throws Exception;
/**
* Shutdown either reading, writing, or both sides of a socket.
* <br />
* This does not actually close the socket descriptor, it just
* controls which calls are still valid on the socket.
* @param thesocket The socket to close
* @param how How to shutdown the socket. One of:
* <PRE>
* APR_SHUTDOWN_READ no longer allow read requests
* APR_SHUTDOWN_WRITE no longer allow write requests
* APR_SHUTDOWN_READWRITE no longer allow read or write requests
* </PRE>
*/
public static native int shutdown(long thesocket, int how);
/**
* Close a socket.
* @param thesocket The socket to close
*/
public static native int close(long thesocket);
/**
* Destroy a pool associated with socket
* @param thesocket The destroy
*/
public static native void destroy(long thesocket);
/**
* Bind the socket to its associated port
* @param sock The socket to bind
* @param sa The socket address to bind to
* This may be where we will find out if there is any other process
* using the selected port.
*/
public static native int bind(long sock, long sa);
/**
* Listen to a bound socket for connections.
* @param sock The socket to listen on
* @param backlog The number of outstanding connections allowed in the sockets
* listen queue. If this value is less than zero, the listen
* queue size is set to zero.
*/
public static native int listen(long sock, int backlog);
/**
* Accept a new connection request
* @param sock The socket we are listening on.
* @param pool The pool for the new socket.
* @return A copy of the socket that is connected to the socket that
* made the connection request. This is the socket which should
* be used for all future communication.
*/
public static native long accept(long sock)
throws Exception;
/**
* Set an OS level accept filter.
* @param sock The socket to put the accept filter on.
* @param name The accept filter
* @param args Any extra args to the accept filter. Passing NULL here removes
* the accept filter.
*/
public static native int acceptfilter(long sock, String name, String args);
/**
* Query the specified socket if at the OOB/Urgent data mark
* @param sock The socket to query
* @return True if socket is at the OOB/urgent mark,
* otherwise return false.
*/
public static native boolean atmark(long sock);
/**
* Issue a connection request to a socket either on the same machine
* or a different one.
* @param sock The socket we wish to use for our side of the connection
* @param sa The address of the machine we wish to connect to.
*/
public static native int connect(long sock, long sa);
/**
* Send data over a network.
* <PRE>
* This functions acts like a blocking write by default. To change
* this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
* socket option.
*
* It is possible for both bytes to be sent and an error to be returned.
*
* APR_EINTR is never returned.
* </PRE>
* @param sock The socket to send the data over.
* @param buf The buffer which contains the data to be sent.
* @param offset Offset in the byte buffer.
* @param len The number of bytes to write; (-1) for full array.
* @return The number of bytes send.
*
*/
public static native int send(long sock, byte[] buf, int offset, int len);
/**
* Send data over a network.
* <PRE>
* This functions acts like a blocking write by default. To change
* this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
* socket option.
*
* It is possible for both bytes to be sent and an error to be returned.
*
* APR_EINTR is never returned.
* </PRE>
* @param sock The socket to send the data over.
* @param buf The Byte buffer which contains the data to be sent.
* @param offset The offset within the buffer array of the first buffer from
* which bytes are to be retrieved; must be non-negative
* and no larger than buf.length
* @param len The maximum number of buffers to be accessed; must be non-negative
* and no larger than buf.length - offset
* @return The number of bytes send.
*
*/
public static native int sendb(long sock, ByteBuffer buf,
int offset, int len);
/**
* Send data over a network using internally set ByteBuffer
*/
public static native int sendbb(long sock,
int offset, int len);
/**
* Send multiple packets of data over a network.
* <PRE>
* This functions acts like a blocking write by default. To change
* this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
* socket option.
* The number of bytes actually sent is stored in argument 3.
*
* It is possible for both bytes to be sent and an error to be returned.
*
* APR_EINTR is never returned.
* </PRE>
* @param sock The socket to send the data over.
* @param vec The array from which to get the data to send.
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -