Current location - Quotes Website - Collection of slogans - How to bind the port of socket client in java?
How to bind the port of socket client in java?
The Socket client in java only needs to create a socket object, with the ip of the machine where the server is located and the port of the server as parameters. The client code can be seen in the following example:

Socket socket = new socket ("168.160.12.42", 9998);

Or:

Socket Socket = new Socket(inet address . getlocalhost(),5678); //Apply to connect to the server with the host name InetAddress.getLocalHost ().

The client must know the IP address of the server, and Java also provides the related class InetAddress. An instance of this object must be provided through its static method, which mainly provides a method to obtain the local IP and InetAddress directly by name or IP.

in = new buffered reader(new InputStreamReader(socket . getinputstream()));

out = new PrintWriter(socket . get output stream(),true);

The above program code establishes a Socket object, which is connected to a server object with an ip address of168.160.12.42 and a port of 9998. And establish an input stream and an output stream, which correspond to the output of the server and the writing of the client respectively.