20165304 "Java Programming" ninth week learning summary

Textbook learning content summary

URL class

URL类是java.net包中的一个重要的类,URL的实例封装着一个统一资源定位符,使用URL创建对象的应用程序称作客户端程序。
一个URL对象通常包含最基本的三部分信息:协议、地址、资源。

构造方法:

  ```public URL(String spec) throws MalformedURLException   

public URL(String protocol, String host,String file) throws MalformedURLException```

读取URL中的资源:URL对象调用```InputStream openStream()```

InetAdress class

地址的表示:域名和IP地址
获取Internet上主机的地址:
1.可以使用InetAddress类的静态方法getByName(String s);
2.获得一个InetAddress对象,该对象含有主机地址的域名和IP地址,该对象用如下格式表示它包含的信息:

www.sina.com.cn/202.108.37.40
Get the address of the local machine
You can use the static method getLocalHost() of the InetAddress class to get an InetAddress object, which contains the domain name and IP address of the local machine.

socket

与mysocket相关的方法
getInputStream()获得一个输入流
getOutputStream()获得一个输出流
用getInputStream()得到的输入流接到另一个DataInputStream数据流上
用getOutputStream()得到的输出流接到另一个DataOutputStream数据流上

ServerSocket对象与服务器端套接字:
1.建立ServerSocket对象:

  ```try{  ServerSocket  serverForClient =new ServerSocket(2010);
  }
  catch(IOException e){}``` 

2. Use the method accept() to connect the client's socket to the server-side socket

    ```try{  Socket sc= serverForClient .accept();
    }
   catch(IOException e){}```  

3. The so-called "receiving" client's socket connection means that the accept() method will return a Socket object connected to the client's Socket object.

使用多线程技术
为了防止堵塞线程,服务器端收到一个客户的套接字后,就应该启动一个专门为该客户服务的线程。

UDP datagram

基于UDP通信的基本模式是:
1将数据打包,称为数据包(好比将信件装入信封一样),然后将数据包发往目的地。
2.接受别人发来的数据包(好比接收信封一样),然后查看数据包中的内容。



发送数据包
1.用DatagramPacket的以下两个构造方法创建待发送的数据包:

DatagramPacket(byte data[],int length,InetAddtress address,int port) DatagramPack(byte data[],int offset,int length,InetAddtress address,int port)

2. Use the constructor without parameters of the DatagramSocket class: DatagramSocket() to create an object that is responsible for sending data packets. E.g:

DatagramSocket mail_out=new DatagramSocket(); mail_out.send(data_pack);

接收数据包
1.用DatagramSocket的另一个构造方法DatagramSocket(int port) 创建一个对象,其中的参数必须和待接收的数据包的端口号相同。
2.对象mail_in使用方法receive(DatagramPacket pack)接受数据包。
3.用DatagramPack类的另外一个构造方法: DatagramPack(byte data[],int length)创建一个数据包,用于接收数据包

broadcast datagram

Java Remote Invocation (RMI)

code hosting

learning perception

This week, I learned a Java class dedicated to network programming, and I have learned a lot from the computer network class.

learning progress

Completed more than 1200 lines of code, 16 blogs, 163 hours of study time

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325096713&siteId=291194637