本文整理汇总了Java中java.net.Socket.setTcpNoDelay方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.setTcpNoDelay方法的具体用法?Java Socket.setTcpNoDelay怎么用?Java Socket.setTcpNoDelay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Socket
的用法示例。
在下文中一共展示了Socket.setTcpNoDelay方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: openChannel
import java.net.Socket; //导入方法依赖的package包/类
public static Channel openChannel ( InetSocketAddress isa ) throws IOException, SocketException {
System.err.println("* Opening socket " + isa);
Socket s = SocketFactory.getDefault().createSocket(isa.getAddress(), isa.getPort());
s.setKeepAlive(true);
s.setTcpNoDelay(true);
System.err.println("* Opening channel");
OutputStream outputStream = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(outputStream);
dos.writeUTF("Protocol:CLI-connect");
ExecutorService cp = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread ( Runnable r ) {
Thread t = new Thread(r, "Channel");
t.setDaemon(true);
return t;
}
});
Channel c = new ChannelBuilder("EXPLOIT", cp).withMode(Mode.BINARY).build(s.getInputStream(), outputStream);
System.err.println("* Channel open");
return c;
}
示例2: connect
import java.net.Socket; //导入方法依赖的package包/类
public void connect() {
if (!isConnected()) {
try {
socket = new Socket();
socket.setReuseAddress(true);
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.setSoLinger(true, 0);
socket.connect(new InetSocketAddress(host, port), connectionTimeout);
if ( soTimeout > 0)
socket.setSoTimeout(soTimeout);
outputStream = new RedisOutputStream(socket.getOutputStream());
inputStream = new RedisInputStream(socket.getInputStream());
} catch (IOException ex) {
broken = true;
throw new JedisConnectionException("Failed connecting to host " + host + ":" + port, ex);
}
}
}
示例3: b
import java.net.Socket; //导入方法依赖的package包/类
private static boolean b(String str) {
long currentTimeMillis = System.currentTimeMillis();
try {
b.a("ConnectivityTest: begin to connect to " + str);
Socket socket = new Socket();
socket.connect(Host.b(str, 5222), BaseImageDownloader.DEFAULT_HTTP_CONNECT_TIMEOUT);
socket.setTcpNoDelay(true);
b.a("ConnectivityTest: connect to " + str + " in " + (System.currentTimeMillis() -
currentTimeMillis));
socket.close();
return true;
} catch (Throwable th) {
b.d("ConnectivityTest: could not connect to:" + str + " exception: " + th.getClass()
.getSimpleName() + " description: " + th.getMessage());
return false;
}
}
示例4: 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();
}
示例5: bind
import java.net.Socket; //导入方法依赖的package包/类
@Override
public void bind(
final Socket socket,
final HttpParams params) throws IOException {
Args.notNull(socket, "Socket");
Args.notNull(params, "HTTP parameters");
assertNotOpen();
socket.setTcpNoDelay(params.getBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true));
socket.setSoTimeout(params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0));
socket.setKeepAlive(params.getBooleanParameter(CoreConnectionPNames.SO_KEEPALIVE, false));
final int linger = params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1);
if (linger >= 0) {
socket.setSoLinger(linger > 0, linger);
}
super.bind(socket, params);
}
示例6: createWireIO
import java.net.Socket; //导入方法依赖的package包/类
public WireIO createWireIO(Socket clientSocket) {
try {
clientSocket.setTcpNoDelay(true); // Necessary at least on Solaris to avoid delays in e.g. readInt() etc.
ObjectInputStream socketIn = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream socketOut = new ObjectOutputStream(clientSocket.getOutputStream());
WireIO wireIO = new WireIO(socketOut, socketIn);
return wireIO;
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
示例7: peerFromSocket
import java.net.Socket; //导入方法依赖的package包/类
public static Peer peerFromSocket(Socket socket)
throws IOException {
Peer peer = null;
boolean success = false;
try {
// TCP_NODELAY is crucial here because of bad interactions between
// Nagle's Algorithm and Delayed ACKs. With connection keepalive
// between the client and DN, the conversation looks like:
// 1. Client -> DN: Read block X
// 2. DN -> Client: data for block X
// 3. Client -> DN: Status OK (successful read)
// 4. Client -> DN: Read block Y
// The fact that step #3 and #4 are both in the client->DN direction
// triggers Nagling. If the DN is using delayed ACKs, this results
// in a delay of 40ms or more.
//
// TCP_NODELAY disables nagling and thus avoids this performance
// disaster.
socket.setTcpNoDelay(true);
SocketChannel channel = socket.getChannel();
if (channel == null) {
peer = new BasicInetPeer(socket);
} else {
peer = new NioInetPeer(socket);
}
success = true;
return peer;
} finally {
if (!success) {
if (peer != null) peer.close();
socket.close();
}
}
}
示例8: connect
import java.net.Socket; //导入方法依赖的package包/类
public void connect() throws UnknownHostException, IOException {
LOG.log(Level.INFO, "connecting to " + host + ":" + port);
socket = new Socket(host, port);
socket.setTcpNoDelay(true);
socket.setSoTimeout(DEFAULT_TIMEOUT);
in = socket.getInputStream();
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
thread = new URClientThread(name);
thread.start();
connected = true;
}
示例9: GUIControlChannel
import java.net.Socket; //导入方法依赖的package包/类
/**
* Try to connect to a remote FDT instance
*
* @param inetAddress
* @param port
* @param notifier
* @throws Exception
*/
public GUIControlChannel(InetAddress inetAddress, int port, GUIControlChannelNotifier notifier) throws Exception {
try {
this.notifier = notifier;
controlSocket = new Socket();
controlSocket.connect(new InetSocketAddress(inetAddress, port), CONNECT_TIMEOUT);
this.remoteAddress = inetAddress;
this.remotePort = port;
this.localPort = controlSocket.getLocalPort();
controlSocket.setTcpNoDelay(true);
//only the first octet will be interpreted by the AcceptTask at the other end
controlSocket.getOutputStream().write(new byte[]{3});
//from now on only CtrlMsg will be sent
initStreams();
controlSocket.setSoTimeout(1000);
//
} catch (Throwable t) {
close("Cannot instantiate ControlChannel", t);
throw new Exception(t);
}
}
示例10: setAcceptedSocketOptions
import java.net.Socket; //导入方法依赖的package包/类
public void setAcceptedSocketOptions(Acceptor acceptor,
ServerSocket serverSocket,
Socket socket)
throws SocketException
{
// Disable Nagle's algorithm (i.e., always send immediately).
socket.setTcpNoDelay(true);
if (keepAlive)
socket.setKeepAlive(true);
}
示例11: setSocketOptions
import java.net.Socket; //导入方法依赖的package包/类
/**
* Set the options for the current socket.
*/
protected boolean setSocketOptions(Socket socket) {
// Process the connection
int step = 1;
try {
// 1: Set socket options: timeout, linger, etc
if (soLinger >= 0) {
socket.setSoLinger(true, soLinger);
}
if (tcpNoDelay) {
socket.setTcpNoDelay(tcpNoDelay);
}
if (soTimeout > 0) {
socket.setSoTimeout(soTimeout);
}
// 2: SSL handshake
step = 2;
serverSocketFactory.handshake(socket);
} catch (Throwable t) {
if (log.isDebugEnabled()) {
if (step == 2) {
log.debug(sm.getString("endpoint.err.handshake"), t);
} else {
log.debug(sm.getString("endpoint.err.unexpected"), t);
}
}
// Tell to close the socket
return false;
}
return true;
}
示例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: prepareSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* Performs standard initializations on a newly created socket.
*
* @param sock the socket to prepare
* @param context the context for the connection
* @param params the parameters from which to prepare the socket
*
* @throws IOException in case of an IO problem
*/
protected void prepareSocket(
final Socket sock,
final HttpContext context,
final HttpParams params) throws IOException {
sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
final int linger = HttpConnectionParams.getLinger(params);
if (linger >= 0) {
sock.setSoLinger(linger > 0, linger);
}
}
示例14: create
import java.net.Socket; //导入方法依赖的package包/类
@Override
public HttpClientConnection create(final HttpHost host) throws IOException {
final String scheme = host.getSchemeName();
Socket socket = null;
if ("http".equalsIgnoreCase(scheme)) {
socket = this.plainfactory != null ? this.plainfactory.createSocket() :
new Socket();
} if ("https".equalsIgnoreCase(scheme)) {
socket = (this.sslfactory != null ? this.sslfactory :
SSLSocketFactory.getDefault()).createSocket();
}
if (socket == null) {
throw new IOException(scheme + " scheme is not supported");
}
final String hostname = host.getHostName();
int port = host.getPort();
if (port == -1) {
if (host.getSchemeName().equalsIgnoreCase("http")) {
port = 80;
} else if (host.getSchemeName().equalsIgnoreCase("https")) {
port = 443;
}
}
socket.setSoTimeout(this.sconfig.getSoTimeout());
if (this.sconfig.getSndBufSize() > 0) {
socket.setSendBufferSize(this.sconfig.getSndBufSize());
}
if (this.sconfig.getRcvBufSize() > 0) {
socket.setReceiveBufferSize(this.sconfig.getRcvBufSize());
}
socket.setTcpNoDelay(this.sconfig.isTcpNoDelay());
final int linger = this.sconfig.getSoLinger();
if (linger >= 0) {
socket.setSoLinger(true, linger);
}
socket.setKeepAlive(this.sconfig.isSoKeepAlive());
socket.connect(new InetSocketAddress(hostname, port), this.connectTimeout);
return this.connFactory.createConnection(socket);
}
示例15: configureSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* Configures socket properties based on properties from the connection
* (tcpNoDelay, snd/rcv buf, traffic class, etc).
*
* @param props
* @throws SocketException
* @throws IOException
*/
private void configureSocket(Socket sock, Properties props) throws SocketException, IOException {
sock.setTcpNoDelay(Boolean.valueOf(props.getProperty(TCP_NO_DELAY_PROPERTY_NAME, TCP_NO_DELAY_DEFAULT_VALUE)).booleanValue());
String keepAlive = props.getProperty(TCP_KEEP_ALIVE_PROPERTY_NAME, TCP_KEEP_ALIVE_DEFAULT_VALUE);
if (keepAlive != null && keepAlive.length() > 0) {
sock.setKeepAlive(Boolean.valueOf(keepAlive).booleanValue());
}
int receiveBufferSize = Integer.parseInt(props.getProperty(TCP_RCV_BUF_PROPERTY_NAME, TCP_RCV_BUF_DEFAULT_VALUE));
if (receiveBufferSize > 0) {
sock.setReceiveBufferSize(receiveBufferSize);
}
int sendBufferSize = Integer.parseInt(props.getProperty(TCP_SND_BUF_PROPERTY_NAME, TCP_SND_BUF_DEFAULT_VALUE));
if (sendBufferSize > 0) {
sock.setSendBufferSize(sendBufferSize);
}
int trafficClass = Integer.parseInt(props.getProperty(TCP_TRAFFIC_CLASS_PROPERTY_NAME, TCP_TRAFFIC_CLASS_DEFAULT_VALUE));
if (trafficClass > 0) {
sock.setTrafficClass(trafficClass);
}
}