0%

java-Day14——网络

InetAddress的使用

作用

InetAddress类的一个实例就代表一个具体的ip地址

实例化方式

InetAddress getByName(String host):获取指定ip对应的InetAddress的实例

InetAddress getLocalHost():获取本地ip对应的InetAddress的实例

常用方法

getHostName():

getHostAddress()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class netTest {
@Test
public void test(){
try{
//1.实例化
//getByName(String host):获取指定ip对应的InetAddress的实例
InetAddress inet1 = InetAddress.getByName("192.168.23.31");
System.out.println(inet1);

InetAddress inet2 = InetAddress.getByName("www.baidu.com");
System.out.println(inet2);

//getLocalHost():获取本地Ip对应的InetAddress的实例
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3);

//2.两个常用方法
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());

}catch (UnknownHostException e){
e.printStackTrace();
}

}
}

Socket类

TCP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class TCPTest2 {
@Test
public void client(){
Socket socket = null;
FileInputStream fis = null;
OutputStream os = null;
try {
//1.创建Socket
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9090;
socket = new Socket(inetAddress, port);
//2.创建File的实例、FileInputStream的实例
File file = new File("xx.jpg");
fis = new FileInputStream(file);
//3.通过Socket获取输出流
os = socket.getOutputStream();
//读写数据
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//4.接收来自于服务端的数据
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[5];
int len1;
while((len1 = is.read(buffer))!=-1){
baos.write(buffer1,0,len);
}
System.out.println(baos.toString());
//客户端不再继续发送数据
socket.shutdownOutput();

} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//5.关闭Socket和相关流
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
//创建ServerSocket
int port = 9090;
serverSocket = new ServerSocket();
//2.接受来自于客户端的socket:accept(_
socket = serverSocket.accept();
//3.通过Socket获取一个输入流
is = socket.getInputStream();
//4.创建File类的实例、FileOutputStream
File file = new File("xxx_copy.jpg");
fos = new FileOutputStream(file);
//5.读写过程
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
//6.服务端发送数据给客户端
OutputStream os = socket.getOutputStream();
os.write("pic recevied".getBytes());

} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//6关闭相关Socket和流
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
serverSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
}

URL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class UrlTest {
@Test
public void test(){
String str = "http://www.google.com";
try {
URL url = new URL(str);

System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getFile());

} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
-------------本文结束感谢您的阅读-------------