Day 173 网络编程(2)

网络编程(2)

通信协议

网络通信协议:速率,传输码率,代码结构,传输控制…

TCP/IP协议簇:实际上是一组协议

TCP:用户传输协议

UDP:用户数据报协议

IP:网络互连协议

TCP和UDP对比

TCP:

  • 连接、稳定
  • 三次握手、四次断开
  • 客户端、服务端
  • 传输完成、释放连接

UDP:

  • 不连接、不稳定
  • 客户端、服务端:没有明确界限
  • 不管有没有准备好,都可以发

TCP

客户端

  1. 连接服务器Socket
  2. 发送消息
package com.yangxu.net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {

        Socket socket =null;
        OutputStream outputStream = null;
        //要知道服务器的地址
        try {
            InetAddress name = InetAddress.getByName("127.0.0.1");
            //端口号
            int port = 9999;
            //创建一个Socket连接
            socket = new Socket(name,port);
            outputStream = socket.getOutputStream();
            outputStream.write("你好".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务器

  1. 建立服务的端口ServerSocket
  2. 等待用户的链接accept
  3. 接收用户消息
package com.yangxu.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket socket = null;
        Socket accept = null;
        InputStream inputStream =null;
        ByteArrayOutputStream stream =null;


        //这边需要一个地址
        try {
            socket = new ServerSocket(9999);
            //等待客户端连接过来
            accept = socket.accept();
            //读取客户端的消息
            inputStream = accept.getInputStream();
            stream = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes))!=-1){
                stream.write(bytes,0,len);
            }
            System.out.println(stream.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            if (stream!=null){
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件上传

服务器端

package com.yangxu.net;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo02 {
    public static void main(String[] args) {
        ServerSocket socket = null;
        Socket accept = null;
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            socket = new ServerSocket(9000);
            accept = socket.accept();
            inputStream = accept.getInputStream();
            fileOutputStream = new FileOutputStream(new File("receive.jpg"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

客户端

package com.yangxu.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClientDemo02 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;

        try {
            InetAddress name = InetAddress.getByName("127.0.0.1");
            socket = new Socket(name,9000);
            outputStream = socket.getOutputStream();
            fileInputStream = new FileInputStream(new File("jc1612011_1a(1).jpg"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Tomcat

服务器端

  • 自定义 S
  • Tomcat服务器 S:Java后台服务器

客户端

  • 自定义 C
  • 浏览器 B

UDP

不用连接,知道对方的地址即可

发送消息

接收端

package com.yangxu.net2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServerDemo01 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
        socket.receive(packet);
        socket.close();
        System.out.println(new String(packet.getData(),0,packet.getLength()));
    }
}

发送端

package com.yangxu.net2;

import java.io.IOException;
import java.net.*;

//不需要连接服务器
public class UdpClientDemo01 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String msg = "你好,你个服务器";
        InetAddress name = InetAddress.getByName("localhost");
        int port = 9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,name,port);
        socket.send(packet);
        socket.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学习争取保研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值