1. 程式人生 > 實用技巧 >初識網路程式設計

初識網路程式設計

前言

1.埠表示計算機的一個程式的程序:
2.不同的程序有不同的埠號,用來區分軟體
3.理論上規定了0~65536
4.TCP,UDP:65536*2 Tcp:80 udp:80,單個協議下,埠不能衝突
5.埠分類:
共有埠0~1023
HTTP:80 超文字傳輸協議
HTTPS:443 超文字傳輸安全協議,基於ssl的http協議
FTP:21 ,檔案傳輸協議
Telent:23 遠端傳輸協議
程式註冊埠:1024~49151
Tomcat:8080
MySQL:3306
Oracle:1521
動態,私有:49152~65535

簡單實現網路通訊
package com.cl.lesson01;


import java.net.InetAddress;
import java.net.UnknownHostException;

//封裝IP,InetAddress
public class TestInterAddress {
    public static void main(String[] args) {
        try {
            InetAddress interAddress1=InetAddress.getByName("127.0.0.1");
            System.out.println(interAddress1);
            InetAddress interAddress3
=InetAddress.getByName("localhost"); System.out.println(interAddress3); InetAddress interAddress4=InetAddress.getLocalHost(); System.out.println(interAddress4); //查詢網站IP地址 InetAddress interAddress2=InetAddress.getByName("www.baidu.com"); System.out.println(interAddress2);
//常用方法 System.out.println(interAddress2.getAddress()); System.out.println(interAddress2.getCanonicalHostName());//規範的名字 System.out.println(interAddress2.getHostAddress());//ip System.out.println(interAddress2.getHostName());//域名 }catch (UnknownHostException e){ e.printStackTrace(); } } } package com.cl.lesson01; import java.net.InetSocketAddress; //封裝計算機的ip+埠號,InetSocketAddress public class TestInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName());//地址 System.out.println(socketAddress.getPort());// } }