今日网络编程完结!!!

1.6 TCP(20-12-11)

客户端

  1. 连接服务器 Socket
  2. 发送消息

服务器

  1. 建立服务的端口ServerSocket
  2. 等待用户链接 accept
  3. 接受用户的消息
文件上传

服务器端

客户端

Tomcat

服务端

  • 自定义S
  • Tomcat服务器S

客户端

  • 自定义C
  • 浏览器B

1.7 UDP(20-12-11)

发短信:不用链接,需要知道对方地址!

发送端

//发送端代码

接收端

//接收端代码

完成实时聊天

//example

1.8 URL(20-12-11)

统一资源定位符:定位资源的,定位互联网上的某一个资源

DNS域名解析 www.baidu.com xxx.x..x...x

URL每个方法的输出

public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
        System.out.println(url.getProtocol());//协议
        System.out.println(url.getHost());//主机IP
        System.out.println(url.getPort());//端口
        System.out.println(url.getPath());//文件
        System.out.println(url.getFile());//全路径
        System.out.println(url.getQuery());//参数
    }
/*
http
localhost
8080
/helloworld/index.jsp
/helloworld/index.jsp?username=kuangshen&password=123
username=kuangshen&password=123
*/

使用URL下载网络资源

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLDown {
    public static void main(String[] args) throws Exception {
        //1.下载地址

        URL url = new URL("http://localhost:8080/qinjiang/SecurityFile.txt");//网络资源链接

        //2.连接到这个资源 HTTP
        HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
        InputStream inputStream=urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("SecurityFile.txt");//下载到本地的名称和文件类型

        byte[] buffer=new byte[1024];
        int len;
        while ((len=inputStream.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        fos.close();
        inputStream.close();
        urlConnection.disconnect();

    }
}
本人学习Java的一片私人空间……
原文地址:https://www.cnblogs.com/none-space/p/14122241.html