Java Network Programming (next) __ Programming

Source: Internet
Author: User
Tags connect socket
One, client socketDefinition: The socket is a connection between two hosts, a single connection, the local and remote host from this socket to get input and output streams, use this stream can send and receive data at the same time. Socket to the programmer mask the bottom of the network details, such as error detection, packet size, packet decomposition, Baozhong, network address. 1. Construct and connect SocketThe Javanetsocket class is the base class for Java to complete client TCP operations, which uses native code to communicate with the local TCP stack of the host operating system.

constructor function:

Pubic socket (String host,int port) throws Unknownhostexception,ioexception public socket (inetaddress host,int port) Throws IOException/** The following two constructors can specify the local network interface and port to connect, the network interface can be a physical interface or a virtual interface, and if LocalPort incoming 0,java will randomly select an available port between 1024 and 65535 * /Public Socket (String host,int port,inetaddress interface,int localport) throws Ioexception,unknownhostexception Public Socket (inetaddress host,int port,inetaddress interface,int localport) throws IOException/See the source can actually find the parameters passed in is actually two SocketAddress (address + port), one is connected to the host, one is the local network interface. Another constructor is invoked in all of the constructor methods above/**: Public Socket (socketaddress address,socketaddress Localaddr,boolean stream): In this constructor, a call to the Bind (localaddress); Connect (address); So when you make a connection, you create a Socket, then call the Bind method to start the binding, and finally call the Connect method for the connection. So the following 3 constructors are not connected/public sockets () public socket (proxy proxy) protected socket (SocketImpl impl)

When a connection is established, it is time to start sending data to each other.

The public InputStream getInputStream () throws ioexception;//gets the input stream from which to get the data from the service end public OutputStream Getoutputstream () Throws ioexception;//gets the output stream that can be used to send data to the server

Semi-close Socket

public void Close ()//closes the socket's input and output stream public void Shutdowninput () throws IOException;//closes the input stream, shutdown method does not close the socket The public void Shutdownoutput () throws IOException;//closes the output stream.

Get information about the socket

Public inetaddress getinetaddress ()//Return the address of the remote host to public int getport ();//Return to the remote host port public inetaddress getlocaladdress () //Returns the address of the local host to public int getlocalport ();//Returns the Port public boolean isclosed () for the local host, or whether the public boolean isconnected () has been closed;// Indicates whether a remote host has never been connected and returns true as long as it is connected

Socket options: all have corresponding getter and setter methods

Tcp_nodelay: Whether to turn off the buffer of the socket, if it is turned off, it will be sent once it is ready, instead of waiting until the buffer is full So_linger: Indicates how to handle the datagram that has not been sent when the socket closes, you can set the number of seconds to delay, and once time is up, Close the connection regardless of whether there is any data. Maximum latency is 65,535 seconds So_timeout: Indicates the length of time that the Read method blocks when fetching data from the socket. 0 is infinite, also the default value SO_REVBUF: Sets the size of the receive buffer, and the head layer uses the buffer to elevate the performance of the network SO_SNDBUF: Sets the size of the send buffer, but is actually set to a so_keepalive with a smaller size of the send and receive buffers: The client occasionally sends a packet through an idle connection to ensure that the server is not crashed oobinline:out of Band, emergency data, will be sent immediately so_reuseaddr: Reuse port, may receive data not received by previous socket 2.Socket AddressDefinition: The SocketAddress class represents a connection endpoint, which is an empty abstract class, and its primary purpose is to provide a convenient storage for temporary socket connection information such as IP address and port

Method:

Public socketaddress getremotesocketaddress ()//Returns the address of the connected system public socketaddress getlocalsocketaddress (); Returns the address of the initiating connection

Subclass Inetsocketaddress Create:

Public inetsocketaddress (inetaddress address,int Port)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.