TCP网络编程

1.前置基础知识:

一、网络编程中的两个主要问题:
1.如何准确定位网络上的一台或多台主机,定位主机上的特定应用
2.找到主机后如何进行高效的传输

二、网络编程中的两个要素:
1.提供IP和端口号解决主机寻找问题
2.提供网络协议(如TCP/IP)实现高效传输

三、通信要素一:IP和端口号
1.IP:唯一标识Internet上的计算机
2.在Java中使用InetAddress代替IP
3.IP的分类:IPv4和IPv6 万维网和局域网
4.域名:www.baidu.com www.mi.com
5.本地回路地址:127.0.0.1 ,对应localhost
6.实例化InetAddress的两个方法:getByName(String host),getLocalHost()
两个常用方法:getHostName()/getHostAddress()
7.端口号:正在计算机上运行的进程
要求:不同进程的端口号不同
范围:0——65535
8.端口号与IP构成一个网络构接字

代码的有关测试:
 1 public class InetAddressTest {  2     public static void main(String[] args) {  3         try {  4             InetAddress inet1 = InetAddress.getByName(192.168.10.14);  5             System.out.println(inet1);  6             //获取域名的IP地址  7             InetAddress inet2 = InetAddress.getByName(www.baidu.com);  8             System.out.println(inet2);  9             //获取本机的IP地址 10             InetAddress inet3 = InetAddress.getLocalHost(); 11             System.out.println(inet3); 12             System.out.println(inet2.getHostName()); 13             System.out.println(inet2.getHostAddress()); 14         } catch (UnknownHostException e) { 15             throw new RuntimeException(e); 16         } 17     } 18 }

 

 

2.实现TCP的网络编程:

分为两个部分实现:
1.客户端
(1).创建socket对象,指明IP和端口号
(2).获取一个输出流(getOutputStream()),用于输出数据
(3).写出数据操作(write())
(4).关闭操作

2.服务器
(1).创建服务端的ServerSocket,指明服务端的端口号
(2).调用accept()方法来接受客户端的socket
(3).获取输入流(getInputStream())
(4).读取输入流中的数据(使用ByteArrayOutputStream()来进行实现)
(5).关闭操作

代码示例:
 1 public class TCPTest1 {  2     public static void client()  {//客户端  3         Socket socket = null;  4         OutputStream os = null;  5         try {  6             InetAddress inet = InetAddress.getByName(127.0.0.1);  7             //1.创建socket对象,指明服务端的IP和端口号  8             socket = new Socket(inet,8899);  9             //2.获取一个输出流,用于输出数据 10             os = socket.getOutputStream(); 11             //3.写出数据的操作 12             os.write(你好,我是客户端A.getBytes(StandardCharsets.UTF_8)); 13         } catch (IOException e) { 14             e.printStackTrace(); 15         } finally { 16             //4.资源的关闭 17             if (os != null) { 18                 try { 19                     os.close(); 20                 } catch (IOException e) { 21                     e.printStackTrace(); 22                 } 23             } 24             if (socket != null) { 25                 try { 26                     socket.close(); 27                 } catch (IOException e) { 28                     e.printStackTrace(); 29                 } 30             } 31         } 32     } 33  34     public static void server()  {//服务端 35         ServerSocket ss = null; 36         Socket socket = null; 37         InputStream is = null; 38         ByteArrayOutputStream bos = null; 39         try { 40             //1.创建服务器端的ServerSocket,指明自己的端口号 41             ss = new ServerSocket(8899); 42             //2.调用accept()方法来接收客户端的socket 43             socket = ss.accept(); 44             //3.获取输入流 45             is = socket.getInputStream(); 46             //4.读取输入流中数据 47             bos = new ByteArrayOutputStream(); 48             byte[] str = new byte[5]; 49             int len; 50             while ((len = is.read(str)) != -1) { 51                 bos.write(str,0,len); 52             } 53             System.out.println(bos.toString()); 54         } catch (IOException e) { 55             e.printStackTrace(); 56         } finally { 57             //5.关闭资源 58             if (bos != null) { 59                 try { 60                     bos.close(); 61                 } catch (IOException e) { 62                     e.printStackTrace(); 63                 } 64             }{ 65                 try { 66                     is.close(); 67                 } catch (IOException e) { 68                     e.printStackTrace(); 69                 } 70             } 71             if (is != null) 72             if (socket != null) { 73                 try { 74                     socket.close(); 75                 } catch (IOException e) { 76                     e.printStackTrace(); 77                 } 78             } 79             if (ss != null) { 80                 try { 81                     ss.close(); 82                 } catch (IOException e) { 83                     e.printStackTrace(); 84                 } 85             } 86         } 87     } 88 }
 1 public class TCPTest2 {  2   3     public void client() throws IOException {  4         Socket socket = new Socket(InetAddress.getByName(127.0.0.1),9090);  5         OutputStream os = socket.getOutputStream();  6         FileInputStream fis = new FileInputStream(new File(beauty.png));  7         byte[] buffer = new byte[1024];  8         int len;  9         while ((len = fis.read()) != -1) { 10             os.write(buffer,0,len); 11         } 12         socket.shutdownOutput();//客户端不在进行数据输出 13         //接受服务端发送的数据 14         InputStream is = socket.getInputStream(); 15         ByteArrayOutputStream bos = new ByteArrayOutputStream(); 16         byte[] bufferr = new byte[20]; 17         int len1; 18         while ((len1 = is.read(buffer)) != -1) { 19             bos.write(bufferr,0,len1); 20         } 21         System.out.println(bufferr.toString()); 22  23         fis.close(); 24         os.close(); 25         socket.close(); 26     } 27  28     public void server() throws IOException { 29         ServerSocket ss = new ServerSocket(9090); 30         Socket socket = ss.accept(); 31         InputStream is = socket.getInputStream(); 32         FileOutputStream fos = new FileOutputStream(new File(beauty.png)); 33         byte[] buffer = new byte[1024]; 34         int len; 35         while ((len = is.read(buffer)) != -1) { 36             fos.write(buffer,0,len); 37         } 38         //服务端给予客户端反馈 39         OutputStream os = socket.getOutputStream(); 40         os.write(已收到信息.getBytes(StandardCharsets.UTF_8)); 41  42         fos.close(); 43         is.close(); 44         socket.close(); 45         ss.close(); 46     } 47 }