tonglin0325的个人主页

Java——Java网络

1.IP和InetAddress#

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.net.InetAddress;

public class InetAddress_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
InetAddress locAdd = null; //声明InetAddress对象
InetAddress remAdd = null; //声明InetAddress对象

locAdd = InetAddress.getLocalHost(); //得到本地的InetAddress对象
remAdd = InetAddress.getByName("www.baidu.com");

System.out.println("本机的IP地址:"+locAdd.getHostAddress());
System.out.println("远程的IP地址:"+remAdd.getHostAddress());
System.out.println("本机是否可达:"+locAdd.isReachable(5000));
}

}

2.URLConnection,URLEncoder和URLDecoder#

1.使用URL读取内容#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.awt.im.InputContext;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class URL_demo {

public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
URL url = new URL("http","10.108.84.220",8080,"/Hello/Hello.html"); //指定操作的URL
InputStream input = url.openStream(); //打开输入流,读取URL内容
Scanner scan = new Scanner(input); //实例化读取分隔符
scan.useDelimiter("\n"); //设置读取分隔符
while(scan.hasNext()){
System.out.println(scan.next());
}
}

}

 

2.取得URL的基本信息#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.net.URL;
import java.net.URLConnection;

public class URL_demo {

public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根

URL url = new URL("http://www.baidu.com");
URLConnection urlCon = url.openConnection(); //建立连接
System.out.println("内容大小:"+urlCon.getContentLength()); //取得内容大小
System.out.println("内容类型:"+urlCon.getContentType()); //取得内容类型
}

}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.net.URLDecoder;
import java.net.URLEncoder;

public class URL_demo {

public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根

String keyWord = "张三";
String encod = URLEncoder.encode(keyWord,"UTF-8"); //对内容进行编码
System.out.println("编码之后的内容:"+encod);
String decod = URLDecoder.decode(keyWord,"UTF-8"); //对内容进行编码
System.out.println("解码之后的内容:"+decod);
}

}

 输出

1
2
3
编码之后的内容:%E5%BC%A0%E4%B8%89
解码之后的内容:张三
  

3.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
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloServer_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
ServerSocket server = null; //声明ServerSocket对象
Socket client = null; //一个Socket对象表示一个客户端
PrintStream out = null; //声明打印流对象,以向客户端输出
server = new ServerSocket(8888); //表示服务器在8888端口上等待客户端的访问
System.out.println("服务器运行,等待客户端连接");
client = server.accept(); //程序阻塞,等待客户端连接
String str = "HelloWord"; //要向客户端输出信息
out = new PrintStream(client.getOutputStream()); //实例化打印流对象,输出信息
out.println(str);
out.close();
client.close();
server.close();
}

}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class HelloClient_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
Socket client = null; //声明Socket对象
client = new Socket("localhost",8888); //指定连接的主机和端口
BufferedReader buf = null; //声明BufferedReader对象,接受信息
//指定客户端的输入流
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = buf.readLine();
System.out.println("服务器输出的内容是"+str);
client.close();
buf.close();
}

}

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
ServerSocket server = null; //声明ServerSocket对象
Socket client = null; //一个Socket对象表示一个客户端
PrintStream out = null; //声明打印流对象,以向客户端输出
BufferedReader buf = null; //声明BufferedReader对象,接受信息
server = new ServerSocket(8888); //表示服务器在8888端口上等待客户端的访问
boolean f = true;
while(f){
System.out.println("服务器运行,等待客户端连接:");
client = server.accept(); //接收客户端连接
//得到客户端的输入信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
//实例化客户端的输出流
out = new PrintStream(client.getOutputStream());
boolean flag = true;
while(flag){
String str = buf.readLine(); //在此处不断地接收信息
if(str == null || "".equals(str)){
flag = false;
}else{
if("bye".equals(str)){
flag = false;
}else{
out.print("ECHO"+str);
}
}
}
out.close();
client.close();
}
server.close();
}
}

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class EchoClient_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
Socket client = null; //声明Socket对象
client = new Socket("localhost",8888); //指定连接的主机和端口
BufferedReader buf = null; //声明BufferedReader对象,接受信息
PrintStream out = null; //输出流,向服务器端发送信息
BufferedReader input = null; //声明BufferedReader对象
//从键盘接收数据
input = new BufferedReader(new InputStreamReader(System.in));
//向服务器端输出信息
out = new PrintStream(client.getOutputStream());
//接收服务器端输入信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
boolean flag = true;

while(flag){
System.out.print("输入信息:");
String str = input.readLine(); //从键盘接收数据
out.println(str);

if("bye".equals(str)){
flag = false;
}else{
String echo = buf.readLine();
System.out.println(echo);
}
}
client.close();
buf.close();
}
}

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

class EchoThreadServer implements Runnable{
private Socket client = null;

public EchoThreadServer(Socket client){
this.client = client;
}

@Override
public void run() {
// TODO 自动生成的方法存根
PrintStream out = null; //声明打印流对象,以向客户端输出
BufferedReader buf = null; //声明BufferedReader对象,接受信息

try{
//得到客户端的输入信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
//实例化客户端的输出流
out = new PrintStream(client.getOutputStream());
boolean flag = true;

while(flag){
String str = buf.readLine(); //在此处不断地接收信息
if(str == null || "".equals(str)){
flag = false;
}else{
if("bye".equals(str)){
flag = false;
}else{
out.print("ECHO"+str);
}
}
}
out.close();
client.close();
}catch(Exception e){
}
}
}

public class EchoThreadServer_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
ServerSocket server = null;
Socket client = null;
server = new ServerSocket(8888);
boolean f = true;
while(f){
System.out.println("服务器运行,等待客户端连接:");
client = server.accept();
new Thread(new EchoThreadServer(client)).start();
}
server.close();
}

}

4.UDP#

 UDP server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
DatagramSocket ds = null; //声明DatagramSocket对象
DatagramPacket dp = null; //声明DatagramPacket对象
ds = new DatagramSocket(3000);
String str = "HelloWord";
dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),9000);
System.out.println("发送信息");
ds.send(dp);
ds.close();
}

}

 UDP client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPClient_demo {

public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
DatagramSocket ds = null; //声明DatagramSocket对象
byte[] buf = new byte[1024]; //定义接收数据的字节数据
DatagramPacket dp = null; //声明DatagramPacket对象
ds = new DatagramSocket(9000); //此客户端在9000端口监听
dp = new DatagramPacket(buf,1024); //指定接收数据的长度为1024
System.out.println("等待接收数据");
ds.receive(dp); //接收数据
String str = new String(dp.getData(),0,dp.getLength())+" from"
+dp.getAddress().getHostAddress()+" : "+dp.getPort(); //接收数据
System.out.println(str); //输出数据
ds.close();
}

}