Java笔记--网络编程
本站字数:108k    本文字数:1.8k    预计阅读时长:9min    访问次数:次
Java笔记–网络编程
一、 网络編程中有两个主要的问题:
- 如何准确地定位网络上一台或多台主机:定位主机上的特定的应用
- 到主机后如何可靠高效地进行数据传输
二、 网络编程的两个要素
- 对应问题一:IP和端口号
- 对应问题二:提供网络通信协议:TCP/IP参考模型
通信要素一:IP和端口号
- IP:唯一的表示Internet上的计算机(通信实体)
- 在Java中使用InetAdress类代表IP
- IP分类:IPv4和IPv6;万维网和局域网
- 域名
- 本地回路地址:127.0.0.1  localhost
- 如何实例化InetAdress:两个方法:
- getByName(String host)
- getLocalHost()
 
- 获取域名,获取主机地址
- getHostName()
- getHostAdress()
 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | public void test() {
 try {
 InetAddress inet1 = InetAddress.getByName("192.168.1.103");
 InetAddress inet1_1 = InetAddress.getByName("tplogin.cn");
 InetAddress inet2 = InetAddress.getByName("www.baidu.com");
 InetAddress inet3 = InetAddress.getByName("localhost");
 InetAddress inet3_1 = InetAddress.getLocalHost();
 
 System.out.println(inet1);
 System.out.println(inet1_1);
 System.out.println(inet2);
 System.out.println(inet3);
 System.out.println(inet3_1);
 
 System.out.println(inet2.getHostName());
 System.out.println(inet2.getHostAddress());
 } catch (UnknownHostException e) {
 e.printStackTrace();
 }
 
 }
 
 | 
- 端口号:端口号标识正在计算机上运行运行的进程(程序)
- 不用的进程有不同的端口号
- 被规定为一个16位的整数0~65535。
- 端口分类:
- 公认端口:0~1023。被预先定义的服务通信占用(如:HTTP占用端口80,FTP占用端口21,Telnet占用端口23)
- 注册端口:1024-49151。分配给用户进程或应用程序。(如:Tomcat占用端口8080,MySQL占用端口3360,Oracle占用端口1521等)
- 动态/私有端口:49152-65535
 
- 端口号与IP地址的组合得出一个网络套接字:Socket
 
通信要素二:网络通信协议
TCP/IP协议簇
- 传输控制协议——-TCP 
- 用户数据报协议—UDP 
- TCP协议:
 ➢ 使用TCP协议前,须先建立TCP连接,形成传输数据通道
 ➢ 传输前,采用“三次握手”方式,点对点通信,是可靠的
 ➢ TCP协议进行通信的两个应用进程:客户端、服务端。
 ➢ 在连接中可进行大数据量的传输
 ➢ 传输完毕,需释放已建立的连接,效率低
 
- UDP协议
 ➢ 将数据、源、目的封装成数据包,不需要建立连接
 ➢ 将每个数据包的大小限制在64K内
 ➢发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
 ➢可以广播发送
 ➢发送数据结束时无需释放资源,开销小,速度快
 
TCP的网络编程
客户端发送文本到服务端
| 12
 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
 
 | @Test
 public void client() {
 Socket socket = null;
 OutputStream os = null;
 try {
 InetAddress inet  = InetAddress.getByName("192.168.1.103");
 socket = new Socket(inet, 12356);
 
 os = socket.getOutputStream();
 os.write("我是WZL".getBytes());
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 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 sock = null;
 InputStream is = null;
 ByteArrayOutputStream baos = null;
 try {
 ss = new ServerSocket(12356);
 sock = ss.accept();
 
 is = sock.getInputStream();
 
 
 
 
 
 
 
 
 
 
 baos = new ByteArrayOutputStream();
 byte[] buffer = new byte[5];
 int len;
 while ((len = is.read(buffer)) != -1){
 baos.write(buffer, 0, len);
 }
 
 System.out.println(baos.toString());
 
 System.out.println("收到了来自:" + sock.getInetAddress().getHostAddress() + "的消息");
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (baos != null) {
 try {
 baos.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (is != null) {
 try {
 is.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (sock != null) {
 try {
 sock.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (ss != null) {
 try {
 ss.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 | 
客户端发送文件到服务端
| 12
 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
 
 | @Testpublic void client() {
 Socket sock = null;
 OutputStream os = null;
 FileInputStream fis = null;
 try {
 
 InetAddress inet = InetAddress.getByName("192.168.1.103");
 sock = new Socket(inet, 9090);
 
 os = sock.getOutputStream();
 fis = new FileInputStream(new File("IOTest/pic1.png"));
 
 
 byte[] buffer = new byte[1024];
 int len;
 while ((len = fis.read(buffer)) != -1){
 os.write(buffer, 0, len);
 }
 
 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (fis != null) {
 try {
 fis.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (os != null) {
 try {
 os.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (sock != null) {
 try {
 sock.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 @Test
 
 public void server() {
 ServerSocket ss = null;
 Socket sock = null;
 InputStream is = null;
 FileOutputStream fos = null;
 try {
 ss = new ServerSocket(9090);
 sock = ss.accept();
 is = sock.getInputStream();
 fos = new FileOutputStream(new File("~/IOTest/pic_trans.png"));
 
 byte[] buffer = new byte[1024];
 int len;
 while ((len = is.read(buffer)) != -1){
 fos.write(buffer, 0, len);
 }
 System.out.println("收到文件,来自:" + sock.getInetAddress().getHostAddress());
 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (fos != null) {
 try {
 fos.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (is != null) {
 try {
 is.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (sock != null) {
 try {
 sock.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (ss != null) {
 try {
 ss.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 | 
完成发送以后服务端返回值
尤其注意IO是阻塞式的,如果不停止流那么流不会停止的,需要sock.shutdownOutput();来停止阻塞
| 12
 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
 113
 114
 115
 116
 117
 118
 
 | @Testpublic void client() {
 Socket sock = null;
 OutputStream os = null;
 FileInputStream fis = null;
 try {
 
 InetAddress inet = InetAddress.getByName("192.168.1.103");
 sock = new Socket(inet, 9090);
 
 os = sock.getOutputStream();
 fis = new FileInputStream(new File("IOTest/pic1.png"));
 
 
 byte[] buffer = new byte[1024];
 int len;
 while ((len = fis.read(buffer)) != -1){
 os.write(buffer, 0, len);
 }
 
 sock.shutdownOutput();
 
 
 InputStream is = sock.getInputStream();
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
 byte[] bufferr = new byte[5];
 int len1;
 while ((len1 = is.read(bufferr)) != -1){
 baos.write(bufferr, 0, len1);
 }
 System.out.println(baos.toString());
 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (fis != null) {
 try {
 fis.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (os != null) {
 try {
 os.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (sock != null) {
 try {
 sock.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 @Test
 
 public void server() {
 ServerSocket ss = null;
 Socket sock = null;
 InputStream is = null;
 FileOutputStream fos = null;
 try {
 ss = new ServerSocket(9090);
 sock = ss.accept();
 is = sock.getInputStream();
 fos = new FileOutputStream(new File("IOTest/pic_trans.png"));
 
 
 byte[] buffer = new byte[1024];
 int len;
 while ((len = is.read(buffer)) != -1){
 fos.write(buffer, 0, len);
 }
 System.out.println("收到文件,来自:" + sock.getInetAddress().getHostAddress());
 
 
 OutputStream os = sock.getOutputStream();
 os.write("文件接收成功".getBytes());
 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (fos != null) {
 try {
 fos.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (is != null) {
 try {
 is.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (sock != null) {
 try {
 sock.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 if (ss != null) {
 try {
 ss.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 | 
UDP网络编程
| 12
 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
 
 | @Test
 public void sender() {
 DatagramSocket socket = null;
 try {
 InetAddress inet = InetAddress.getByName("192.168.1.103");
 socket = new DatagramSocket();
 
 String str = "我是UDP方式发送的这条信息";
 byte[] data = str.getBytes();
 DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
 
 socket.send(packet);
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (socket != null)
 socket.close();
 }
 }
 
 @Test
 
 public void receiver() {
 DatagramSocket socket = null;
 try {
 socket = new DatagramSocket(9090);
 
 byte[] buffer = new byte[1024];
 DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length);
 
 socket.receive(dp);
 System.out.println(new String(dp.getData(), 0, dp.getData().length));
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (socket != null)
 socket.close();
 }
 }
 
 |