网络编程socket

 1 /**
 2  * 问题1:IP与端口号
 3  * 问题2:网络通信协议,TCP/IP模型
 4  *
 5  * 问题1:IP与端口号:
 6  * 1:IP 唯一的标识internet上的计算机
 7  * 2:在java中使用InetAddress类代表IP
 8  * 3:IP分为IPv4与IPv6
 9  * 4:域名:输入域名www.baidu.com然后通过DNS域名解析系统进行解析得到IP地址
10  * 5:本地回路地址:127.0.0.1 对应着localhost
11  * **********
12  * 6:获取InetAddress类实例的方法:
13  *    1调用getByName返回一个实例
14  *    2:调用getLocalHost返回一个实例
15  * 7:端口号:正在计算机上运行的进程
16  *  要求:不同的进程有不同的端口号
17  *  范围:被规定为一个16位的整数0-65535
18  * 8:端口号与IP地址组合得到一个网络套接字:Socket
19  * **************
20  * 问题2:网络通信协议
21  * TCP/IP协议:
22  * 传输层有两个很重要的协议:TCP/UDP
23  * 传输控制协议:TCP:(Transmission Control Protocol)
24  * 用户数据协议UDP:(User Datagram Protocol)
25  * ***************
26  *
27  *
28  */
29 public class InterNetTest {
30     @Test
31     public void test(){
32         try {
33             InetAddress inetAddress= InetAddress.getByName("192.168.10.14");
34             //获取本机IP
35             System.out.println(InetAddress.getLocalHost());
36             //getHostName:获取主机名
37             System.out.println(inetAddress.getHostName());
38             //getHostAddress:获取主机地址
39             System.out.println(inetAddress.getHostAddress());
40             //System.out.println(inetAddress);
41         } catch (UnknownHostException e) {
42             e.printStackTrace();
43         }
44     }
45 }

对于TCP/IP协议

创建一个客户端与服务器端,进行文件的传输

 1 **
 2  * TCP例题
 3  * 1:将客户端的信息传到服务器端
 4  *
 5  *
 6  */
 7 public class TCPTest {
 8     @Test//客户端
 9     public void client(){
10         Socket socket= null;
11         OutputStream stream= null;
12         try {
13             //1:首先创建一个socket对象,指明服务器的IP与端口号,
14             // 创建socket对象之前需要创建一个InetAddress类的对象作为形参传入socket构造器中
15             InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
16             socket = new Socket(inetAddress,8899);
17             //2:获取输出流,用于输出数据
18             stream = socket.getOutputStream();
19             //3:写出数据
20             stream.write("你好,你叫什么名字".getBytes());
21         } catch (IOException e) {
22             e.printStackTrace();
23         } finally {
24             try {
25                 if (stream!=null)
26                 stream.close();
27             } catch (IOException e) {
28                 e.printStackTrace();
29             }
30             try {
31                 if (socket!=null)
32                 socket.close();
33             } catch (IOException e) {
34                 e.printStackTrace();
35             }
36         }
37 
38     }
39     @Test//服务器端
40     public void server(){
41         ServerSocket socket1= null;
42         Socket socket= null;
43         InputStream inputStream= null;
44         ByteArrayOutputStream by= null;
45         try {
46             //1:创建服务器端的ServerSocket对象,指明自己的端口号
47             socket1 = new ServerSocket(8899);
48             //2:调用accept方法,表示接受来自于客户端的socket
49             socket = socket1.accept();
50             //3:获取输入流
51             inputStream = socket.getInputStream();
52             //4;读取输入流
53             by = new ByteArrayOutputStream();
54             byte[]bytes=new byte[5];
55             int len;
56             while ((len=inputStream.read(bytes))!=-1){
57                 by.write(bytes,0,len);
58             }
59                 System.out.println(by.toString());
60         } catch (IOException e) {
61             e.printStackTrace();
62         } finally {
63             try {
64                 if (by!=null)
65                 by.close();
66             } catch (IOException e) {
67                 e.printStackTrace();
68             }
69             try {
70                 if (inputStream!=null)
71                 inputStream.close();
72             } catch (IOException e) {
73                 e.printStackTrace();
74             }
75             try {
76                 if (socket!=null)
77                 socket.close();
78             } catch (IOException e) {
79                 e.printStackTrace();
80             }
81             try {
82                 if (socket1!=null)
83                 socket1.close();
84             } catch (IOException e) {
85                 e.printStackTrace();
86             }
87         }
88 //不建议这样写,可能会出现乱码
89 //        byte[]bytes=new byte[1024];
90 //        int len;
91 //        while ((len=inputStream.read(bytes))!=-1){
92 //            String str=new String(bytes,0,len);
93 //            System.out.println(str);
94 //        }
95     }
96 }

例题

  1 /**
  2  * 例题2:客户端发送文件给服务器端,服务器端将文件保存在本地,图片传输完成之后,服务器端发送信息给客户端
  3  *
  4  */
  5 public class TCPTest2 {
  6     @Test//客户端
  7     public void client(){
  8         Socket socket= null;//1创建socket对象,建立端口号,与IP地址
  9         OutputStream stream= null;//2获取输出流
 10         BufferedInputStream bf= null;
 11         try {
 12             InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
 13             socket = new Socket(inetAddress,8800);
 14             stream = socket.getOutputStream();
 15             bf = new BufferedInputStream(new FileInputStream(new File("8.jpeg")));
 16             //将图片文件使用缓冲流读取
 17             byte[]bytes=new byte[1024];
 18             int len;
 19             while ((len=bf.read(bytes))!=-1){
 20                 stream.write(bytes,0,len);//首先将文件读取到bf中然后将bf中的数据写入到stream中
 21             }
 22             socket.shutdownOutput();
 23             //接受发过来的信号
 24             InputStream inputStream=socket.getInputStream();
 25             ByteArrayOutputStream by=new ByteArrayOutputStream();
 26             byte[]bytes1=new byte[1024];
 27             int leng;
 28             while ((leng=inputStream.read(bytes1))!=-1){
 29                 by.write(bytes1,0,leng);
 30             }
 31             System.out.println(by.toString());
 32             by.close();
 33             inputStream.close();
 34         } catch (IOException e) {
 35             e.printStackTrace();
 36         } finally {
 37             try {
 38                 if (bf!=null)
 39                 bf.close();
 40             } catch (IOException e) {
 41                 e.printStackTrace();
 42             }
 43             try {
 44                 if (socket!=null)
 45                 socket.close();
 46             } catch (IOException e) {
 47                 e.printStackTrace();
 48             }
 49             try {
 50                 if (stream!=null)
 51                 stream.close();
 52             } catch (IOException e) {
 53                 e.printStackTrace();
 54             }
 55         }
 56 
 57 
 58 
 59     }
 60     @Test//服务器端
 61     public void server(){
 62         ServerSocket serverSocket= null;
 63         Socket socket= null;
 64         InputStream inputStream= null;
 65         FileOutputStream fileOutputStream= null;
 66         try {
 67             //1:首先创建一个ServerSocket对象指明自己的端口号
 68             serverSocket = new ServerSocket(8800);
 69             //2:调用accept方法接受传进来的socket对象
 70             socket = serverSocket.accept();
 71             //3:获取输入流
 72             inputStream = socket.getInputStream();
 73             //4:读取输入流
 74             fileOutputStream = new FileOutputStream(new File("1234.jpeg"));
 75             byte[]bytes=new byte[1024];
 76             int len;
 77             while ((len=inputStream.read(bytes))!=-1){
 78                 fileOutputStream.write(bytes,0,len);
 79             }
 80             //返回发送成功给客户端
 81             OutputStream outputStream=socket.getOutputStream();
 82             outputStream.write("数据已经收到".getBytes());
 83             outputStream.close();
 84         } catch (IOException e) {
 85             e.printStackTrace();
 86         } finally {
 87             try {
 88                 if (fileOutputStream!=null)
 89                 fileOutputStream.close();
 90             } catch (IOException e) {
 91                 e.printStackTrace();
 92             }
 93             try {
 94                 if (inputStream!=null)
 95                 inputStream.close();
 96             } catch (IOException e) {
 97                 e.printStackTrace();
 98             }
 99             try {
100                 if (socket!=null)
101                 socket.close();
102             } catch (IOException e) {
103                 e.printStackTrace();
104             }
105             try {
106                 if (serverSocket!=null)
107                 serverSocket.close();
108             } catch (IOException e) {
109                 e.printStackTrace();
110             }
111         }
112     }
113 }

URL:统一资源定位符:

有协议http ,主机名localhost  端口号8080,资源地址xxx 参数列表xxx组成

 1 /**
 2  * URL(Uniform Resource Locator):统一资源定位符表示网络上某一个资源的地址:俗称种子
 3  * 格式:
 4  * 协议     主机名    端口号   资源地址    参数列表
 5  * http     localhost 8080      xxx        xxx
 6  * 常用的一些方法:得到协议名称,等等
 7  */
 8 public class URLTest {
 9     public static void main(String[] args) {
10         URL url= null;
11         try {
12             url = new URL("http://bbs.chinaunix.net/thread-4159645-1-1.html");
13         } catch (MalformedURLException e) {
14             e.printStackTrace();
15         }
16         System.out.println(url.getProtocol());
17     }
18 }

 

posted @ 2021-12-08 19:17  tiiiiii  阅读(58)  评论(0)    收藏  举报