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

Java network programming content

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Java network programming:

1 、 URL 、 URLConnection 、 Socket

2. IP:Internet Protocol, using 32-bit length (4 bytes) of binary data.

3. IP addresses are divided into five categories.

Category A government agencies 1.0.0.1-126.255.255.254

Category B medium-sized companies 128.0.0.1-191.255.255.254

Category C anyone 192.0.0.1-223.255.255.254

Class D Multicast 224.0.0.1-239.255.255.254

240.0.0.1 for class E experiments-255.255.255.255

127.0.0.1 or Localhost represents the native machine

4. The InetAddress class in java represents the IP address, which is used to realize the translation between hostname and IP address. The InetAddress class describes the IP address and implements it through Inet4Address and Inet6Address.

5. Common methods:

Define the object through the static factory method.

Static InetAddress getLocalHost () gets the InetAddress object of the local host

Static InetAddress getByName (String host) gets the InetAddress object specified by host. Host can be hostname, IP, DNS domain name

String getHostAddress () gets the IP address as a string with dots.

String getHostName () gets the host name

6. TCP/IP hierarchical structure

Application layer, transport layer, network layer, data link layer

IP: network layer

TCP: transport layer

UDP: transport layer

HTTP: application layer

FTP: application layer

URL:Universal Resource Locator uniform resource location symbol.

It consists of the following five parts:

: / /: / #

Transport Protocol (protocol): HTTP, FTP, File, etc.

Hostname (hostname): specifies the hostname of the resource. Can be IP, hostname, or domain name

Port number (port): used to distinguish between different services provided in a computer, such as web services and FTP services, each using a port number. 0-65535. When the port is omitted, the default is 80 for the website. 0room1023 is the port number reserved by the system, so try to use more than 1024.

File name (filename): includes the full path to the file. The default file name is index.html

Reference (regerence): a reference within a resource, such as http://www.google.com/index.html#chapter1.ppt

7. URL class

URL url = new URL ("http://www.baidu.com");

Public String getProtocol () URL protocol

Hostname of public String getHost () URL

Port of public int getPort () URL

File name of public String getFile () URL

Public String getContent () transport protocol

InputStream openStream () opens the input stream of the URL

Public String getPath gets the path of the URL

URLConnection openConnection () opens the connection to the location represented by the URL

Void set (string protocol,string host,int port,string file,string ref) sets the values for each field of the URL.

8. Use steps:

1. Create a URL object

2. Use the openStream () method of the URL object to return an InputStream

3. Just read it from InputStream.

9. URLConnection represents the communication connection between the application and the resource identified by URL, which is an abstract class

Url.openConnection ()

Void connect ()

Object getContent () gets the content of the URL

Int getContentLength () gets the content length of the response data

String getContentType () gets the content type of the response data.

Long getDate () gets the creation time of the response data

Long getExpiration () gets the termination time of the response data

GetInputStream () gets the input stream of the connection

GetLastModified () gets the last modification time of the response data

GetOutputStream gets the output stream of the connection

Public class TestNet {

Public static void main (String [] args) {

Try {

Int c

URL url = new URL ("http://skynet.skhynix-cq.com.cn/plusWare/Main.aspx/");

URLConnection urlConnection = url.openConnection ()

System.out.println ("the date is:" + new Date (urlConnection.getDate ()

System.out.println ("content-type:" + urlConnection.getContentType ())

InputStream inputStream = urlConnection.getInputStream ()

While ((c=inputStream.read ())! =-1) {

System.out.print ((char) c)

}

InputStream.close ()

} catch (MalformedURLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

} catch (IOException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

}

10. Socket (socket)

The socket is the programming interface of the TCP/IP protocol, and a Socket is uniquely determined by an IP address and a port number. An endpoint running on a network for two-way communication between processes of two different hosts, used to establish a channel for communication between two different applications over the network.

In order to communicate in the network environment between application processes located on different hosts, a socket must be established in each segment of the network.

Data stream sockets (Stream Socket) are connected, reliable, bi-directional, persistent, point-to-point, and slow represents TCP

Packet sockets (Datagram Socket) are connectionless. The representative is UDP

11. Socket class

Create a client socket object with the constructor.

Close () closes the socket connection

InetAddress getInetAddress () gets the Internet address of the remote host that is currently connected

InputStream getInputStream () gets the input stream corresponding to Socket

InetAddress getLocalAddress () gets the Internet address of the local host

Int getLocalPort () gets the port number of the local connection

OutputStream getOutputStream () gets the output stream of the Socket

Int getPort () gets the port number of the remote host

Void shutdownInput () closes the input stream

Void shutdownOutput ()

12. ServerSocket class

Used on the server side to listen for all connections from the specified port without creating a Socket object for each new connection.

ServerSocket (int port) create a connection socket

ServerSocket (int port, int backlog): create a listening socket

ServerSocket (int port, int backlog,InetAddress bindAddr) snooping socket

Socket accept () accepts the connection and returns a socket object.

Void close ()

InetAddress getInetAddress () gets the address bound by the server Socket

Int getLocalPort () gets the port number that the server Socket listens on

Int getSoTimeout () gets the timeout of the connection

Void setSoTimeout (int timeout) represents the timeout for the accept () method of ServerSocket to wait for a customer connection. If the parameter value is not 0, it means that it will never time out and enter the blocking state.

13. Socket communication:

The server constructs a ServerSocket object to listen on the established port, and the server thread is in a waiting state.

Then the Client side constructs the Socket class object to connect with the designated port on the server. The server establishes a connection after receiving the connection request. Communication depends on input and output streams.

Server: ServerSocket object-- accept () method

Client: socket object. Request connection

GetInputStream and getOutputStream of the Socket class

Close after communication is completed

Public class TestNet {

Public static void main (String [] args) throws Exception {

ServerSocket server = null

Socket pair = null

PrintStream outPrintStream = null

Server = new ServerSocket (2588)

System.out.println ("Server running, waiting for client connection")

Pair = server.accept (); / / waiting for connection

String string = "hello network!"

OutPrintStream = new PrintStream (pair.getOutputStream ())

OutPrintStream.println (string)

Pair.close ()

Server.close ()

}

}

/ / Client

Public class Client {

Public static void main (String [] args) throws Exception {

Socket client = null

Client = new Socket ("localhost", 2588); / / create a new Socket and connect

BufferedReader buf = null

Buf = new BufferedReader (new InputStreamReader (client.getInputStream ()

String str = buf.readLine ()

System.out.println ("Server output:" + str)

Buf.close ()

Client.close ()

}

}

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

Servers

Wechat

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

12
Report