IPAddress and port number
Port numbers are represented by two bytes (16-bit binary numbers) and range from 0 to 65535, where the port numbers between 0 and 1023 are used for some well-known network services and applications.
A user’s normal application needs to use port numbers above 1024 to avoid being occupied by another application or service.
InetAddress
common method

Code demo:
DatagramPacket



DatagramSocket



UDPNetwork program

Code demo:
ServerSocket


Socket


Method declaration
|
Functional description
|
int getPort()
|
This method returns a int type object, which is the port number of the Socket object connected to the server side.
|
InetAddress getLocalAddress()
|
This method is used to get the local IP address bound by the Socket object and encapsulate the IP address as an object return of type InetAddress
|
void close()
|
This method is used to close the Socket connection and end this communication. Before closing the socket, all input / output streams associated with the socket should be shut down, because a good program should release all resources at the end of execution
|
InputStream getInputStream()
|
This method returns an input stream object of type InputStream, which, if returned by a server-side Socket, is used to read the data sent by the client, and vice versa, to read the data sent by the server-side.
|
OutputStream getOutputStream()
|
This method returns an OutputStream-type output stream object that, if returned by a server-side Socket, is used to send data to the client, and vice versa, to send data to the server.
|
Graphic:

Code demo: