网络编程
使用InetAddress类代表IP,IP+端口号=Socket(网络套接字).
InetAddress.getByName(String),可以写ip也可以写域名.
//getByName(String) ip/域名
InetAddress address = InetAddress.getByName("127.0.0.1");
System.out.println("address = " + address);
InetAddress address1 = InetAddress.getByName("www.baidu.com");
System.out.println("address1 = " + address1);
//getHostName 获取主机名
System.out.println(address.getHostName());
//getHostAddress 获取主机地址
System.out.println(address1.getHostAddress());
OSI参考模型
UDP协议
UDP协议:
1.将数据源目的地封装成数据包,不需要建立连接.
2.每个数据包的大小限制在64kb内.
3.发送不管对方是否准备好,接收方收到也不确认,故不可靠.
4.可以广播发送.
5.发送数据结束时无需释放资源开销小,速度快.
TCP协议
TCP协议:
1.使用TCP协议前,须先建立TCP连接,形成传输数据通道.
2.传输前,采用"三次握手"方式,点对点通信是可靠的.
3.进行通信的两个应用程序: 客户端和服务端.
4.在连接中可以进行大数据量的传输.
传输完毕,需释放已建立的连接,效率低.
TCP三次握手
TCP四次挥手
TCP四次挥手: 双方都可以发起,通常由客户端发起.
在Java中使用
java中用DatagramSocket类代表UDP的发送者或接收者.
send()发送,receive()接收.
用Socket类代表TCP的客户端,用ServerSocket代表TCP的服务端.
getOutputStream(): 发送.
getInputStream(): 接收.
shutdownOutput(): 关闭发送通道,不然会阻塞.
以TCP为例,代码如下:
//TCP 客户端发送文件到服务器 服务器回传接受信息给客户端
public class TCPFileTest {
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
BufferedInputStream bis = null;
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 8899);
os = socket.getOutputStream();
bis = new BufferedInputStream(new FileInputStream("haha3.jpg"));
int len;
byte[] buffer = new byte[1024];
while ((len=bis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//发送完毕 关闭输出
socket.shutdownOutput();
//接受回传信息
is = socket.getInputStream();
int len1;
byte[] buffer1 =new byte[1024];
while ((len1=is.read(buffer1))!=-1){
System.out.println(new String(buffer1,0,len1));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
BufferedOutputStream bos = null;
try {
ss = new ServerSocket(8899);
socket = ss.accept();
is = socket.getInputStream();
bos = new BufferedOutputStream(new FileOutputStream("haha_tcp.jpg"));
int len;
byte[] buffer = new byte[1024];
while ((len=is.read(buffer))!=-1){
bos.write(buffer);
}
System.out.println("成功接受");
//回传数据
OutputStream os = socket.getOutputStream();
os.write(("我已经接受到你: "+socket.getInetAddress()+" 发的图片").getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
URL
Url(Uniform Resourse Location): 统一资源定位符.
java中用URL类表示URL.
常用方法:
getProtocol(): 获取协议名.
getHost(): 获取主机名.
getPort(): 获取端口.
getPath(): 获取文件路径.
getFile(): 获取文件名.
getQuery(): 获取查询名.
下载网络文件(前提Url是正确的网络文件地址):
openConnection(): 打开连接.
connect(): 连接.
getInputStream(): 获取输入流.