网络编程TCP/UDP

网络编程

如何实现网络的通信

  • ip
  • 端口号
  • 192.168.1.1:3600

网络模型

TCP/IP四层概念模型:应用层、传输层、网络层、数据链路层 。

  • 应用层:HTTP···
  • 传输层:TCP/UDP
  • 网络层:IP/ICMP···
  • 数据链路层:FDDI、PPP···

TCP发送消息接受消息

tcp客户端

package com.seadun.demo.seadun.net.tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/*
 * @desc  tcp客户端
 * @author qihao
 * @date 2021/6/5 18:00
 * @param
 * @return
 */
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
//        1.我们要知道服务器地址端口号
        try {
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 9999;
//        2. 我们创建一个socket连接
            socket = new Socket(address, port);
//        3. 我们向socket发送io消息流
            outputStream = socket.getOutputStream();
            outputStream.write("特殊机密".getBytes());
            System.out.println("你好,客户端已经发送消息");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


tcp服务端(一直开启)

package com.seadun.demo.seadun.net.tcp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
 * @desc tcp服务端
 * @author qihao
 * @date 2021/6/5 18:00
 * @param
 * @return
 */
public class TcpServiceDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ByteArrayOutputStream baos = null;
        try {
//           要有一个地址端口,ip为localhost
            serverSocket = new ServerSocket(9999);
            while (true) {
//           等待客户端连接过来,使用serverSocket。accept进行接收
                socket = serverSocket.accept();
//           读取客户端发过来的消息
                inputStream = socket.getInputStream();
                //该方法可能导致乱码,我们使用管道流
//             byte[] buffer = new byte[1024];
//             int len;
//             while ((len = inputStream.read(buffer))!=-1){
//                 String s = new String(buffer, 0,len);
//                 System.out.println(s);
//             }
                //  管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                System.out.println(baos.toString());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

UCP发送消息接受消息

ucp发送端

package com.seadun.demo.seadun.net.tcp;
import java.net.*;
/*
 * @desc UDP无连接发送消息,UDP不区分客户端和服务端,都可以接受发送消息
 * @author qihao
 * @date 2021/6/5 20:34
 * @param
 * @return
 */
public class UdpDemo01 {
    public static void main(String[] args) throws Exception {
        //建立一个socket
        DatagramSocket datagramSocket = new DatagramSocket();
        //建个包
        String msg = "你好,我们是UDP连接-重要机密";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        //数据,数据的长度开始到结束,要发送给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
        //发送包
        datagramSocket.send(packet);
        //关闭流
        datagramSocket.close();
    }
}

udp接受端

package com.seadun.demo.seadun.net.tcp;
/*
 * @desc UDP无连接发送消息,UDP不区分客户端和服务端,都可以接受发送消息
 * @author qihao
 * @date 2021/6/5 20:35
 * @param
 * @return
 */
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//还是要等待客户端的链接
public class UdpDemo02 {
    public static void main(String[] args) throws IOException {
        //开放端口
        DatagramSocket datagramSocket = new DatagramSocket(9090);
        byte[] buffer = new byte[1024];
        //接受
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        datagramSocket.receive(packet);
        System.out.println(new String(packet.getData()));
        datagramSocket.close();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值