XhstormR


On a dark desert highway Cool wind in my hair


Java Socket

Updated on 2017-02-16

  • Socket:属于 操作系统提供 的一个 API 接口,不属于 TCP/IP 协议栈,工作在应用层与传输层之间,由 协议地址 共同确定。
    • 协议:TCP(面向连接,可靠)、UDP(面向无连接,不可靠)。
    • 地址:
      • IP:IPv4(192.168.1.1)、IPv6。
      • 端口:[0,65535],其中 [0,1023] 为系统所保留。
  • Uniform Resource Locator(URL):统一资源定位器,表示 Internet 上某一 资源地址
    • http://blog.xhstormr.tk:80/post/jooq/index.html?abc=123&def=456#top
      • 协议名资源名 共同组成,中间用 冒号 隔开。

InetAddress

fun main(args: Array<String>) {
//    InetAddress.getByName("192.168.1.2")
//    InetAddress.getByName("DESKTOP-OPN10MH")
    val inetAddress = InetAddress.getLocalHost()
    println(inetAddress)
    println("计算机名:${inetAddress.hostName}")
    println("IP地址:${inetAddress.hostAddress}")
    println("IP地址(字节数组):${Arrays.toString(inetAddress.address)}")
}

----

DESKTOP-OPN10MH/192.168.1.2
DESKTOP-OPN10MH
IP192.168.1.2
IP[-64, -88, 1, 2]

URL

fun main(args: Array<String>) {
    val url1 = URL("http://blog.xhstormr.tk:80")
    val url2 = URL(url1, "/post/jooq/index.html?abc=123&def=456#top")
    println(url2)
    println("协议:${url2.protocol}")
    println("主机:${url2.host}")
    println("端口:${url2.port}")
    println("文件名:${url2.file}")
    println("文件路径:${url2.path}")
    println("相对路径(#):${url2.ref}")
    println("查询参数(?):${url2.query}")
}

----

http://blog.xhstormr.tk:80/post/jooq/index.html?abc=123&def=456#top
http
blog.xhstormr.tk
80
/post/jooq/index.html?abc=123&def=456
/post/jooq/index.html
(#)top
(?)abc=123&def=456

TCP

  • Socket
  • ServerSocket
线
----
fun main(args: Array<String>) {
    ServerSocket(4567).use {
        while (true) {
            Thread(A(it.accept())).start()     
        }
    }
}

class A(val socket: Socket) : Runnable {     线
    override fun run() {
        socket.use {
            println("运行线程:${Thread.currentThread()}")
            println("本地地址:${it.localAddress} 本地端口:${it.localPort}")
            println("远端地址:${it.inetAddress} 远端端口:${it.port}")
            val `in` = DataInputStream(it.inputStream)     
            val out = DataOutputStream(it.outputStream)     
            println("Receive:${`in`.readUTF()}")     
            out.writeUTF("Hello Client!")
        }     
    }     线
}

----

线Thread[Thread-0,5,main]
/127.0.0.1 4567
/127.0.0.1 54569
ReceiveHello Server!

----
fun main(args: Array<String>) {
    Socket("127.0.0.1", 4567).use {
        println("运行线程:${Thread.currentThread()}")
        println("本地地址:${it.localAddress} 本地端口:${it.localPort}")
        println("远端地址:${it.inetAddress} 远端端口:${it.port}")
        val `in` = DataInputStream(it.inputStream)     
        val out = DataOutputStream(it.outputStream)     
        out.writeUTF("Hello Server!")
        println("Receive:${`in`.readUTF()}")     
    }     
}     线

----

线Thread[main,5,main]
/127.0.0.1 54569
/127.0.0.1 4567
ReceiveHello Client!

UDP

  • DatagramPacket
  • DatagramSocket

----
fun main(args: Array<String>) {
    val data1 = ByteArray(1024)     
    val data2 = "Hello Client!".toByteArray()     
    val packet1 = DatagramPacket(data1, data1.size)     
    val packet2 = DatagramPacket(data2, data2.size, null, 0)     

    DatagramSocket(4567).use {
        while (true) {
            it.receive(packet1)     
            println("Receive:${String(packet1.data, 0, packet1.length)}")

            packet2.address = packet1.address
            packet2.port = packet1.port
            it.send(packet2)
        }
    }
}

----

ReceiveHello Server!

----
fun main(args: Array<String>) {
    val data1 = ByteArray(1024)     
    val data2 = "Hello Server!".toByteArray()     
    val packet1 = DatagramPacket(data1, data1.size)     
    val packet2 = DatagramPacket(data2, data2.size, InetAddress.getByName("127.0.0.1"), 4567)     

    DatagramSocket().use {
        it.send(packet2)
        it.receive(packet1)     
        println("Receive:${String(packet1.data, 0, packet1.length)}")
    }
}

----

ReceiveHello Client!
TOP