socet网络编程

  一、 网络模型

    应用层   -----     HTTP协议 

    传输层   -----     TCP协议

    网络层   -----       IP协议

    链路层   -----     以太网协议

  二、什么是socket  

    socket是一种

  三、Upd协议与Tcp协议的区别

 

  四、Upd协议下的服务端与客户端的demo

 

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.InetAddress;
 5 //Upd服务端
 6 class UpdService{
 7     
 8     public static void main(String[] args) throws IOException {
 9             System.out.println("upd协议服务端已启动...");
10             //创建端口号   默认ip地址是本机
11             DatagramSocket ds = new DatagramSocket(8080);
12             byte[] buf = new byte[1024];
13             //数据包
14             DatagramPacket dp = new DatagramPacket(buf, 0, buf.length);
15             //会发生阻塞  等待客户端发送请求
16             ds.receive(dp);
17             System.out.println("来源:" + dp.getAddress() + " 端口" +  dp.getPort());
18             System.out.println("接受来自客户端的数据:" + new String(dp.getData(), 0, dp.getLength()));
19             ds.close();    
20     }
21     
22 }
23 
24 //Upd客户端
25 public class UpdClient {
26     public static void main(String[] args) throws IOException {
27         
28             System.out.println("upd协议客户端开始请求...");
29             DatagramSocket ds = new DatagramSocket();
30             String str = "好好学习, 天天向上" ;
31             byte[] buf = str.getBytes();
32             DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 8080);
33             //发送数据包
34             ds.send(dp);
35             ds.close();
36             
37     }
38 }

    五、Tcp协议下的服务端与客户端的demo

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.io.OutputStream;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 //tcp协议下的服务端
 7 class TcpService{
 8     public static void main(String[] args) throws IOException {        
 9         InputStream inputStream = null;
10         ServerSocket serverSocket = null;
11         try {
12             System.out.println("tcp协议下的服务器启动.....");
13             serverSocket = new ServerSocket(8080);
14             //接受客户端发送过来的数据
15             Socket accept = serverSocket.accept();
16             //获取输入流
17             inputStream = accept.getInputStream();
18             byte[] buf = new byte[1024];
19             inputStream.read(buf, 0, buf.length);
20             System.out.println("客户端请求数据:" + new String(buf, 0, buf.length));
21             //返回给客户端的信息
22             OutputStream outputStream = accept.getOutputStream();
23             outputStream.write("损粗".getBytes());
24             outputStream.close();
25         }finally {
26             inputStream.close();
27             if(!serverSocket.isClosed()) serverSocket.close();
28         }    
29     }
30 }
31 //tcp协议下的客户端
32 public class TcpClient {
33     public static void main(String[] args) throws IOException {
34         Socket socket = null;
35         try {
36             System.out.println("tcp协议客户端启动...");
37             socket = new Socket("127.0.0.1", 8080);
38             OutputStream outputStream = socket.getOutputStream();
39             outputStream.write("good good study, day day up!".getBytes());
40         }finally {
41             socket.close();
42         }
43     }
44 }

 

posted @ 2020-08-13 21:56  无虑的小猪  阅读(155)  评论(0编辑  收藏  举报