Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the options for JAVA Socket

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what are the options for JAVA Socket, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The Socket option specifies how the native socket that the Java Socket class depends on sends and receives data. Nine options are supported for client-side Socket,Java:

TCP_NODELAY

SO_BINDADDR

SO_TIMEOUT

SO_LINGER

SO_SNDBUF

SO_RCVBUF

SO_KEEPALIVE

OOBINLINE

IP_TOS

TCP_NODELAY

Public void setTcpNoDelay (boolean on) throws SocketException

Public boolean getTcpNoDelay () throws SocketException

Setting TCP_NODELAY to true ensures that packets are sent as quickly as possible, regardless of packet size.

SetTcpNoDelay (true) turns off buffering for Socket. SetTcpNoDelay (false) enables buffering again!

GetTcpNoDelay () returns true when buffering is off, and false if buffering is turned on

SO_LINGER

Public void setSoLinger (boolean on, int linger) throws SocketException

Public int getSoLinger () throws SocketException

The SO_LINGER option specifies how to handle data that has not been sent when Socket is closed. By default, the close () method returns immediately, but the system still attempts to send the rest of the data. If the delay time is set to 0, all unsent packets will be discarded when Socket is shut down. If SO_LINGER is turned on and the delay time is set to any positive number, the close () method blocks, waiting for data to be sent and an acknowledgement to be received. When the corresponding number of seconds in the past, Socket is closed, all remaining data will not be sent and no acknowledgement will be received!

SO_TIMEOUT

Public synchronized void setSoTimeout (int timeout) throws SocketException

Public synchronized int getSoTimeout () throws SocketException

Normally, when trying to read data from Socket, the read () call takes as long as possible to get enough bytes. Setting SO_TIMEOUT ensures that the blocking time for each call does not exceed a fixed number of milliseconds; 0 is interpreted as an infinite timeout, which is the default!

SO_RCVBUF and SO_SNDBUF

Public synchronized void setReceiveBufferSize (int size) throws SocketException

Public synchronized int getReceiveBufferSize () throws SocketException

Public synchronized void setSendBufferSize (int size) throws SocketException

Public synchronized int getSendBufferSize () throws SocketException

The SO_RCVBUF option controls the size of the recommended receive buffer for network input. The SO_SNDBUF option controls the size of the recommended send buffer for network input

SO_KEEPALIVE

Public void setKeepAlive (boolean on) throws SocketException

Public boolean getKeepAlive () throws SocketException

The default value for SO_KEEPALIVE is false. If SO_KEEPALIVE is turned on, the following code closes it:

If (s.getKeepAlive ())

{

S.setKeepAlive (false)

}

OOBINLINE

By default, Java ignores emergency data received from Socket. However, if you want to receive emergency data from normal data, you need to set the OOBINLINE option to true using the following method:

Public void setOOBInline (boolean on) throws SocketException

Public boolean getOOBInline () throws SocketException

The default value for OOBINLINE is false.

SO_REUSEADDR

Public void setReuseAddress (boolean on) throws SocketException

Public boolean getReuseAddress () throws SocketException

Close or connect

If socket is closed, the isClosed () method returns true, otherwise it returns false. If you are not sure about the status of a Socket, you can use this method to check first.

However, this is not a comprehensive test. If a Socket is not connected from the beginning, isClosed () also returns false, even though Socket is not actually opened at all.

The Socket class also has an isConnected () method. If the Socket does connect to the remote host, the isConnected () method returns true, even if the Socket is closed.

To see whether a Socket is currently open, you need to check two conditions:

/ / first, isConnected () returns true, and isClosed () returns false.

Boolen connected = socket.isConnected () & &! socket.isClosed ()

What are the options for JAVA Socket to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report