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 constructors of Socket and ServerSocket classes

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the construction methods of Socket and ServerSocket classes". In the daily operation, I believe that many people have doubts about the construction methods of Socket and ServerSocket classes. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the construction methods of Socket and ServerSocket classes?" Next, please follow the editor to study!

Overview of the Socket class:

Socket enables an application to read and write data from the network. Two applications on different computers can send and receive byte streams through a connection. When sending messages, you need to know each other's ip and port. In java, socket refers to the java.net.Socket class.

Construction method: ServerSocket () creates a bind server socket ServerSocket (int port) creates a server socket, binds to the specified port ServerSocket (int port, int backlog), creates a server socket, binds it to the specified local port number, and uses the specified backlog ServerSocket (int port, int backlog, InetAddress bindAddr) to create a server with the specified port backlog to listen to the backlog. And local IP address binding API:Socket accept () listens for the connection to the socket and accepts it void bind (SocketAddress endpoint) ServerSocket binding to a specific address (IP address and port number) void bind (SocketAddress endpoint, int backlog) ServerSocket binding to a specific address (IP address and port number) void close () closes the socket. ServerSocketChannel getChannel () returns the unique ServerSocketChannel object associated with this socket. If any InetAddress getInetAddress () returns the local address of this server socket, int getLocalPort () returns the port number that this socket is listening on, SocketAddress getLocalSocketAddress (), returns the address of the endpoint to which this socket is bound, int getReceiveBufferSize (), to get the value of the SO_RCVBUF option of this ServerSocket, that is, the size of the buffer. Will be used to accept the ServerSocket binding status returned from the ServerSocket socket boolean getReuseAddress () if the SO_REUSEADDR enables int getSoTimeout () to retrieve the settings SO_TIMEOUTprotected void implAccept (Socket s) subclass using this method to overload the ServerSocket binding status returned by the socket boolean isBound () that returns its own subclass boolean isClosed (int connectionTime, int latency) Int bandwidth) set this ServerSocket performance preference void setReceiveBufferSize (int size) set a default value to propose to accept the ServerSocket socket SO_RCVBUF option void setReuseAddress (boolean on) enable / disable SO_REUSEADDR socket option static void setSocketFactory (SocketImplFactory fac) set the server socket implementation factory for the application. Void setSoTimeout (int timeout) enables / disables SO_TIMEOUT at the specified timeout, and String toString () returns this socket as the address and port ServerSocket class overview of the String implementation in milliseconds:

The Socket class represents a client socket, that is, the socket required to build any time you connect to a remote server application. Now, to implement a server application, you need a different approach. The server needs to be on standby at any time, because we don't know when the client will send the request, so we need to use ServerSocket, which corresponds to the java.net.ServerSocket class.

ServerSocket is different from Socket in that ServerSocket waits for a request from the client. Once a connection request is obtained, a Socket example is created to communicate with the client.

Construction method: Socket () creates a connection socket with the default type of the socketimpl system. Socket (InetAddress address, int port) creates a stream socket and connects it to the specified port number in the specified IP address. Socket (InetAddress host, int port, boolean stream) is outdated.

Use UDP to transport DatagramSocket. Socket (InetAddress address, int port, InetAddress localAddr, int localPort) creates a socket and connects it to the specified remote address on the specified remote port. Socket (Proxy proxy) creates a socket type for a connection, specifies the proxy, and if any, should be used regardless of any other settings. Protected Socket (SocketImpl impl) creates a user-specified socketimpl connection socket. Socket (String host, int port) creates a stream socket and connects it to the specified port number on the specified host. Socket (String host, int port, boolean stream) is outdated.

Use UDP to transport DatagramSocket. Socket (String host, int port, InetAddress localAddr, int localPort) creates a socket and connects it to the specified remote host on the specified remote port API:void bind (SocketAddress bindpoint) binds the socket to the local address. Void close () closes the socket. Void connect (SocketAddress endpoint) connects this socket to the server. Void connect (SocketAddress endpoint, int timeout) connects this socket to the server with the specified timeout value. SocketChannel getChannel () returns the unique SocketChannel object associated with this socket, if any. InetAddress getInetAddress () returns the address of the socket connection. InputStream getInputStream () returns the input stream for this socket. Boolean getKeepAlive () if SO_KEEPALIVE is enabled. InetAddress getLocalAddress () gets the local address of the bound socket. Int getLocalPort () returns the local port number bound by this socket. SocketAddress getLocalSocketAddress () returns the address of the endpoint to which this socket is bound. Boolean getOOBInline () if SO_OOBINLINE is enabled. OutputStream getOutputStream () returns the output stream of this socket. Int getPort () returns the remote port number of this socket connection. Int getReceiveBufferSize () gets the value of the SO_RCVBUF option for this Socket, which is the size of the Socket input buffer used by the platform. SocketAddress getRemoteSocketAddress () returns the address of the endpoint of this socket connection, or if it is an irrelevant null. Boolean getReuseAddress () if SO_REUSEADDR is enabled. Int getSendBufferSize () gets the SO_SNDBUF option value of the Socket, that is, the size of the buffer is used by the platform to output the Socket. Int getSoLinger () returns the setting SO_LINGER. Int getSoTimeout () returns the setting SO_TIMEOUT. Boolean getTcpNoDelay () if TCP_NODELAY is enabled. Int getTrafficClass () gets the business class or service type boolean isBound () in the IP header of the packet sent from this socket and returns the binding status of the socket. Boolean isClosed () returns the closed state of the socket. Boolean isConnected () returns the connection status of the socket. Boolean isInputShutdown () returns whether the read half of the socket connection is turned off. Boolean isOutputShutdown () returns whether the write of the socket connection is closed or not. Void sendUrgentData (int data) sends one byte of emergency data on the socket. Void setKeepAlive (boolean on) enables / disables SO_KEEPALIVE. Void setOOBInline (boolean on) enable / disable SO_OOBINLINE (TCP Emergency data receipt) by default, this option disables receiving emergency data on TCP sockets that are silently discarded. Void setPerformancePreferences (int connectionTime, int latency, int bandwidth) sets the performance preferences for this socket. Void setReceiveBufferSize (int size) sets the SO_RCVBUF option, which Socket specifies the value. Void setReuseAddress (boolean on) enables / disables the SO_REUSEADDR socket option. Void setSendBufferSize (int size) sets the SO_SNDBUF option for the value specified by this Socket. Static void setSocketImplFactory (SocketImplFactory fac) sets up the application of the client socket implementation factory. Void setSoLinger (boolean on, int linger) enables / disables SO_LINGER with the specified length of stay in seconds. Void setSoTimeout (int timeout) enables / disables SO_TIMEOUT for the specified timeout in milliseconds. Void setTcpNoDelay (boolean on) enables / disables TCP_NODELAY (the algorithm that disables / enables Nagle). Void setTrafficClass (int tc) sets the traffic class or IP header byte service that sends packets from the socket. Void shutdownInput () places the input stream for this socket in the end of the stream. Void shutdownOutput () disables the output stream for this socket. String toString () puts this String socket here, and the study on "what are the construction methods of Socket and ServerSocket classes" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report