本文整理汇总了Java中java.net.Socket.setReuseAddress方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.setReuseAddress方法的具体用法?Java Socket.setReuseAddress怎么用?Java Socket.setReuseAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Socket
的用法示例。
在下文中一共展示了Socket.setReuseAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setProperties
import java.net.Socket; //导入方法依赖的package包/类
public void setProperties(Socket socket) throws SocketException{
if (rxBufSize != null)
socket.setReceiveBufferSize(rxBufSize.intValue());
if (txBufSize != null)
socket.setSendBufferSize(txBufSize.intValue());
if (ooBInline !=null)
socket.setOOBInline(ooBInline.booleanValue());
if (soKeepAlive != null)
socket.setKeepAlive(soKeepAlive.booleanValue());
if (performanceConnectionTime != null && performanceLatency != null &&
performanceBandwidth != null)
socket.setPerformancePreferences(
performanceConnectionTime.intValue(),
performanceLatency.intValue(),
performanceBandwidth.intValue());
if (soReuseAddress != null)
socket.setReuseAddress(soReuseAddress.booleanValue());
if (soLingerOn != null && soLingerTime != null)
socket.setSoLinger(soLingerOn.booleanValue(),
soLingerTime.intValue());
if (soTimeout != null && soTimeout.intValue() >= 0)
socket.setSoTimeout(soTimeout.intValue());
if (tcpNoDelay != null)
socket.setTcpNoDelay(tcpNoDelay.booleanValue());
}
示例2: send
import java.net.Socket; //导入方法依赖的package包/类
private synchronized void send(Message message) throws IOException {
if(message == null) return;
Socket socket = new Socket();
socket.setReuseAddress(true);
socket.setPerformancePreferences(2, 1, 0); // connection, latency, bandwidth
socket.setTcpNoDelay(true);
socket.setTrafficClass(0x10); // low delay
socket.setSoTimeout(timeoutMillis);
socket.connect(address);
@SuppressWarnings("resource")
OutputStream out = socket.getOutputStream();
// TODO additional check. Is it needed?
if(out == null) throw new IOException("Socket has no output stream.");
String text = message.toString();
out.write(text.getBytes());
if(CRUSH.debug) CRUSH.debug(this, "DRP> " + text);
out.flush();
socket.close();
}
示例3: setProperties
import java.net.Socket; //导入方法依赖的package包/类
public void setProperties(Socket socket) throws SocketException {
if (rxBufSize != null)
socket.setReceiveBufferSize(rxBufSize.intValue());
if (txBufSize != null)
socket.setSendBufferSize(txBufSize.intValue());
if (ooBInline != null)
socket.setOOBInline(ooBInline.booleanValue());
if (soKeepAlive != null)
socket.setKeepAlive(soKeepAlive.booleanValue());
if (performanceConnectionTime != null && performanceLatency != null && performanceBandwidth != null)
socket.setPerformancePreferences(performanceConnectionTime.intValue(), performanceLatency.intValue(),
performanceBandwidth.intValue());
if (soReuseAddress != null)
socket.setReuseAddress(soReuseAddress.booleanValue());
if (soLingerOn != null && soLingerTime != null)
socket.setSoLinger(soLingerOn.booleanValue(), soLingerTime.intValue());
if (soTimeout != null && soTimeout.intValue() >= 0)
socket.setSoTimeout(soTimeout.intValue());
if (tcpNoDelay != null)
socket.setTcpNoDelay(tcpNoDelay.booleanValue());
}
示例4: run
import java.net.Socket; //导入方法依赖的package包/类
@Override
public void run() {
Log.d(TAG,"run");
Socket socket = new Socket();
try {
/*
note: setReuseAddress
When closing a TCP connection, the connection may stay in a time-out state (usually
referred to as TIME_WAIT state or 2 MSL wait state) for a period of time after the
close of the connection. For applications that use the well-known socket address or port,
sockets associated with a socket address or port may be unable to bind to the required
SocketAddress if it is in the timeout state.
*/
InetSocketAddress addr = new InetSocketAddress(mAddress.getHostAddress(),WiFiDirectCommunicator.PORT);
socket.bind(null);
socket.setReuseAddress(true);
socket.connect(addr,WiFiDirectCommunicator.TIMEOUT);
Log.d(TAG, "Launching the I/O handler");
manager = new CommManager(socket, handler);
new Thread(manager).start();
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
示例5: connectActually
import java.net.Socket; //导入方法依赖的package包/类
private void connectActually(){
try {
mSocket = new Socket(this.ip,this.port);
mSocket.setTcpNoDelay(true);
mSocket.setReuseAddress(true);
mDataInputStream = new DataInputStream(mSocket.getInputStream());
mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());
} catch (IOException e) {
if(mTCPResultListener !=null){
mTCPResultListener.onConnectFailed(e.getMessage());
}
}
}
示例6: run
import java.net.Socket; //导入方法依赖的package包/类
public void run() {
updateUIToast("creatting socket");
try {
clientSocket = new Socket(clientIpAddress, SocketServerPORT + 1);
clientSocket.setReuseAddress(true);
//clientSocket.bind(new InetSocketAddress(SocketServerPORT + 1));
updateUI(getIpAddress(), clientSocket.toString());
outp = new PrintWriter(clientSocket.getOutputStream(), true);
inp = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println(clientSocket.getInetAddress().getHostAddress() + " is ther server");
updateUIToast(clientSocket.getInetAddress().getHostAddress() + " is ther server");
} catch (IOException e) {
e.printStackTrace();
updateUIToast(e.toString());
}
if (clientSocket != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
connectClientButton.setVisibility(View.GONE);
}
});
}
}
示例7: getConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
* @throws IOException if an error occurred
*/
public TrackerServer getConnection(int serverIndex) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
}
示例8: configureSocket
import java.net.Socket; //导入方法依赖的package包/类
private void configureSocket(Socket socket) throws SocketException {
socket.setTcpNoDelay(TCP_NO_DELAY.get(settings));
ByteSizeValue tcpSendBufferSize = TCP_SEND_BUFFER_SIZE.get(settings);
if (tcpSendBufferSize.getBytes() > 0) {
socket.setSendBufferSize(tcpSendBufferSize.bytesAsInt());
}
ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings);
if (tcpReceiveBufferSize.getBytes() > 0) {
socket.setReceiveBufferSize(tcpReceiveBufferSize.bytesAsInt());
}
socket.setReuseAddress(TCP_REUSE_ADDRESS.get(settings));
}
示例9: connect
import java.net.Socket; //导入方法依赖的package包/类
/**
* connect to server
* @return connected Socket object
*/
public Socket connect() throws IOException
{
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout);
return sock;
}
示例10: listen
import java.net.Socket; //导入方法依赖的package包/类
public void listen() throws Exception {
if (doListen()) {
log.warn("ServerSocket already started");
return;
}
setListen(true);
while ( doListen() ) {
Socket socket = null;
if ( getTaskPool().available() < 1 ) {
if ( log.isWarnEnabled() )
log.warn("All BIO server replication threads are busy, unable to handle more requests until a thread is freed up.");
}
BioReplicationTask task = (BioReplicationTask)getTaskPool().getRxTask();
if ( task == null ) continue; //should never happen
try {
socket = serverSocket.accept();
}catch ( Exception x ) {
if ( doListen() ) throw x;
}
if ( !doListen() ) {
task.setDoRun(false);
task.serviceSocket(null,null);
getExecutor().execute(task);
break; //regular shutdown
}
if ( socket == null ) continue;
socket.setReceiveBufferSize(getRxBufSize());
socket.setSendBufferSize(getTxBufSize());
socket.setTcpNoDelay(getTcpNoDelay());
socket.setKeepAlive(getSoKeepAlive());
socket.setOOBInline(getOoBInline());
socket.setReuseAddress(getSoReuseAddress());
socket.setSoLinger(getSoLingerOn(),getSoLingerTime());
socket.setSoTimeout(getTimeout());
ObjectReader reader = new ObjectReader(socket);
task.serviceSocket(socket,reader);
getExecutor().execute(task);
}//while
}
示例11: getConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection(int serverIndex) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
}
示例12: openSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* open real socket and set time out when waitForAck is enabled
* is socket open return directly
*/
protected void openSocket() throws IOException {
if(isConnected()) return ;
try {
socket = new Socket();
InetSocketAddress sockaddr = new InetSocketAddress(getAddress(), getPort());
socket.connect(sockaddr,(int)getTimeout());
socket.setSendBufferSize(getTxBufSize());
socket.setReceiveBufferSize(getRxBufSize());
socket.setSoTimeout( (int) getTimeout());
socket.setTcpNoDelay(getTcpNoDelay());
socket.setKeepAlive(getSoKeepAlive());
socket.setReuseAddress(getSoReuseAddress());
socket.setOOBInline(getOoBInline());
socket.setSoLinger(getSoLingerOn(),getSoLingerTime());
socket.setTrafficClass(getSoTrafficClass());
setConnected(true);
soOut = socket.getOutputStream();
soIn = socket.getInputStream();
setRequestCount(0);
setConnectTime(System.currentTimeMillis());
if (log.isDebugEnabled())
log.debug(sm.getString("IDataSender.openSocket", getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)));
} catch (IOException ex1) {
SenderState.getSenderState(getDestination()).setSuspect();
if (log.isDebugEnabled())
log.debug(sm.getString("IDataSender.openSocket.failure",getAddress().getHostAddress(), Integer.valueOf(getPort()), Long.valueOf(0)), ex1);
throw (ex1);
}
}
示例13: connect
import java.net.Socket; //导入方法依赖的package包/类
private Socket connect() throws IOException {
Socket socket = new Socket();
socket.setReuseAddress(true);
socket.setSoTimeout(SOCKET_TIMEOUT);
socket.connect(address, SOCKET_TIMEOUT);
return socket;
}
示例14: getConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* return connected tracker server
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection(int serverIndex) throws IOException
{
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout);
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
}
示例15: connectSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* @since 4.1
*/
public Socket connectSocket(
final Socket socket,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpParams params) throws IOException, ConnectTimeoutException {
if (remoteAddress == null) {
throw new IllegalArgumentException("Remote address may not be null");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
Socket sock = socket;
if (sock == null) {
sock = createSocket();
}
if (localAddress != null) {
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
sock.bind(localAddress);
}
int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
int soTimeout = HttpConnectionParams.getSoTimeout(params);
try {
sock.setSoTimeout(soTimeout);
sock.connect(remoteAddress, connTimeout);
} catch (SocketTimeoutException ex) {
throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
}
return sock;
}