(Java)网络通信

网络通讯的三要素

IP地址
public class dome {
    public static void main(String[] args)  {
        try {
            //获取本机IP地址对象
            InetAddress localHost = InetAddress.getLocalHost();
            //获取本机名
            System.out.println(localHost.getHostName());
            //获取本机地址名
            System.out.println(localHost.getHostAddress());

            //获取指定IP或局域网的IP地址对象
            InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress.getHostName());
            System.out.println(inetAddress.getHostAddress());

            //判断本机和与该ip地址是否连通
            System.out.println(inetAddress.isReachable(2000));
        } catch (UnknownHostException e) {
            System.out.println(e.getLocalizedMessage());
        } catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

UDP通信

public class Client {
    public static void main(String[] args) throws Exception {
        //创建客户端对象(发韭菜出去的人)
        DatagramSocket socket = new DatagramSocket();


        /*DatagramPacket(byte buf[], int offset, int length,
                InetAddress address, int port)
                参数一:封装要发出去的数据
                参数二:发出去的数据大小(字节个数)
                参数三:服务端的IP地址
                参数四:服务端程序的接口
                */
        Scanner sc =new Scanner(System.in);
        while (true) {
            String msg = sc.nextLine();

            if("exit".equals(msg)){
                System.out.println("关闭客户端");
                socket.close();
                break;
            }
            byte[] bytes = msg.getBytes();
            //创建数据包对象封装要发出的数据
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length,InetAddress.getLocalHost(),6666);

            //发送数据
            socket.send(packet);
            System.out.println("数据发送完毕");
        }
    }
}
public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("--服务端--");
        //创建一个服务端(接韭菜的人) 注册端口
        DatagramSocket socket = new DatagramSocket(6666);

        //创建一个数据包,用于接收对象
        byte[] buffer = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

        while (true) {
            //接收数据
            socket.receive(packet);
            //获取接收了多少数据
            int length = packet.getLength();
            String s = new String(buffer, 0, length);
            System.out.println(s);

            //获取客户端端口和地址
            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(packet.getPort());
        }
    }
}

TCP通信

public class Client {
    public static void main(String[] args) throws Exception {
        //创建Socket对象,并同时请求与服务端连接
        Socket socket = new Socket(InetAddress.getLocalHost(),8888);

        //从Socket管道中得到输出流,用来发送数据
        OutputStream out = socket.getOutputStream();
        //创建数据输出流对象
        DataOutputStream d = new DataOutputStream(out);

        Scanner sc = new Scanner(System.in);
        while (true) {
            //传输数据
            System.out.println("请输入");
            String s = sc.nextLine();
            if("exit".equals(s)){
                System.out.println("退出系统!");
                d.close();
                socket.close();
                break;
            }
            d.writeUTF(s);
            d.flush();
        }
    }
}
public class Server {
    public static void main(String[] args) throws Exception {
        //创建ServerSocket对象,并注册端口
        ServerSocket serverSocket = new ServerSocket(8888);

        //等待客户端连接
        Socket socket = serverSocket.accept();
        //从Socket中得到一个字节输入流,用来接收数据
        InputStream in = socket.getInputStream();
        //创建数据输入流,和前面保持一致
        DataInputStream d = new DataInputStream(in);
        //读入数据
        while (true) {
            try {
                String s = d.readUTF();
                System.out.println(s);
            } catch (IOException e) {
                System.out.println(socket.getRemoteSocketAddress()+"离线了");
                d.close();
                socket.close();
                break;
            }

        }
    }
}

多个客户端

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8888);

        //多线程接收多个客户端的数据
        while(true){
            Socket socket = serverSocket.accept();
            System.out.println(socket.getRemoteSocketAddress()+"上线了");
            new ServerReadSocket(socket).start();
        }
    }
}
public class ServerReadSocket extends Thread{
    private Socket socket;
    public ServerReadSocket(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            InputStream in = socket.getInputStream();
            DataInputStream d = new DataInputStream(in);
            while(true){
                try {
                    String s = d.readUTF();
                    System.out.println(s);
                } catch (Exception e) {
                    System.out.println(socket.getRemoteSocketAddress()+"下线了");
                    socket.close();
                    d.close();
                    break;
                }
            }
        } catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值