Study No. 20165334 "Java Program Design" 9th Week study summary Textbook Learning content Summary 13 Chapter Knowledge Summary get Address 1. Get the address of a host on the Internet
- can use the static method of the InetAddress class Getbyname (String s);
The
-
Obtains a InetAddress object that contains the domain name and IP address of the host address that represents the information it contains in the following format:
www.sina.com.cn/202.108.37.40
1, getting the address of the local machine
Use the static method of the InetAddress class Getlocalhost () to obtain a InetAddress object that contains the domain name and IP address of the local machine.
2, Client socket
establishes a socket object to connect to the server: try{socket mysocket=new socket ("http://192.168.0.78", 1880);} catch (IOException e) {} Mysocket-related method getInputStream () obtains an input stream Getoutputstream () obtains an output stream with getInputStream () The resulting input flows to another DataInputStream data stream with the output from Getoutputstream () to the other DataOutputStream traffic on the
3, The ServerSocket object and the server-side socket
server must establish a ServerSocket object that connects the client's socket object to the server-side socket object to achieve the purpose of the connection. Establish ServerSocket object: try{serversocket serverforclient =new serversocket (2010); } catch (IOException e) {} uses the method accept () to connect the client's socket to the server-side socket, as shown in the following code: try{socket sc= serverforclient. Accept (); } catch (IOException e) {} The so-called "receive" client's socket connection is the accept () method that returns a socket object that is connected to the client socket object.
4, send packet
1. Package the data with the Datagrampacket class, which is to create an object, called a packet, with the Datagrampacket class. Create the packets to be sent using the following two construction methods of Datagrampacket:
- Datagrampacket (byte data[],int length,inetaddtress address,int Port)
- Datagrampack (byte data[],int offset,int length,inetaddtress address,int Port)
2. Use the Datagramsocket class without parameters to construct a method: Datagramsocket () Creates an object that is responsible for sending the packet. For example:
- Datagramsocket mail_out=new Datagramsocket ();
Mail_out.send (Data_pack);
Receiving packets
- First, create an object with Datagramsocket's other construction method datagramsocket (int port), where the parameters must be the same as the port number of the packet to be received. For example, if the sender sends a packet with a port of 5666, then create the Datagramsocket object as follows:
Datagramsocket mail_in=new Datagramsocket (5666);
- The object mail_in then accepts the packet using method receive (Datagrampacket pack).
3. Use another construction method of the Datagrampack class: Datagrampack (Byte data[],int length) to create a packet to receive the packet, for example:
byte data[]=new byte[100];
int length=90;
Datagrampacket pack=new Datagrampacket (data,length);
Mail_in.receive (Pack);
The packet pack will receive data with length bytes.
Code Hosting
(run result of statistics.sh script)
Learning progress Bar
|
lines of code (new/cumulative) |
Blog Volume (Add/accumulate) |
Learning Time (new/cumulative) |
Important Growth |
| Goal |
5000 rows |
30 Articles |
400 hours |
|
| First week |
200/200 |
2/2 |
20/20 |
|
| Second week |
300/500 |
2/4 |
18/38 |
|
| Third week |
500/1000 |
3/7 |
22/60 |
|
| Week Four |
300/1300 |
2/9 |
30/90 |
|
| Week Five |
1000/1300 |
2/9 |
30/90 |
|
| Week Six |
900/1300 |
2/9 |
30/90 |
|
|
| Seventh Week |
600/1300 |
2/9 |
30/90 |
|
| Eighth Week |
900/1300 |
2/9 |
30/90 |
|
|
| Nineth Week |
13016/1300 |
2/9 |
30/90 |
|
Resources
20165334 Java Programming 9th Week of study summary