博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java网络编程简介
阅读量:5102 次
发布时间:2019-06-13

本文共 2330 字,大约阅读时间需要 7 分钟。

1.host

任何一个可以上网的设备都是一个host

host包括ip地址 主机名字和域名

ip地址

唯一指定网络上的一台机器(世界级的身份证号)

ipv4 ipv6

主机名字

多用在本地,局域网内(姓名,一个区域内唯一)

域名

多用在世界级别的(江苏省南京市一样的)

2.网络连接的两种角色

服务端和客户端

服务端的端口: 如223.3.68.116:8080中的8080

一个服务器可能拥有很多服务的时候用于分辨服务类型
0~1023被征用为系统专用的

3.连接

建立在两个host两个端口的之间的联系

连接

4.java网络编程

java。net.*

InetAddress简介

  1. Abstraction of a network address
  2. Allows an address to be obtained from a host name and vice versa
  3. Is immutable (is a read-only object)

Static construction and using( using a factory method ) 

InetAddress的构造函数不是公开的(public),所以需要通过它提供的静态方法来获取,有以下的方法:

static InetAddress[] getAllByName(String host)

static InetAddress getByAddress(byte[] addr)
static InetAddress getByAddress(String host,byte[] addr)
static InetAddress getByName(String host)
static InetAddress getLocalHost()

InetAddress address=InetAddress.getByName("www.baidu.com");

Some useful methods:

  • String getHostName() //Gives you the host name (for example “www.sun.com”)
  • String getHostAddress()//Gives you the address (for example “192.18.97.241”)
  • InetAddress getLocalHost()
  • InetAddress[] getAllByName(String hostName)

Socket

Two Types of Socket

ServerSocket

java.net.ServerSocket (is used by servers so that they can accept incoming connections)

A server is a piece of software which advertises

and then provides some service on request 
Listens on well-known port for incoming connections
Creates a dynamically allocated Socket for each newly established connection
Maintains a queue to ensure that prospective clients are not lost
Construction:

ServerSocket(int port, int backlog) Allows up to backlog requests to queue waiting 

for the server to deal with them

*some useful methods:

Socket accept() //Blocks waiting for a client to attempt to establish a connection

void close() //Called by the server when it is shutting down to ensure that any resources are
deallocated

java.net.Socket (is used by clients who wish to establish a connection to a (remote) server)

A client is a piece of software (usually on a different machine) which makes use of some service

Socket

Provides access to network streams Bi-directional communication between sender and

receiver
Can be used to connect to a remote address and port
*constructor:

Socket(String remoteHost, int port)//Also used to accept an incoming connection (see

ServerSocket)

转载于:https://www.cnblogs.com/goldenhair/p/3488069.html

你可能感兴趣的文章
linux 7- - watch,free,mpstat,vmstat,iostat,pidstat,df,du
查看>>
jQuery--捕获键盘敲击
查看>>
Mail.Ru Cup 2018 Round 3
查看>>
计算题:装箱问题
查看>>
mac os 下安装mysql
查看>>
第八章 进程控制
查看>>
form
查看>>
POJ--3268 Silver Cow Party(最短路)
查看>>
好东西
查看>>
loadrunner基础学习笔记二
查看>>
【Qt开发】Qt中显示图像的两种方法对比
查看>>
android获取内置和外置SD卡路径 - z
查看>>
常见 HTTP/FTP/WebSocket 错误代码大全 - 转
查看>>
华为lab-rs-v1-1.5_smart link
查看>>
String及其常用API
查看>>
php笔记--php安装
查看>>
封装特效记录--持续更新
查看>>
使用VIsio绘制E-R图
查看>>
Linux文件类型 扩展名的作用
查看>>
MySQL常用命令基础操作
查看>>