java学习笔记——网络编程

网络通信要素


基于tcp协议的网络编程



需要注意的是:

Socket.shutdownInput( )与Socket.shutdownOutput( )方法,如果不使用这两个方法很可能就会造成read阻塞(客户端与服务端都read时)


/*
 * 从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
 */
public class TestTCP3 {
    
    //客户端
    @Test
    public void client() throws IOException{
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
        FileInputStream fis = new FileInputStream("./1.jpg");
        OutputStream os = s.getOutputStream();
        
        byte[] b = new byte[1024];
        int len = 0;
        while((len = fis.read(b)) != -1){
            os.write(b, 0, len);
        }
        
        s.shutdownOutput();
        
        //客户端接收反馈
        InputStream in = s.getInputStream();
        
        while((len = in.read(b)) != -1){
            System.out.println(new String(b, 0, len));
        }
        
        in.close();
        os.close();
        fis.close();
        s.close();
    }
    
    //服务端
    @Test
    public void server() throws IOException{
        ServerSocket ss = new ServerSocket(9898);
        Socket s = ss.accept();
        
        InputStream in = s.getInputStream();
        FileOutputStream fos = new FileOutputStream("2.jpg");
        
        byte[] b = new byte[1024];
        int len = 0;
        while((len = in.read(b)) != -1){
            fos.write(b, 0, len);
        }
        
        s.shutdownInput();
        
        OutputStream os = s.getOutputStream();
        os.write("接收美图成功!".getBytes());
        
        os.close();
        fos.close();
        in.close();
        s.close();
        ss.close();
    }

}



基于udp协议的网络编程


public class TestUDP {
    
    //发送端
    @Test
    public void send() throws IOException{
        
        DatagramSocket ds = new DatagramSocket();
        
        int i = 0;
        
        while(i <= 100){
            String str = "使用 UDP 协议发送数据给接收端! --- " + i++;
            byte[] buf = str.getBytes();
            DatagramPacket dp = new DatagramPacket(buf, 0, buf.length, InetAddress.getByName("127.0.0.1"), 9898);
            
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
            }
            
            ds.send(dp);
        }

        
        ds.close();
    }
    
    //接收端
    @Test
    public void receive() throws IOException{
        DatagramSocket ds = new DatagramSocket(9898);
        
        byte[] buf = new byte[1024];
        
        int i = 0;
        while(i <= 100){
            DatagramPacket dp = new DatagramPacket(buf, buf.length);
            
            ds.receive(dp);
            
            System.out.println(new String(dp.getData(), 0, dp.getLength()));
            
            i++;
        }
        
        
        ds.close();
    }

}



URL编程

public class TestURL {
    
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://down.sandai.net/XLNetAcc/XLNetAccSetup.exe");
        
        /*
            public String getProtocol(  )     获取该URL的协议名
            public String getHost(  )           获取该URL的主机名
            public String getPort(  )            获取该URL的端口号
            public String getPath(  )           获取该URL的文件路径
            public String getFile(  )             获取该URL的文件名
            public String getRef(  )             获取该URL在文件中的相对位置
            public String getQuery(   )        获取该URL的查询名
         */
        
        /*System.out.println(url.getProtocol());
        System.out.println(url.getHost());
        System.out.println(url.getPort());
        System.out.println(url.getPath());
        System.out.println(url.getFile());
        System.out.println(url.getRef());
        System.out.println(url.getQuery());*/
        
        /*InputStream in = url.openStream();
        byte[] b = new byte[1024];
        int len = 0;
        
        while((len = in.read(b)) != -1){
            System.out.println(new String(b, 0, len));
        }
        
        in.close();*/
        
        URLConnection uc = url.openConnection();
        
//        OutputStream os = uc.getOutputStream();
        
        /*InputStream in = uc.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        
        while((len = in.read(b)) != -1){
            System.out.println(new String(b, 0, len));
        }
        
        in.close();*/
        
        InputStream in = uc.getInputStream();
        FileOutputStream fos = new FileOutputStream("d:/xunlei.exe");
        
        byte[] b = new byte[1024];
        int len = 0;
        while((len = in.read(b)) != -1){
            fos.write(b, 0, len);
        }
        
        fos.close();
        in.close();
    }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值